Correct this code please: #include <stdio.h> int main(){ int m,n,i,j,k,p,q; prin
ID: 3879216 • Letter: C
Question
Correct this code please:
#include <stdio.h>
int main(){
int m,n,i,j,k,p,q;
printf("Hom many students? ");
scanf("%d",&m);
printf("How many assignments? ");
scanf("%d",&n);
//student names
char student_name[5][25];
for(i=0;i<m;i++)
{
printf("Enter name for Student %d: ",i);
scanf(" %[^ ]",student_name[i]);
}
//grades
for(j=0;j<n;j++)
{
char student_grade[5][25];
for(k=0;k<m;k++)
{
printf("Enter grade for Assignment %d for %s: ",j,student_name[k]);
scanf(" %[^ ]",&student_grade[j]);
}
}
//final grades
for(i=0;i<m;i++)
{
char student_finalgrade[5][25];
printf("Enter final grade for Student %d: ",i);
scanf(" %[^ ]",&student_finalgrade[i]);
}
//printFinalgrades
for(k=0;k<m;k++)
{
printf("%s ",student_name[k]);
}
printf(" ");
for(q=0;q<m;q++)
{
for(p=0;p<n;p++)
{
char student_grade[5][25];
printf("%s",student_grade[i]);
}
printf(" ");
}
for(i=0;i<m;i++)
{
char student_finalgrade[5][25];
printf("%s ",student_finalgrade[i]);
}
}
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. Your program should: - Ask the user how many students, and how many assignments there are - Get student names from the user - Get grades from the user - Calculate final grades - Print the report Requirements: - Must use multidimensional arrays hold student names - 2D array to - 2D array to hold all grades - Array to hold final letter grades Functions (You must fill in the parameters): void getGrades (); void printGrades (); void getStudents(); void printstudents ; void calcGrades) void printFinalGrades)Explanation / Answer
Solution:
code(Completely Working):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// function declarations
void getStudents(char* names[], int noOfStuds);
void getGrades(int** grade, int noOfStuds, int noOfTests);
void calcGrades(char gradeletter[], int** grade, int noOfStuds, int noOfTests);
void printStudents(char* names[], int noOfStuds);
void printGrades(int** grade, int noOfStuds, int noOfTests);
void printFinalGrades(char gradeletter[], int noOfStuds);
int main()
{
char buff[BUFSIZ];
// declaring variables
int noOfStuds, noOfTests, i;
// getting the inputs entered by the user
printf("How many students :");
scanf("%d", &noOfStuds);
printf("How many assignments :");
scanf("%d", &noOfTests);
// creating two dimensional array which stores grades
int* grade[noOfStuds];
for (i = 0; i < noOfStuds; i++)
grade[i] = (int*)malloc(noOfTests * sizeof(int));
// creating array which stores student names
char* names[noOfStuds];
// creating array which stores student grade letters
char gradeletter[noOfStuds];
// calling the functions
getStudents(names, noOfStuds);
getGrades(grade, noOfStuds, noOfTests);
calcGrades(gradeletter, grade, noOfStuds, noOfTests);
printStudents(names, noOfStuds);
printGrades(grade, noOfStuds, noOfTests);
printFinalGrades(gradeletter, noOfStuds);
return 0;
}
// This function will get the student names entered by the user
void getStudents(char* names[], int noOfStuds)
{
int i;
char ch;
char str[40];
while ((ch = getchar()) != ' ' && ch != EOF)
;
for (i = 0; i < noOfStuds; i++)
{
printf("Enter name of student#%d :", (i + 1));
gets(str);
names[i] = (char*)malloc(sizeof str);
strcpy(names[i], str);
}
}
// This function will get the student grades entered by the user
void getGrades(int** grade, int noOfStuds, int noOfTests)
{
int i, j;
char buff[BUFSIZ];
for (i = 0; i < noOfStuds; i++)
{
printf("Student #%d : ", (i + 1));
for (j = 0; j < noOfTests; j++)
{
printf("Enter score of student%d :", (j + 1));
scanf("%d", &grade[i][j]);
}
}
}
// This function will calculate the grade letter
void calcGrades(char gradeletter[], int** grade, int noOfStuds, int noOfTests)
{
int i, j;
double tot = 0.0, avg = 0.0;
for (i = 0; i < noOfStuds; i++)
{
for (j = 0; j < noOfTests; j++)
{
tot += grade[i][j];
}
avg = tot / noOfTests;
if (avg >= 90)
{
gradeletter[i] = 'A';
}
else if (avg >= 80 && avg < 90)
{
gradeletter[i] = 'B';
}
else if (avg >= 70 && avg < 80)
{
gradeletter[i] = 'C';
}
else if (avg >= 60 && avg < 70)
{
gradeletter[i] = 'D';
}
else if (avg >= 0 && avg < 60)
{
gradeletter[i] = 'F';
}
tot = 0.0;
}
}
// This function will display student names
void printStudents(char** names, int noOfStuds)
{
int i;
printf(" ");
for (i = 0; i < noOfStuds; i++)
{
printf("%s", names[i]);
printf(" ");
}
printf(" ");
}
// This function will display student grades
void printGrades(int** grade, int noOfStuds, int noOfTests)
{
int i, j;
for (i = 0; i < noOfTests; i++)
{
for (j = 0; j < noOfStuds; j++)
{
printf("%d ", grade[j][i]);
}
printf(" ");
}
}
// This function will display student grade letters
void printFinalGrades(char gradeletter[], int noOfStuds)
{
int i;
for (i = 0; i < noOfStuds; i++)
{
printf("%c ", gradeletter[i]);
}
printf(" ");
}
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)