(wL) Forums
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)

Pages: 1 2 3


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.

Try this:
Code:
#include<stdio.h>
int main()
{
int m1,m2,m3,m4,m5,per;
printf("Enter the marks of five subjects\n");
scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5);
per=(int)(((float)(m1+m2+m3+m4+m5))/500.0*100.0);
if(per>=80)
printf("First Division\n");
else if(per>=60)
printf("Second Division\n");
else if(per>=50)
printf("Third Division\n");
else
printf("Fail");
return 0;
}

PS: If you intend on rounding to the nearest integer, add 0.5 before the cast to int.
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 as

float m1,m2,m3,m4,m5,per;
I was thinking like the same.........

Double post

Thanks Bison it worked AND NOW A BIG SALUTE TO PROGRAMMING KING!!Big Grin

Double post

Okay guys moved on to another problem could you get me outta here??
THE CODE:
Code:
#include <stdio.h>
int main()
{
char another;
int num;
do
{
printf("Enter a number");
scanf("%d",&num);
printf("Square of %d is %d\n",num,num*num);
printf("Want to enter another number? y/n");
scanf("%c",&another);
} while(another=='y');
return 0;
}
Now the problem is here
Code:
printf("Enter a number");
scanf("%d",&num);
printf("Square of %d is %d\n",num,num*num);
printf("Want to enter another number? y/n");
scanf("%c",&another);
after this executed
Code:
printf("Square of %d is %d\n",num,num*num);
Nothing HAPPENS IT DOESNT ASK!! Y/N!
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>
int main()
{
char another;
int num;
do
{
printf("Enter a number\n");
scanf("%d",&num);
printf("Square of %d is %d\n",num,num*num);
printf("Want to enter another number? y/n\n");
scanf(" %c",&another);
} while(another=='y');
return 0;
}

Why isn't anyone else replying anymore? Rolleyes


RE: C programming failed... - Dr. gkovr - Dec 03 2012

M. Bison, post: 81277, member: 359 Wrote:Why isn't anyone else replying anymore? Rolleyes

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:
Code:
#include <stdio.h>
int main()
{
char another;
int num;
do
{
printf("Enter a number\n");
scanf("%d",&num);
printf("Square of %d is %d\n",num,num*num);
printf("Want to enter another number? y/n\n");
scanf(" %c",&another);
} while(another=='y');
return 0;
}
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.

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.
lol I dont have homework I am doing it for my interest.....Rolleyes


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?