-
Need Help
--> what is the purpose of if statement in this code;
--> what is purpose of (i-1) in average formula;
Code:
#include <stdio.h>
#define LIMIT 130
int main()
{
float marks[LIMIT];
float average;
float sum=0.0;
int i=0;
do {
printf("\nEnter the marks of the student(-1 to finish)");
scanf("%f", &marks[i]);
if (marks[i] >= 0)
sum = sum + marks[i];
} while(marks[i++] >= 0);
average = sum/(i-1);
printf ("\nThe average is %.2f\n", average);
return 0;
}
-
What do you think the purpose of the if statement is?
What do you think the purpose of the (i - 1) in the formula?
You have to at least try. We aren't an answer factory.
-
oka i have tried it.....yet it gives no error if i remove these but........i need explaination
-
Why do you think there should be an error if you remove them? The question is "what is the purpose?"
What do those lines do? How does the behavior of the program change if you remove them?
-
"what is the purpose of if statement in this code"
Consider the instructions "-1 to finish".
"what is purpose of (i - 1) in average formula"
Consider the value of i after the loop. (Maybe print it.)
--
See how it sings like a sad heart
And joyously screams out it's pain
Sounds that build high like a mountain
Or notes that fall gently like rain