well for now i just want 2 display 500 votes distributed among 4 candidates. And there is 5 precinct. But i'm not worrying about that right now. I just want 2 display the votes for 5 candidates
well for now i just want 2 display 500 votes distributed among 4 candidates. And there is 5 precinct. But i'm not worrying about that right now. I just want 2 display the votes for 5 candidates
M guessing my loop is not executing
Well, okay, but what is the algorithm that you are trying to implement? That is, if Rip van Winkle, who just awoke from his sleep that started before the Industrial Revolution, asked you to explain what you were trying to do, step by step, what would you say to him?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
1. Describe the program
2. Randomly generate 5 election outcome from 5 precinct.
a. Each precinct has 500 voters
b. 500 votes are distributed among 4 candidates
3. Add up the total votes for each candidates from each precinct
4. Declare who is the winner
Good, let's concentrate on 2b. 500 votes are distributed among 4 candidates
What are the details for the algorithm that you intend to implement to do that?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
1. Create an array of 500 voters, name it Votes [500]
2. Now using an index execute the array 500 times to produce a random number between 1 to 4
like;
each votes that produce # 1-4 randomly 1 goes to candidate 1. 2 goes to candidate 2, 3 goes to candidate 3, 4 goes to candidate 4Code:for (i = 0; i <501; i++) { votes[i] = random () % 4 +1 }
Last edited by newbi; 10-30-2012 at 02:54 AM.
Okay, that idea will work, and is appropriate if you actually want to track what each voter voted. One mistake though: the loop condition should be i < 500, not i < 501, since you want to loop 500 times, starting from 0.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
i tried this code but it keeps giving me the same value every time
Code:#include <stdio.h> #include <time.h> #include <stdlib.h> int main (void) { int C_A = 0, C_B = 0, C_C = 0, C_D = 0; int loop = 0, i = 0, int votes [500]; srandom( (unsigned) time(NULL) ); for(i = 0; i < 500; i++) { votes [i] = random ( ) % 4 + 1; printf("\n %d \n", votes[0]); loop = 0; } return (0); }
That is because you keep printing votes[0] instead of votes[i].
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Well then would you please tell me why my 1st code wasn't working
What code? Or rather, what is your current code?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Code:#include <stdio.h> #include <time.h> #include <stdlib.h> int main (void) { int C_A = 0, C_B = 0, C_C = 0, C_D = 0; int loop = 0, i = 0, pre = 0; int votes [500]; srandom( (unsigned) time(NULL) ); for(i = 0; i < 500; i++) { votes [i] = random ( ) % 4 + 1; printf("\n %d \n", votes[i]); } if(i == 1) { C_A = C_A + 1; } if(i == 2) { C_B = C_B + 1; } if(i == 3) { C_C = C_C + 1; } if(i == 4) { C_D = C_D + 1; } loop = 0; printf("\n %d \n", C_A); printf("\n %d \n", C_B); printf("\n %d \n", C_C); printf("\n %d \n", C_D); return (0); }
What's the value of "i" after the loop?Code:for(i = 0; i < 500; i++) { ... } if(i == 1) ... if(i == 2) ... if(i == 3) ... if(i == 4) ...
Bye, Andreas