Program 1:
(Note: It's highly not recommended that you turn this in, write your own or your teacher will mark you down for stealing someone elses code)
Code:
#include <stdio.h>
#include <stdlib.h>
/* main returns an int, nothing else. If
** your teacher disagrees, then he/she is
** an idiot and shouldn't be teaching.
*/
int main ( void )
{
int numvotes,
user_i,
yes = 0,
no = 0;
printf ( "------- Pocket Election -------\n" );
printf ( "\nHow many votes are to be processed?: " );
(void)scanf ( "%d", &numvotes );
if ( numvotes > 1000 || 0 > numvotes ) {
fprintf ( stderr, "Invalid input, number is out of range\n" );
return EXIT_FAILURE;
}
while ( --numvotes >= 0 ) {
printf ( "Enter 1 for AYE, 0 for NAY: " );
(void)scanf ( "%d", &user_i );
if ( user_i == 0 )
++no;
else if ( user_i == 1 )
++yes;
else {
fprintf ( stderr, "Invalid vote\n" );
++numvotes;
}
}
printf ( "\nEnd Results:\n------------\nAYE: %d\nNAY: %d\n", yes, no );
return EXIT_SUCCESS;
}
Program 2:
This one is just as easy, simply ask for each new grade and add that value to a running total:
totalGrades += newGrade;
Once you have the total, divide it by 5 to get the average. Then it's only a matter of finding the range of the average grade and printing the letter grade. Consider an if..else if..else construct:
Code:
if ( avg >= 70 ) puts ( "You got an A" );
else if ( avg >= 60 && 69 >= avg ) puts ( "You got a B" );
else if ( avg >= 50 && 59 >= avg ) puts ( "You got a C" );
else if ( avg >= 40 && 49 >= avg ) puts ( "You got a D" );
else if ( avg >= 30 && 39 >= avg ) puts ( "You got an E" );
else puts ( "You got an F" );
>Thanks for the somewhat mocking reply!
Don't overreact, we're here to help. If we sound rude or mocking and no one calls us on it then it was within the bounds of good taste for these boards. Accept the information you're given and ignore the attitudes.
-Prelude