I am trying to write a code to find the lowest grade among 10 quizzes and calculate the final grade of quiz, midterm and final exam.
This is the code. When i run it, it just says program finished with exit code 0.
#include <stdio.h>
#include <math.h>
int main(void)
{
int i, quiz[i], min, QUIZ, final_grade;
double MID, FINAL;
/*loop store quiz grades*/
for (i=1; i<=10; i)
{
printf ("Enter quiz%d grade:", i);
scanf ("%d", &quiz[i]);
}
/*store midterm and final value*/
printf ("Enter midterm grade:");
scanf ("%lf", &MID);
printf ("Enter final exam grade:");
scanf ("%lf", &FINAL);
/*find sum of quiz score*/
QUIZ=0;
for (i=1; i<=10; i)
{
QUIZ = quiz[i];
}
printf ("%d",QUIZ);
/*find min quiz score*/
for (i=1; i<=10; i)
{
min=quiz[1];
if (quiz[i]<min);
min=quiz[i];
}
printf ("\n%d",min);
/*calculate quiz pecent score*/
QUIZ = 2.5*(QUIZ-min)/9;
printf ("\n%d",QUIZ);
/*calculate final grade*/
if (MID>=FINAL)
{
final_grade=MID*.35 FINAL*.4 QUIZ;
}
else
{
final_grade=MID*.25 FINAL*.5 QUIZ;
}
printf ("\nThe final grade is:%d", final_grade);
return 0;
}
It works fine in this part
{
int i, quiz[i];
/*loop store quiz grades*/
for (i=1; i<=10; i)
{
printf ("Enter quiz%d grade:", i);
scanf ("%d", &quiz[i]);
}
int min;
min=1;
/*find min quiz grade*/
if (quiz[i]<quiz[min])
{
min=i;
}
quiz[min]=0;
printf ("%d", quiz[min]);
return (0);
}
But when I add more work to the program it doesnt take input anymore.
CodePudding user response:
well stright off this is wrong
int i, quiz[i]
since you havent given i
a value how big do you expect quiz
to be
I changed to
int i, quiz[10]
after that it runs and gives me output, not sure if its the correct ouput though.
Enter quiz1 grade:3
Enter quiz2 grade:4
Enter quiz3 grade:5
Enter quiz4 grade:
5
Enter quiz5 grade:5
Enter quiz6 grade:5
Enter quiz7 grade:5
Enter quiz8 grade:5
Enter quiz9 grade:5
Enter quiz10 grade:5
Enter midterm grade:5
Enter final exam grade:6
47
5
11
The final grade is:15
CodePudding user response:
In your code
int i, quiz[i], min, QUIZ, final_grade;
you are using i
uninitialized. The value of i
at that point is indeterminate. Your program behaviour is undefined, therefore.
You need to ensure to populate i
with a valid value, before you can use that to define the VLA size.
CodePudding user response:
int i, quiz[i];
This is wrong. You have not given how big i
, and therefore quiz
, is supposed to be.
This is correct way:-
int i, quiz[5]; //you can change the size.
CodePudding user response:
use the intialization with 0
int i = 0
then start the loop with i = 0 as well
for(i = 0 ; i <= 10 ; i)
And everything should be good to go