Hey guys!

I'm trying to calculate the average for student marks using pretest loop, and for some reason i get the wrong result, even though the code looks proper to me.,
there are 7 questions all of them totaling 100%, i haveto return average mark
i HAVETO use pretest loop.

Any help would be greatly appreciated!!
here comes the code! Thanx

{
double stdmark;
int i;
int total=0;
int average;

        for(i=1;i<=7;i++)
        {
//        printf("\n");
        printf("Enter Mark#%d:",i);
        scanf("%lf", &stdmark);
        total=total+stdmark;
        }

average=total/i;

return average;
}

How funny: you get double input then use INTEGER arithmetics with int variables...
That's all, guy...

How funny: you get double input then use INTEGER arithmetics with int variables...
That's all, guy...

Thanx a lot for a tip, i changed everything to double except i, cuz if i change i to double the "Enter Mark#0" occurs on every entry, but still the result is abit incorrect, it came closer though, for example if im inputing 4 for every 7 marks i get the result of 3.5 instead of 4

any suggestions, here comes the latest version:

{
double stdmark;
int i;
double total=0;
double average;

        for(i=1;i<8;i++)
        {
//        printf("\n");
        printf("Enter Mark#%d:",i);
        scanf("%lf", &stdmark);
        total=total+stdmark;
        }

average=total/i;

return average;
}

Thanx!

Before average=total/i; just add printf("Tell me what's in i: %d\n", i); and perhaps you'll find out the problem.

Before average=total/i; just add printf("Tell me what's in i: %d\n", i); and perhaps you'll find out the problem.

thanx a lot it sais 8, but when i try to set it <= 7 its still 8.,
any suggestions?

thanx a lot it sais 8, but when i try to set it <= 7 its still 8.,
any suggestions?

Yes. Change to for(i=0;i<7;i++) and add to printf("Enter Mark#%d:",i + 1);

Yes. Change to for(i=0;i<7;i++) and add to printf("Enter Mark#%d:",i + 1);

It works great, but im trying to understand the logic behind it,once we start the loop, i=0, and then in the first pass in the condition it adds 1 to it so i=1, then after printf it adds again? making it 2 still going through the first pass???
please explain., thank you kindly

cheers!

It works great, but im trying to understand the logic behind it,once we start the loop, i=0, and then in the first pass in the condition it adds 1 to it so i=1, then after printf it adds again? making it 2 still going through the first pass???
please explain., thank you kindly

cheers!

No quite. Initially, it will assign the value zero to i, then it will check the condition and then it will go over the block of statements where printf() is one of them. At this point the %d format parameter will still receive the sum of zero + one. After the block {} is fulfilled, i is incremented, and the condition is evaluated once more.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.