Question

This program is a modification of your program for A3a. Instead of accepting input from user through standard input, you will be taking input from a file. You must take the input file name on the command line Be sure to have simple error checking to be sure the file exists and was successfully opened The input file will have the following format: First 1ine: Number of students Second Line: Number of assignments Third Line: Student Names, space delimited Fourth+ Line(s): Grades for all students for one assignment, space delimited Example of input file format: 2 Joel Sophie 100 97 78 92 62 70 Requirements: - Your program must read input from a file in the proper format, NOT stdin - Your program should accept the filename from the commandline as shown in the example below Your program should be able to handle a file of any reasonable size. (I will not use excessively long student names, but there may be MANY students and grades.) Do not just make your arrays really large, you need to dynamically allocate them Example: $ ./a.out infile.txt Joel 100 Sophie 97 92 70

Here is the (A3b-smallgrades.txt) text file:

Here is the (A3b-largegrades.txt) text file:

Here is the Program A3a without modification:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <time.h>

void getGrades(int ROWS, int COLS, int grades[ROWS][COLS], char students[COLS][20]);

void printGrades(int ROWS, int COLS, int grades[ROWS][COLS]);

void getStudents(int COLS, char students[COLS][20]);

void printStudents(int COLS, char students[COLS][20]);

void calcGrades(int ROWS, int COLS, int grades[ROWS][COLS], char Fgrades[]);

void printFinalGrades(int COLS, char Fgrades[]);

int main()

{

   srand(time(0));

   int stu = 0, assign = 0;

   char input[50];

   printf("How many students? ");

   fgets(input, sizeof(input), stdin);

   sscanf(input, "%d", &stu);

   printf("How many assignments? ");

   fgets(input, sizeof(input), stdin);

   sscanf(input, "%d", &assign);

   char students[stu][20];

   int grades[assign][stu];

   char final_grade[stu];

   getStudents(stu, students);

   getGrades(assign, stu, grades, students);

   calcGrades(assign, stu, grades, final_grade);

   printf("\n");

   printStudents(stu, students);

   printGrades(assign, stu, grades);

   printFinalGrades(stu, final_grade);

   return 0;

}

void printFinalGrades(int COLS, char Fgrades[])

{

   int i = 0;

   for (i = 0; i < COLS; ++i)

   {

       printf("%10c", Fgrades[i]);

   }

   printf("\n");

}

void calcGrades(int ROWS, int COLS, int grades[ROWS][COLS], char Fgrades[])

{

   int avg = 0;

   int sum = 0;

   int i = 0, j = 0;

   for (i = 0; i < COLS; ++i)

   {

       // Find the sum

       sum = 0;

       for (j = 0; j < ROWS; ++j)

       {

           sum += grades[j][i];

       }

       avg = sum / ROWS;

       if (avg >= 90)

           Fgrades[i] = 'A';

       else if (avg >= 80)

           Fgrades[i] = 'B';

       else if (avg >= 70)

           Fgrades[i] = 'C';

       else if (avg >= 60)

           Fgrades[i] = 'D';

       else

           Fgrades[i] = 'F';

   }

}

void printStudents(int COLS, char students[COLS][20])

{

   int i = 0;

   int numA = COLS;

   for (i = 0; i < numA; ++i)

   {

       printf("%10s", students[i]);

   }

   printf("\n");

}

void getStudents(int COLS, char students[COLS][20])

{

   int i = 0, len = 0;

   for (i = 0; i < COLS; ++i)

   {

       printf("Enter name for Student %d: ", i);

       fgets(students[i], sizeof(students[i]), stdin);

       len = strlen(students[i]);

       students[i][len-1] = '\0';

   }

}

void printGrades(int ROWS, int COLS, int grades[ROWS][COLS])

{

   int i = 0, j = 0;

   for (i = 0; i < ROWS; ++i)

   {

       for (j = 0; j < COLS; ++j)

       {

           printf("%10d", grades[i][j]);

       }

       printf("\n");

   }

}

void getGrades(int ROWS, int COLS, int grades[ROWS][COLS], char students[COLS][20])

