Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,154,146 members, 7,821,910 topics. Date: Wednesday, 08 May 2024 at 09:30 PM

Help With C - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Help With C (1420 Views)

Help With C# Programming And Omnisharp. / Please I Need Help With C++ / E Commerce Site With C# Mvc (2) (3) (4)

(1) (Reply) (Go Down)

Help With C by rawplantain: 3:20am On Nov 12, 2016
//Below is a simple code, but it,s not working fine, guess there is a logical error but i can,t figure it out
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause"wink or input loop */

int main(int argc, char *argv[]) {
int i;
int n;
int sum =0;
printf("Enter the number of students\n"wink;
scanf("%d\n", &n);
int a[n];
for (i=0;i<n;i++){
printf("Enter the score for student %d\n", i);
scanf("%d\n", &a[i]);
sum=sum+a[i];

}

printf("The sum is %d\n", sum);
float avg;
avg=sum/n;
printf("The average is %f\n", avg);

return 0;
}
// Thanks
Re: Help With C by timtoday: 11:48am On Nov 12, 2016
What is wrong with your code:

1. Check how you are using the function
scanf(...)

remove the newline
"\n"
you are using. scanf is not the same as printf, though they might be similar.

2. Why is your average though declared to be float has integer division? Integer division doesn't given you float "results".
With integer division the "decimal" part is truncated. So you might have to cast one of the operand of the integer division as float
to get what you want. Of course, you will get your answer as "float" with decimal places and ZEROs without casting. But that would
not be the right answer.

What you don't NEED.
Throw out the header
 #include <stdlib.h>
because you don't need it here and it will make your file unnecessarily large.
Why is that? I will allow you to search that out.
My point, use only what you need.

Lastly, format your code properly using spacing. It makes your code easy to read.

Of course, I could have just corrected your code and repost. But I would not have helped you and but now that you have
to make the changes yourself. When you get it I would not have robbed you of the joy of accomplishment.

Hope this helps.

3 Likes 1 Share

Re: Help With C by HouseOfBalloons(f): 2:58pm On Nov 12, 2016
Remove '\n' from your scanf. It shouldn't be there
Re: Help With C by rawplantain: 4:08pm On Nov 12, 2016
timtoday:
What is wrong with your code:

1. Check how you are using the function
scanf(...)

remove the newline
"\n"
you are using. scanf is not the same as printf, though they might be similar.

2. Why is your average though declared to be float has integer division? Integer division doesn't given you float "results".
With integer division the "decimal" part is truncated. So you might have to cast one of the operand of the integer division as float
to get what you want. Of course, you will get your answer as "float" with decimal places and ZEROs without casting. But that would
not be the right answer.

What you don't NEED.
Throw out the header
 #include <stdlib.h>
because you don't need it here and it will make your file unnecessarily large.
Why is that? I will allow you to search that out.
My point, use only what you need.

Lastly, format your code properly using spacing. It makes your code easy to read.

Of course, I could have just corrected your code and repost. But I would not have helped you and but now that you have
to make the changes yourself. When you get it I would not have robbed you of the joy of accomplishment.

Hope this helps.


Thanks alot, the program is now ok.
Re: Help With C by ClintonNzedimma(m): 12:42pm On Nov 16, 2016
remove the \n from the scanf ,it is not an output statement
Re: Help With C by romme2u: 11:46pm On Nov 20, 2016
op try to understand what every character you type in ur code is doing. this will enable u to know what your program is doing. it will also enable to code faster with less error.

(1) (Reply)

Kotlin For Android / Learn Web Development / Django Rest Api - Dropdown Of Countries And States In The World

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 17
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.