# Hints required to complete simple programs

• 06-30-2002
Unregistered
Hints required to complete simple programs
Hi, I'm new to C programming and I could really ungently do with some helpful hints (or partial code if you have the time and patience) to help me complete these simple problems:

Problem 1. A program to allow the user to record a an individuals vote which will be either a 'YES' or 'NO'. It is not known how many people will vote but it will be not more than a maximum of 1000. The number of votes will be entered first, and when all have been cast, the total numbers voting YES and NO will be displayed.

The only problem I have with this one is that I have no idea what code I should use to count the votes from the inputted number and store them.

Problem 2. A program to input the results of a series of 5 examinations, then output the average result together with the overall grade for the average, the grading is as follows:

A = 70%+, B=60-69%, C=50-59%, D=40-49%, E=30-39%, F=0-29%)

I pretty much figured I need to use a loop here. But I have no idea how to write the code to keep checking the current grade against the given grading.

All help is appreciated! Thanks v.much in advance!

DC.
• 06-30-2002
Salem
> will be not more than a maximum of 1000

> The number of votes will be entered first

> what code I should use to count the votes
for ( i = 0 ; i < num_votes ; i++ ) {
// cast a vote
}

Easy money :)
• 06-30-2002
sean345
Problem 1:
You should just keep two global variables
Code:

```int Yes = 0; int No = 0; //Inside a loop you could so something similiar to while(Yes+No<TotalVotes){ if(toupper(Vote[0])=='Y'){     Yes++; } else{     No++; } }```
At the end you could just use printf to display the yes and no votes which would be inside the variables Yes and No.

Problem 2
What you could do is get the input for the scores and store them to an int array.
Code:

```int Scores[5]; int Average = 0; char Grade = 0; //Get the input from the scores using fgets or scanf.  Average = (Scores[0] + Scores[1] + Scores[2] + Scores[3] + Scores[4])/5; //Decide what the grade is if(Average<=29){     Grade = 'F'; } else if(Average>=30 && Average<=39){     Grade = 'E'; } //Keep doing this to determine the grade```
Then you can print the average and the grade.

- Sean
• 06-30-2002
Unregistered
Thanks for the help so far guys. You are saving my life here!

Sorry to trouble you further but here's what I have and its still a bit shabby. Any pointers?

Problem 1

#include <stdio.h>

void main (void)

int yes = 0;
int no = 0;

printf("------- Election Program -------\n");
printf("\nHow many votes are to be processed?: ");

for ( i = 0 ; i < numvotes ; i++ ) {
}

printf("\nTotal number of YES votes is: %d", yes);
printf("\nTotal number of NO votes is: %d", no);

I think im missing a whole lot of code out of this one. But I cant figure how to hammer it all together.

Problem 2

#include <stdio.h>

void main (void)

int scores[5];
int average = 0;

scanf("%d",&scores[1]);
scanf("%d",&scores[2]);
scanf("%d",&scores[3]);
scanf("%d",&scores[4]);
scanf("%d",&scores[5]);
Average = (scores[0] + scores[1] + scores[2] + scores[3] + scores[4])/5;
if(average<=29){
}
else if(average>=30 && average<=39){
}
else if(average>=40 && average<=49){
}
else if(average>=50 && average<=59){
}
else if(average>=60 && average<=69){
}
else if(average>=70){

Thanks!
• 06-30-2002
Sebastiani
Wooo, looks bad, man. You need a GOOD BOOK, better yet, work though at least two or three of them before proceeding any further...

I will tell you something though:

Quote:

for ( i = 0 ; i < numvotes ; i++ ) {
}

"I think im missing a whole lot of code out of this one..."
Yes. You forgot to get the user's input, and and didn't count the votes either. You treat the array numvotes[] as if it were a single integer! Besides that, do you really think someone will manually input one thousand votes? Sheesh.

Quote:

int scores[5]; //...5 cells in use...
...
scanf("%d",&scores[5]); //...using 6th cell, uh-oh...

So don't forget, the array index starts at [0] and ends at [4] for this array...

Good luck man, and don't forget the books.
• 06-30-2002
Unregistered
Books would be a great idea, If these programs were not required to be completed for tomorrow. Thanks for the somewhat mocking reply!

Anyone else?
• 06-30-2002
Prelude
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:

```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" );```