{

   int i = 0, j = 0;

   for (i = 0; i < ROWS; ++i)

       for (j = 0; j < COLS; ++j)

           {

               printf("Enter grade for Assignments %d for %s: ", i, students[j]);

               scanf("%d", &grades[i][j]);

           }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Prototype declaration of functions
void getGrades(int ROWS, int COLS, int grades[ROWS][COLS], char students[COLS][20]);
void printGrades(int ROWS, int COLS, int grades[ROWS][COLS]);
void getStudents(int COLS, char students[COLS][20]);
void printStudents(int COLS, char students[COLS][20]);
void calcGrades(int ROWS, int COLS, int grades[ROWS][COLS], char Fgrades[]);
void printFinalGrades(int COLS, char Fgrades[]);
// File pointer
FILE *fptr;
// main function definition
int main(int argc, char** argv)
{
// To store number of students and number of assignments
int stu = 0, assign = 0;
// Opens the file in read mode
// command line argument argv[1] contains the file name
fptr = fopen(argv[1], "r");
// Checks if the file pointer is NULL then display the error message and stop
if (fptr == NULL)
{
printf("Cannot open file %s\n", argv[1]);
exit(0);
}// End of if condition
// Reads number of students and number of assignments from file
fscanf(fptr, "%d%d", &stu, &assign);
printf("\n Number of Students %d \n Number of Assignment = %d", stu, assign);
// Matrix for student names
char students[stu][20];
// Matrix for assignment marks
int grades[assign][stu];
// Array for grade
char final_grade[stu];
// Calls the functions to read data from file and calculate grade
getStudents(stu, students);
getGrades(assign, stu, grades, students);
calcGrades(assign, stu, grades, final_grade);
printf("\n");
// Calls the function to display data
printStudents(stu, students);
printGrades(assign, stu, grades);
printFinalGrades(stu, final_grade);

return 0;
}// End of main function

// Function to display final grade of all the students
void printFinalGrades(int COLS, char Fgrades[])
{
int i = 0;
// Loops till number of students
for (i = 0; i < COLS; ++i)
{
// Displays grade
printf("%10c", Fgrades[i]);
}// End of for loop
printf("\n");
}// End of function
// Function to calculate grade
void calcGrades(int ROWS, int COLS, int grades[ROWS][COLS], char Fgrades[])
{
// Initializes the variables
int avg = 0;
int sum = 0;
int i = 0, j = 0;
// Loops till number of cols
for (i = 0; i < COLS; ++i)
{
sum = 0;
// Loops till number of rows
for (j = 0; j < ROWS; ++j)
{
// Calculate total
sum += grades[j][i];
}// End of for loop
// Calculate average
avg = sum / ROWS;
// Checks if average is greater than or equals to 90
if (avg >= 90)
// Assign grade as 'A'
Fgrades[i] = 'A';
// Otherwise checks if average is greater than or equals to 80
else if (avg >= 80)
// Assign grade as 'B'
Fgrades[i] = 'B';
// Otherwise checks if average is greater than or equals to 70
else if (avg >= 70)
// Assign grade as 'C'
Fgrades[i] = 'C';
// Otherwise checks if average is greater than or equals to 60
else if (avg >= 60)
// Assign grade as 'D'
Fgrades[i] = 'D';
// Otherwise less than 60
else
// Assign grade as 'F'
Fgrades[i] = 'F';
}// End of for loop
}// End of function
// Function to display student names
void printStudents(int COLS, char students[COLS][20])
{
int i = 0;
int numA = COLS;
// Loops till number of columns
for (i = 0; i < numA; ++i)
{
// Displays name
printf("%10s", students[i]);
}
printf("\n");
}// End of function
// Function to read student names from file
void getStudents(int COLS, char students[COLS][20])
{
int i = 0, len = 0;
// Loops till number of columns
for (i = 0; i < COLS; ++i)
{
// Reads name from file and stores it in students i index position
fscanf(fptr, "%s", students[i]);
// Calculates length
len = strlen(students[i]);
// Assigns null at the end
students[i][len-1] = '\0';
}// End of for loop
}// End of function
// Function to display grade of each student
void printGrades(int ROWS, int COLS, int grades[ROWS][COLS])
{
int i = 0, j = 0;
// Loops till number of rows
for (i = 0; i < ROWS; ++i)
{
// Loops till number of columns
for (j = 0; j < COLS; ++j)
{
// Displays grades
printf("%10d", grades[i][j]);
}// End of inner for loop
printf("\n");
}// End of outer for loop
}// End of function
// Function to read grade from the file
void getGrades(int ROWS, int COLS, int grades[ROWS][COLS], char students[COLS][20])
{
int i = 0, j = 0;
// Loops till number of rows
for (i = 0; i < ROWS; ++i)
{
// Loops till number of columns
for (j = 0; j < COLS; ++j)
{
// Reads grade from the file and stores it in matrix grades
fscanf(fptr, "%d", &grades[i][j]);
}// End of inner for loop
}// End of outer for loop
}// End of function

Sample output:

Number of Students 4
Number of Assignment = 5
Joe Kaitli Tyle Shaw
65 79 81 62
46 59 90 84
63 26 93 68
89 95 72 78
91 61 64 98
C D B C

Add a comment
Know the answer?
Add Answer to:
Here is the (A3b-smallgrades.txt) text file: Here is the (A3b-largegrades.txt) text file: Here is the Program...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN C You will be creating a program to help a teacher calculate final grades for...

    IN C You will be creating a program to help a teacher calculate final grades for a class. The program will calculate a final letter grade base on a standard 90/80/70/60 scale. You will be taking input from a file that contains all of the names and grades of each student. You will find the average for each student and output each student's final letter grade. Your program should accept the filename from the command-line Add error checking to make...

  • The program is done in C. This program opens a file containing binary or text and...

    The program is done in C. This program opens a file containing binary or text and reads every byte in the file and writes both the ASCII hex value for that byte as well as it’s printable (human-readable) character (characters, digits, symbols) to standard output. The issue I am having is when the file has multiple lines being read. The first line is read and done properly but the other lines do not work correctly. For instance, the text file...

  • Please help modify my C program to be able to answer these questions, it seems the...

    Please help modify my C program to be able to answer these questions, it seems the spacing and some functions arn't working as planeed. Please do NOT copy and paste other work as the answer, I need my source code to be modified. Source code: #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { char title[50]; char col1[50]; char col2[50]; int point[50]; char names[50][50]; printf("Enter a title for the data:\n"); fgets (title, 50, stdin); printf("You entered: %s\n", title);...

  • Within this C program change the input to a file instead of individual input from the...

    Within this C program change the input to a file instead of individual input from the user. You must take the input file name on the command line. Be sure to have simple error checking to be sure the file exists and was successfully opened. The input file will have the following format: First line: Number of students Second Line: Number of grades Third Line: Student Names, space delimited Fourth+ Line(s): Grades for all students for one assignment, space delimited....

  • The program reads an unknown number of words – strings that all 20 characters or less...

    The program reads an unknown number of words – strings that all 20 characters or less in length. It simply counts the number of words read. The end of input is signaled when the user enters control-d (end-of-file). Your program prints the number of words that the user entered. ****** How do you I make it stop when control-d is entered. My code: #include <stdio.h> void main(void) { char sentence[100]; int i = 0; int count = 1; printf("Enter a...

  • // READ BEFORE YOU START: // You are given a partially completed program that creates a...

    // READ BEFORE YOU START: // You are given a partially completed program that creates a list of students for a school. // Each student has the corresponding information: name, gender, class, standard, and roll_number. // To begin, you should trace through the given code and understand how it works. // Please read the instructions above each required function and follow the directions carefully. // If you modify any of the given code, the return types, or the parameters, you...

  • Please do in C please. Edit sliding.c. Comments on code would be nice. Thank you. Here is main.c #include "sliding.h" #include <malloc.h> #include <stdlib.h> //Prints grid in row...

    Please do in C please. Edit sliding.c. Comments on code would be nice. Thank you. Here is main.c #include "sliding.h" #include <malloc.h> #include <stdlib.h> //Prints grid in row major order. Tries to ensure even spacing. void print_grid(int * my_array, int rows, int cols){ int i,j; for(i=0; i<rows; i++){ for(j=0; j<cols; j++){ if(my_array[i*cols + j]!=-1){ printf(" %d ", my_array[i*cols + j]); } else{ printf("%d ", my_array[i*cols + j]); } } printf("\n"); } } int main(int argc, char *argv[]) { int seed,rows,cols;...

  • Question: For the picture writing question, if the question says that the picture length (height) and...

    Question: For the picture writing question, if the question says that the picture length (height) and width are multiples of 5, then be prepared (for example) to handle a situation where you are being asked to blacken the fourth (vertical) strip from the left. Code: #include #include #include #include #define BUFFER_SIZE 70 #define TRUE 1 #define FALSE 0 int** img; int numRows; int numCols; int maxVal; FILE* fo1; void addtopixels(int** imgtemp, int value); void writeoutpic(char* fileName, int** imgtemp); int** readpic(char*...

  • This program is in C++ which reserves flight seats. It uses a file imported to get...

    This program is in C++ which reserves flight seats. It uses a file imported to get the seat chart called "chartIn.txt" which looks like this 1 A B C D E F 2 A B C D E F It continues until row 10. Sorry unable to provide the chartIn.txt file. I am having issues with function displaySeats, it displays then but when it comes to 10 the row looks like this 1 0 A B C D E ,...

  • Using the program segment and sample txt file below, write a program that contains a linked...

    Using the program segment and sample txt file below, write a program that contains a linked list of students. The program should allow the user to: 1. initialize list of students 2. add additional student to front of list 3. add additional student to rear of list 4. delete student 5. sort students alphabetically 6. sort students by idNum 7. show number of students in list 8. print students 9. quit program XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX The program should be divided into the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT