Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Program Help! (C++)


ghost's Avatar
0 0

ok hey guys i need some help with a program heres the code so far:

#include <iostream.h> #include "Rando.h" #include "apvector.h" long MAX; void print (apvector<long>&d); void fill (apvector<long>&d); void fill(apvector<long>&d) { RandGen rnd; int num=0; for (long i=0; i<MAX; i++) { d[i]+rnd.Randint(50000,100000); } }

void print (apvector<long>&d) { cout<<"Province"<<endl; cout<<"Poll #""\t\t\t" "Candidate #1: ""\t\t\t" "Candidate #2:"<<endl; long div=MAX/2; for (long i=0; i<div; i++) { cout.width(6); cout<<i+1; cout<<"\t\t\t"; cout.width(13); cout<<d[i]; cout<<"\t\t\t"; cout.width(13); cout<<d[i+div]<<endl; } } int main() { cout<<"enter number of polls"<<endl; cin>>MAX; MAX=MAX*2; apvector<long>dat(MAX); fill(dat); print(dat); system("pause"); return 0; }

and heres the output… enter number of polls 10 Province Poll # Candidate #1: Candidate #2: 1 56659 57539 2 75900 99485 3 95157 63162 4 79465 56883 5 56231 86563 6 94063 94658 7 79570 64982 8 90866 87145 9 84064 95350 10 56588 99375 Press any key to continue …

im wondering how i would get the total of all the polls in each coloum?


ghost's Avatar
0 0

In the 'for' loop use 2 variables to store the results from each candidate

Example [UNTESTED]

**
long candidate1Total = 0;
long candidate2Total = 0;
**
for (long i=0; i&lt;div; i++) {
  cout.width(6);
  cout&lt;&lt;i+1;
  cout&lt;&lt;&quot;&#92;t&#92;t&#92;t&quot;;
  cout.width(13);
  cout&lt;&lt;d[i];
  **candidate1Total += d[i];**
  cout&lt;&lt;&quot;&#92;t&#92;t&#92;t&quot;;
  cout.width(13);
  cout&lt;&lt;d[i+div]&lt;&lt;endl;
  **candidate2Total += d[i+div];**
}

Regards CyberLemming


ghost's Avatar
0 0

Oh right. I am not registered at Enigma Group so I wouldn't know that.