English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

ok I have a program that reads in values from a file which contains a bunch of numbers, it tallies these numbers as votes in 6 different races and prints out the number of votes for each candidate as well as the winner and the percentage of votes, i need to rewrite the program using arrays which i dont know how to do, heres the program:
#include
#include
using namespace std;
void
tally(int&,int&,int&,int&,int&...
void print(int, int, int, int);

int main() {
int p1=0,p2=0,vp1=0,vp2=0,s1=0,s2=...
int vote;


ifstream voter;
voter.open("ballot.txt");




while(!voter.eof()){

2007-03-20 01:07:51 · 1 answers · asked by Alex P 2 in Computers & Internet Programming & Design

voter>>vote;

if (vote==1){
p1+=1;}
if (vote==2){
p2+=1;}
if (vote==3){
vp1+=1;}
if (vote==4){
vp1+=1;}
if (vote==5){
s1+=1;}
if (vote==6){
s2+=1;}
if (vote==7){
t1+=1;}
if (vote==8){
t2+=1;}
if (vote==9){
j1+=1;}
if (vote==10){
j2+=1;}
if (vote==11){
sen1+=1;}
if (vote==12){
sen2+=1;}
if (vote==0){
p1+=0;}

}
voter.close();

cout<<"CS-114 Ballot Counter"<

cout< print(1, 2, p1, p2);

cout< print(3, 4, vp1, vp2);
cout< print(5, 6, s1, s2);

cout< print(7, 8, t1, t2);

cout< print(9, 10, j1, j2);

cout< print(11, 12, sen1, sen2);
}

2007-03-20 01:08:19 · update #1

void print(int candidate1, int candidate2, int vote1, int vote2){


if (candidate1==1&&candidate2==2)...
int p1=vote1, p2=vote2;


cout<<"President"<<'\t'<<'\t'<...
cout<<"Peter
Parker"<<'\t'<<'\t'<p... !!!";
cout< Murdock"<<'\t'<<'\t'< !!!"<
}
if (candidate1==3&&candidate2==4)...
int p3=vote1, p4=vote2;


cout<<" Vice
President"<<'\t'<<'\t'<<"Votes...
cout<<"Clark Kent"<<'\t'<<'\t'< (p3)/(p3+p4)*100)<<"%";if(p3>p... !!!";
cout< Wayne"<<'\t'<<'\t'<p...
!!!"<
}


if (candidate1==5&&candidate2==6)...
int p5=vote1, p6=vote2;



cout<<"Secretary"<<'\t'<<'\t'<...
cout<<"Sue Storm"<<'\t'<<'\t'< (p5)/(p5+p6)*100)<<"%";if(p5>p... !!!";
cout< Gray"<<'\t'<<'\t'<p...
!!!"<
}

2007-03-20 01:08:44 · update #2

if (candidate1==7&&candidate2==8)...
int p7=vote1, p8=vote2;



cout<<"Treasurer"<<'\t'<<'\t'<...
cout<<"Scott
Summers"<<'\t'<<'\t'<p... !!!";
cout< Blaze"<<'\t'<<'\t'<p...
!!!"<
}


if (candidate1==9&&candidate2==10...
int p9=vote1, p10=vote2;



cout<<"Judge"<<'\t'<<'\t'<<'\t...
cout<<"Hal Jordan"<<'\t'<<'\t'< (p9)/(p9+p10)*100)<<"%";if(p9>... !!!";
cout< Drake"<<'\t'<<'\t'< !!!"<
if (candidate1==11&&candidate2==1...
int p11=vote1, p12=vote2;



cout<<"Senator"<<'\t'<<'\t'<<'...
cout<<"Steve
Rogers"<<'\t'<<'\t'< !!!";
cout< Kyle"<<'\t'<<'\t'< !!!"<
}
cout< }

2007-03-20 01:09:07 · update #3

Ok the only things im supposed to change is the tally function and the print function
the tally function is supposed to be changed to
void tally(int votes[12]);
and the print function is supposed to be changed to
void print(int candidate1, int candidate2, int votes [12]);
so they both have to incorporate arrays, which i dont really know how to do help plz

2007-03-20 01:09:28 · update #4

1 answers

Think of an array as a row in an excel worksheet. Each cell is an array position. Each position has the same datatype. So, if you have 12 candidates, you could keep track of their votes in an array as follows:

int candidates[12];

Arrays always start at position 0. Suppose you have Fred and Joe as candidates. Fred can be candidate[0] and Fred is candidate[1]. Then you can use them just as any other int. For example, to add one to the vote count for Fred, just do this:

candidate[1]=candidate[1]+1;

(Or shorter, candidate[1]++; )

There's more to arrays, but this should get you going.

2007-03-21 19:49:06 · answer #1 · answered by BigRez 6 · 0 0

fedest.com, questions and answers