Thread: Hints required to complete simple programs

  1. #1
    Unregistered
    Guest

    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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > will be not more than a maximum of 1000
    int votes[1000];

    > The number of votes will be entered first
    int num_votes;
    scanf( "%d", &num_votes );

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


    Easy money

  3. #3
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    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
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  4. #4
    Unregistered
    Guest
    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 numvotes[1000]
    int yes = 0;
    int no = 0;

    printf("------- Election Program -------\n");
    printf("\nHow many votes are to be processed?: ");
    scanf("%d",&numvotes);

    for ( i = 0 ; i < numvotes ; i++ ) {
    printf("\nPlease enter the vote result");
    }

    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;
    char grade = 0;

    printf("------- Student Grade Program -------\n");
    printf("\nPlease input the first student grade: ");
    scanf("%d",&scores[1]);
    printf("\nPlease input the second student grade: ");
    scanf("%d",&scores[2]);
    printf("\nPlease input the third student grade: ");
    scanf("%d",&scores[3]);
    printf("\nPlease input the fourth student grade: ");
    scanf("%d",&scores[4]);
    printf("\nPlease input the fifth student grade: ");
    scanf("%d",&scores[5]);
    Average = (scores[0] + scores[1] + scores[2] + scores[3] + scores[4])/5;
    if(average<=29){
    grade = 'F';
    }
    else if(average>=30 && average<=39){
    grade = 'E';
    }
    else if(average>=40 && average<=49){
    grade = 'D';
    }
    else if(average>=50 && average<=59){
    grade = 'C';
    }
    else if(average>=60 && average<=69){
    grade = 'B';
    }
    else if(average>=70){
    grade = 'A'


    Thanks!

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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:

    for ( i = 0 ; i < numvotes ; i++ ) {
    printf("\nPlease enter the vote result");
    }

    "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.






    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.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Unregistered
    Guest
    Books would be a great idea, If these programs were not required to be completed for tomorrow. Thanks for the somewhat mocking reply!

    Still the help you gave was appreciated. Thanks.

    Anyone else?

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  2. a website for simple c programs for beginners
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 02-06-2002, 09:38 PM
  3. Need some really simple help (complete Linux newbie)
    By Hannwaas in forum Linux Programming
    Replies: 11
    Last Post: 12-10-2001, 03:16 PM
  4. Help me with these simple programs
    By Help me in forum C Programming
    Replies: 4
    Last Post: 11-08-2001, 10:38 AM
  5. Need help with simple programs...
    By BCole19 in forum C++ Programming
    Replies: 22
    Last Post: 08-30-2001, 09:45 PM