Thursday, July 9, 2009

Is my grade program in C correct?

# include %26lt;stdio.h%26gt;


main ()


{





char name [25];


int grade,cout,kay,tom, number,a,b,c,d,f,total, loop;











printf("enter students name\n\n");


scanf("%s",%26amp;tom);


printf("enter the grade\n\n");


scanf("%d",%26amp;grade);








kay= tom+grade;





if (grade%26gt;=90%26amp;%26amp;grade%26lt;=100)


a=a+1;


else if(grade%26gt;=80%26amp;%26amp;grade%26lt;=89)


b=b+1;


else if(grade%26gt;=70%26amp;%26amp;grade%26lt;=79)


c=c+1;


else if(grade%26gt;=60%26amp;%26amp;grade%26lt;=69)


d=d+1;


else if(grade%26lt;=59)


f=f+1;








printf("A's ",a);


printf("B's ",b);


printf("C's ",c);


printf("D's ",d);


printf("F's ",f);


printf("%s grade is ",kay);


}

Is my grade program in C correct?
You have the basic idea, but how are you going to handle more than one grade and more than one student? What you have is the heart of the program, but you need some more code to manage that algorithm better.





You're on the right track. Now go a little further.





One other thing, make sure you initialize your variables before using them, in particular, your grade counters. It's good programming, because you can't always count on them being initialized to zero for you.
Reply:Why did you try to type your program in TC... to know if it is correct or have a syntax error.., by the way you forgot the word getch at the end of your program...
Reply:# include %26lt;stdio.h%26gt;


void main (void)


{


char name [25];


int grade,kay,tom, number,a,b,c,d,f,total, loop;





a = b = c = d = f = total = 0; /* Initialize counters to 0 */


printf("enter students name:");


scanf("%s",name); /* Warning here is that the students name cannot contain a space. So if you enter 'John Smith' here then it upsets the grade input */


/* Use fgets(name, 25, stdin) to allow spaces and also prevent overflow on the name. */


printf("enter the grade\n\n");


scanf("%d",%26amp;grade);





kay= tom+grade; /* What is this line meant to do? */





if (grade%26gt;=90%26amp;%26amp;grade%26lt;=100)


a=a+1;


else if(grade%26gt;=80) /* Extra condition not required */


b=b+1;


else if(grade%26gt;=70)


c=c+1;


else if(grade%26gt;=60)


d=d+1;


else if(grade%26gt;=0) /* This covered grades from 0 - 59 */


f=f+1;





printf("A's = %d",a); /* You forgot to add '%d' specifier */


printf("B's = %d",b); /* So it won't output total number */


printf("C's = %d",c); /* of A's, B's etc recorded */


printf("D's = %d",d);


printf("F's = %d",f);


printf("%s grade is %d",name, kay); /* This is the corrected line */


}

pokemon cards

No comments:

Post a Comment