PF Assi Stuff PDF
PF Assi Stuff PDF
/**
* @brief Reads student names and scores into the array.
* @param students Array of studentType structures.
* @param size Number of students.
*/
void readStudentsData(studentType students[], int size) {
printf("--- Enter Student Data ---\n");
for (int i = 0; i < size; i++) {
printf("\nStudent %d:\n", i + 1);
printf("First Name: ");
scanf("%19s", students[i].studentFName); // Read with width limit
printf("Last Name: ");
scanf("%19s", students[i].studentLName);
int score;
do { // Validate score
printf("Score (0-100): ");
scanf("%d", &score);
if (score < 0 || score > 100) printf("Invalid score. Try again.\n");
} while (score < 0 || score > 100);
students[i].testScore = score;
}
}
/**
* @brief Assigns grades based on test scores.
* @param students Array of studentType structures.
* @param size Number of students.
*/
void assignGrades(studentType students[], int size) {
for (int i = 0; i < size; i++) {
if (students[i].testScore >= 90) students[i].grade = 'A';
else if (students[i].testScore >= 80) students[i].grade = 'B';
else if (students[i].testScore >= 70) students[i].grade = 'C';
else if (students[i].testScore >= 60) students[i].grade = 'D';
else students[i].grade = 'F';
}
}
/**
* @brief Finds the highest test score.
* @param students Array of studentType structures.
* @param size Number of students.
* @return The highest score.
*/
int findHighestScore(const studentType students[], int size) {
if (size <= 0) return -1;
int highestScore = students[0].testScore;
for (int i = 1; i < size; i++) {
if (students[i].testScore > highestScore) highestScore =
students[i].testScore;
}
return highestScore;
}
/**
* @brief Prints students with the highest score.
* @param students Array of studentType structures.
* @param size Number of students.
* @param highestScore The highest score to match.
*/
void printHighestScoreStudents(const studentType students[], int size, int
highestScore) {
printf("\n--- Highest Score Students (%d) ---\n", highestScore);
if (size == 0 || highestScore == -1) {
printf("No data.\n");
return;
}
for (int i = 0; i < size; i++) {
if (students[i].testScore == highestScore) {
printf("%-15s, %s\n", students[i].studentLName,
students[i].studentFName);
}
}
}
/**
* @brief Prints all student data (name, score, grade).
* @param students Array of studentType structures.
* @param size Number of students.
*/
void printStudentsData(const studentType students[], int size) {
printf("\n--- All Student Records ---\n");
printf("%-15s %-10s %s\n", "Name", "Score", "Grade");
printf("----------------------------------\n");
for (int i = 0; i < size; i++) {
printf("%-15s, %-4s %-10d %c\n",
students[i].studentLName, students[i].studentFName,
students[i].testScore, students[i].grade);
}
}
/**
* @brief Main function. Orchestrates program flow.
*/
int main() {
studentType classStudents[NUM_STUDENTS];
int highestTestScore;
readStudentsData(classStudents, NUM_STUDENTS);
assignGrades(classStudents, NUM_STUDENTS);
highestTestScore = findHighestScore(classStudents, NUM_STUDENTS);
printStudentsData(classStudents, NUM_STUDENTS);
printHighestScoreStudents(classStudents, NUM_STUDENTS, highestTestScore);
return 0;
}
Q.2)
/**
* @brief Loads menu data.
*/
void getData(menuItemType menuList[], int size) {
// Initialize menu items and their prices
strcpy(menuList[0].itemName, "Plain Egg"); menuList[0].itemPrice = 1.45;
strcpy(menuList[1].itemName, "Bacon and Egg"); menuList[1].itemPrice = 2.45;
strcpy(menuList[2].itemName, "Muffin"); menuList[2].itemPrice = 0.99;
strcpy(menuList[3].itemName, "French Toast"); menuList[3].itemPrice = 1.99;
strcpy(menuList[4].itemName, "Fruit Basket"); menuList[4].itemPrice = 2.49;
strcpy(menuList[5].itemName, "Cereal"); menuList[5].itemPrice = 0.69;
strcpy(menuList[6].itemName, "Coffee"); menuList[6].itemPrice = 0.50;
strcpy(menuList[7].itemName, "Tea"); menuList[7].itemPrice = 0.75;
}
/**
* @brief Displays the menu.
*/
void showMenu(const menuItemType menuList[], int size) {
printf("\n--- Johnny's Breakfast Menu ---\n");
printf("%-3s %-20s %s\n", "No.", "Item", "Price");
for (int i = 0; i < size; i++) {
printf("%-3d %-20s $%.2f\n", i + 1, menuList[i].itemName,
menuList[i].itemPrice);
}
printf("Enter item number, 0 to finish.\n");
}
/**
* @brief Calculates and prints the bill.
*/
void printCheck(const menuItemType menuList[], int size) {
int selection;
double totalCost = 0.0;
while (1) {
printf("Select item (1-%d, 0 to finish): ", size);
scanf("%d", &selection);
if (selection == 0) break;
if (selection > 0 && selection <= size) {
totalCost += menuList[selection - 1].itemPrice;
printf("%-20s $%.2f\n", menuList[selection - 1].itemName,
menuList[selection - 1].itemPrice);
} else {
printf("Invalid selection.\n");
}
}
printf("\n--------------------\n");
printf("%-10s $%.2f\n", "Tax", taxAmount);
printf("%-10s $%.2f\n", "Amount Due", amountDue);
printf("--------------------\n");
printf("Thank you!\n");
}
/**
* @brief Main function.
*/
int main() {
menuItemType menuList[MAX_MENU_ITEMS];
getData(menuList, MAX_MENU_ITEMS);
showMenu(menuList, MAX_MENU_ITEMS);
printCheck(menuList, MAX_MENU_ITEMS);
return 0;
}
Q.3)
while (1) {
printf("Select item: ");
scanf("%d", &selection);
if (selection == 0) break;
if (selection > 0 && selection <= MAX_MENU_ITEMS) {
totalCost += menuList[selection - 1].itemPrice;
printf("%-15s $%.2f\n", menuList[selection - 1].itemName,
menuList[selection - 1].itemPrice);
} else {
printf("Invalid selection.\n");
}
}
printf("\n-------------------\n");
printf("%-10s $%.2f\n", "Tax", taxAmount);
printf("%-10s $%.2f\n", "Total", amountDue);
printf("-------------------\n");
printf("Thank you!\n");
}
// Main function.
int main() {
menuItemType menuList[MAX_MENU_ITEMS];
getData(menuList);
showMenu(menuList);
printCheck(menuList);
return 0;
}
Q.4)
#include <stdio.h>
#include <string.h>
#define MAX_ITEMS 8
#define NAME_LEN 15
#define TAX 0.05
typedef struct {
char name[NAME_LEN];
double price;
} MenuItem;
// Display menu
void displayMenu(const MenuItem menu[]) {
printf("\n--- Johnny's Menu ---\n");
for (int i = 0; i < MAX_ITEMS; i++) {
printf("%d. %-15s $%.2f\n", i + 1, menu[i].name, menu[i].price);
}
printf("Enter # (0 to end):\n");
}
printf("\n-------------------\n");
printf("%-10s $%.2f\n", "Tax", tax);
printf("%-10s $%.2f\n", "Total", finalAmount);
printf("-------------------\nThank you!\n");
}
int main() {
MenuItem menu[MAX_ITEMS];
loadMenu(menu);
displayMenu(menu);
printBill(menu);
return 0;
}
Q.5)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
fclose(file_ptr);
return line_count;
}
if (num_lines_read != -1) {
printf("\nThe content of the file are:\n");
for (int i = 0; i < num_lines_read; i++) {
printf("%s\n", file_lines[i]);
}
}
return EXIT_SUCCESS;
}
Q.6)
Q.7)
printf("Enter lines:\n");
for (int i = 0; i < num_lines; i++) {
if (fgets(line_buffer, MAX_LINE_LEN, stdin) != NULL) {
line_buffer[strcspn(line_buffer, "\n")] = 0; // Remove trailing
newline
fprintf(fp, "%s\n", line_buffer); // Write line to file
} else {
printf("Error reading line %d.\n", i + 1);
break;
}
}
fclose(fp);
printf("Successfully appended %d lines to '%s'.\n", num_lines, filename);
}
int main() {
appendLinesToFile();
return 0; // Success
}