# Distribution of Fix numbers

This is a discussion on Distribution of Fix numbers within the C Programming forums, part of the General Programming Boards category; well for now i just want 2 display 500 votes distributed among 4 candidates. And there is 5 precinct. But ...

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

2. Originally Posted by newbi
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
You can just ignore the for statement altogether.

3. M guessing my loop is not executing

4. 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?

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

6. 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?

7. 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;
Code:
```for (i = 0; i <501; i++)
{
votes[i] = random () % 4 +1
}```
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 4

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

9. 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,
srandom( (unsigned) time(NULL) );

for(i = 0; i < 500; i++)
{
votes [i] = random ( ) % 4 + 1;

loop = 0;
}
return (0);
}```

11. Well then would you please tell me why my 1st code wasn't working

12. What code? Or rather, what is your current code?

13. 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;
srandom( (unsigned) time(NULL) );

for(i = 0; i < 500; i++)
{
votes [i] = random ( ) % 4 + 1;
}

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);
}```

14. Code:
```for(i = 0; i < 500; i++)
{
...
}
if(i == 1)
...
if(i == 2)
...
if(i == 3)
...
if(i == 4)
...```
What's the value of "i" after the loop?

Bye, Andreas

15. Originally Posted by AndiPersti
Code:
```for(i = 0; i < 500; i++)
{
...
}
if(i == 1)
...
if(i == 2)
...
if(i == 3)
...
if(i == 4)
...```
What's the value of "i" after the loop?

Bye, Andreas
well its either 1-4

Page 2 of 3 First 123 Last