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
  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1;    FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){    numout = fread(&num, sizeof(int), 1, file);    c...

  • 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...

  • I have this program that works but not for the correct input file. I need the...

    I have this program that works but not for the correct input file. I need the program to detect the commas Input looks like: first_name,last_name,grade1,grade2,grade3,grade4,grade5 Dylan,Kelly,97,99,95,88,94 Tom,Brady,100,90,54,91,77 Adam,Sandler,90,87,78,66,55 Michael,Jordan,80,95,100,89,79 Elon,Musk,80,58,76,100,95 output needs to look like: Tom Brady -------------------------- Assignment 1: A Assignment 2: A Assignment 3: E Assignment 4: A Assignment 5: C Final Grade: 82.4 = B The current program: import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class main {    public static void main(String[]...

  • 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*...

  • Assume I don't understand C++ Can someone explain this program to me Line by Line? Basically...

    Assume I don't understand C++ Can someone explain this program to me Line by Line? Basically what each line actually does? whats the function? whats the point? Don't tell me what the program does as a whole, I need to understand what each line does in this program. #include #include #include #include #include #define SERVER_PORT 5432 #define MAX_LINE 256 int main(int argc, char * argv[]) {    FILE *fp;    struct hostent *hp;    struct sockaddr_in sin;    char *host;...

  • 8. Rewrite the function shown below so that it is no longer vulnerable to a stack...

    8. Rewrite the function shown below so that it is no longer vulnerable to a stack buffer overflow i void gctinp (ohar *inp, int siz) puts (" Input value "); fgets (inp, siz, stdin); printf("buffer3 getinp read %s\n", inp); 3 4 6 7 void display (char *val) 8 9 10 char tmp [16]; sprintf(tmp, "read val: puts (tmp); %s\n", val); 12 13 int main(int argc, char *argv []) 14 15 16 char buf [16]; getinp (buf, sizeof (buf)); 17 18...

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
ADVERTISEMENT