Replace the grade calculation portion of Lab Assignment #4 with a function as ou
ID: 3797995 • Letter: R
Question
Replace the grade calculation portion of Lab Assignment #4 with a function as outlined by the attached flowchart. The mark[4][5] variable should be a global variable while all other variables should exist only as local variables where they need to be used. The variable j from the main can be transferred to another variable (say k). The i variable does not need to be passed to the function as it will always begin at 0 anyway so you can use a different variable for it as well.
#include <stdio.h>
int main(void)
{
int i=0;
int j=0;
int marks[4][5];
double weight[]={1, 1, 0.8333, 0.75, 0.6667};
for(j=0;j<4;j++){
printf(" Input marks of the 5 subjects separetely of student no : %d",j+1);
for(i=0;i<5;i++){
scanf("%d",&marks[j][i]);
}
}
double grade;
for(j=0;j<4;j++){
grade=0;
for(i=0;i<5;i++){
grade=grade+(weight[i] * marks[j][i]);
}
printf("Student #%d's final marks is %4.2lf ",j + 1,grade);
}
return 0;
}
Explanation / Answer
#include <stdio.h>
int marks[4][5];
double calculate_Grade(int j)
{
double weight[]={1, 1, 0.8333, 0.75, 0.6667};
double grade=0;int i;
for(i=0;i<5;i++){
grade=grade+(weight[i] * marks[j][i]);
}
return grade;
}
int main(void)
{
int i=0;
int j=0;
for(j=0;j<4;j++){
printf(" Input marks of the 5 subjects separetely of student no : %d",j+1);
for(i=0;i<5;i++){
scanf("%d",&marks[j][i]);
}
}
double grade;
for(j=0;j<4;j++){
grade = calculate_Grade(j);
printf("Student #%d's final marks is %4.2lf ",j + 1,grade);
}
return 0;
}