Array Assignment

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JurseyRider734
    lil j the bad b-word
    • Aug 2003
    • 7506

    #1

    Array Assignment

    To help your class you are going to write a program that allows
    the user to enter the last names of the five candidates who ran
    for class president. the user will also enter how many votes each
    candidate received and then output each candidates last name, how
    many votes he/she received, and the percentage of the total votes
    received by the candidate. In addition, your program will output
    the winner of the elcetion.

    For example:

    Candidate Votes Received % of Total Votes
    Smith 110 24.45
    Jameson 35 7.78
    Thomason 150 33.33
    Evans 65 14.44
    Bloom 90 20.00
    Total 450

    The New Class President is Thomason.


    Code:
    // main.cpp : Class President Election
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main&#40;&#41;
    &#123;
       double votes &#91;5&#93;;
       double percent;
       string lName&#91;5&#93;;
       string names;
       double avotes;
       int i;
       double sum=0;
       int index;
       int maxIndex;
       for &#40;i=0; i<5; i++&#41;
       &#123;
          cout<<"Enter the last name of a candidate&#58; ";
          cin>>lName&#91;i&#93;;
          cout<<endl;
          cout<<"Enter how many votes they received&#58; ";
          cin>>votes&#91;i&#93;;
          cout<<endl;
          sum=sum+votes&#91;i&#93;;
          
       &#125;
       cout<<"Candidate\tVotes Received\t\t% of Total Votes"<<endl;
       for &#40;index=0; index<5; index++&#41;
          cout<<votes&#91;index&#93; <<"\t"<<votes&#91;index&#93;;
       cout<<"The total number of votes is "<<sum<<"";
       cout<<endl;
       //string winna;
       //winna=votes&#91;maxIndex&#93;;
       //maxIndex=0;
       //for &#40;index=1; index<5; index++&#41;
          //if &#40;votes&#91;maxIndex&#93; < votes&#91;index&#93;&#41;
             //maxIndex=index;
       //cout<<"The New Class President is&#58; "<<winna<<"";
       //cout<<endl;
       
       for &#40;index=0; index<10; index++&#41;
          cout<<votes&#91;index&#93; <<"\t"<<votes&#91;index&#93;;
       return 0;
    &#125;

    Help/Suggestions? Code is what I did so far in class. Most of the bottom is just a joke i'm not sure what the hell i'm doing.
    Originally posted by Arch0wl
    I'd better be considering I own roughly six textbooks on logic and have taken courses involving its extensive use

    Originally posted by Afrobean
    Originally Posted by JurseyRider734
    the fact that you're resorting to threatening physical violence says a lot anyway.
    Just that you're a piece of shit who can't see reason and instead deserves a fucking beating.
  • Yesssss
    FFR Player
    • Jul 2005
    • 2522

    #2
    RE: Array Assignment

    So far you are doing pretty good.

    The cout in the second for loop should have "<<votes[index]/sum<<"%"", although that gives you a number without a nice two decimal spaces (i.e. %33.333333333333, %20.5).
    As for your commented out code, take out "winna=maxIndex=0;", put "winna=lName[index];" inside the if, and uncomment the rest of your code.
    I have no idea what that last for loop is for so delete it.

    I believe this should work, but I don't have a compiler here at home so I can't check it.
    YOU JUST WON THE GAME! CONGRATULATIONS!

    Comment

    • whiteflame
      FFR Player
      • Sep 2005
      • 378

      #3
      Re: Array Assignment

      Originally posted by JurseyRider734
      Most of the bottom is just a joke i'm not sure what the hell i'm doing.

      Comment

      Working...