![]() |
|
C programming failed... - Printable Version +- (wL) Forums (https://war-lords.net/forum) +-- Forum: Resources (https://war-lords.net/forum/forum-31.html) +--- Forum: Programming (https://war-lords.net/forum/forum-33.html) +--- Thread: C programming failed... (/thread-9285.html) |
RE: C programming failed... - Danger255 - Nov 29 2012 M. Bison, post: 81035, member: 359 Wrote:98+99+88+87+79 = 451 which divided by 500 is less than 1. The datatype is an int which means anything beyond the integer is dropped, making it 0. The result is 0 * 100 = 0.THANKS about it MAN!!! THANKS AGAIN!! Double post M. Bison, post: 81035, member: 359 Wrote:PS: If you intend on rounding to the nearest integer, add 0.5 before the cast to int.Please can u explain this further? RE: C programming failed... - M. Bison - Nov 29 2012 Danger255, post: 81078, member: 14867 Wrote:Please can u explain this further?An integer does not store anything beyond the decimal. That means it always rounds down. (int)15.9999 = 15 (int)15.4999 = 15 (int)15.5 = 15 (int)15.1 = 15 Real example: 99+99+99+99+98 = 494 / 500 = 0.988 * 100 = 98.8 (int)98.8 = 98 If you intend on rounding to the nearest integer (whole number) then adding 0.5 remedies this situation: (int)(15.9999 + 0.5) = 16 (int)(15.4999 + 0.5) = 15 (int)(15.5 + 0.5) = 16 (int)(15.1 + 0.5) = 15 Real example: 99+99+99+99+98 = 494 / 500 = 0.988 * 100 = 98.8 (int)(98.8 + 0.5) = 99 Watch out for negative numbers! RE: C programming failed... - Spartacus - Nov 29 2012 why not just define the number variables as float m1,m2,m3,m4,m5,per; RE: C programming failed... - Danger255 - Dec 01 2012 Spartacus, post: 81083, member: 1060 Wrote:why not just define the number variables asI was thinking like the same......... Double post Thanks Bison it worked AND NOW A BIG SALUTE TO PROGRAMMING KING!! ![]() Double post Okay guys moved on to another problem could you get me outta here?? THE CODE: Code: #include <stdio.h>Code: printf("Enter a number");Code: printf("Square of %d is %d\n",num,num*num);I mean the square PROGRAM STOPS! RE: C programming failed... - M. Bison - Dec 03 2012 Here's what is happening: After inputting the number, you're pressing enter. Enter is a newline character and doesn't actually disappear. The subsequent scanf is using the character to terminate the loop. The easiest solution is to add a space before %c. I've also added some newlines to cleanup your output. Try this: Code: #include <stdio.h>Why isn't anyone else replying anymore?
RE: C programming failed... - Dr. gkovr - Dec 03 2012 M. Bison, post: 81277, member: 359 Wrote:Why isn't anyone else replying anymore? Because I've been busy and I am unfamiliar with C syntax :p RE: C programming failed... - Danger255 - Dec 03 2012 M. Bison, post: 81277, member: 359 Wrote:REALLY MAN A S P A C E can change the world!!:eek: Thanks Bison thanks once again RE: C programming failed... - Spartacus - Dec 03 2012 Danger, just remember you can't let Bison keep on doing your homework for you. You have to figure some things out yourself if you want to learn the language better. i miss programming..too bad i'm too much of a noob to do anything special or unique. RE: C programming failed... - Danger255 - Dec 06 2012 Spartacus, post: 81319, member: 1060 Wrote:Danger, just remember you can't let Bison keep on doing your homework for you.lol I dont have homework I am doing it for my interest.....
RE: C programming failed... - Leaky - Dec 24 2012 GAHHH CHANGE THE 500*100 TO JUST BE 5! per=(m1+m2+m3+m4+m5)/5; that was said like 2 times in this thread then you kept going and making it more and more complicated. the /5 is all you need. why were you doing 500 in the first place? |