Like Tree1Likes

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. #16
    Registered User
    Join Date
    Oct 2012
    Posts
    21
    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. #17
    Registered User
    Join Date
    Oct 2012
    Posts
    21
    Quote Originally Posted by newbi View Post
    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. #18
    Registered User
    Join Date
    Oct 2012
    Posts
    21
    M guessing my loop is not executing

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,439
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Registered User
    Join Date
    Oct 2012
    Posts
    21
    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. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,439
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User
    Join Date
    Oct 2012
    Posts
    21
    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
    Last edited by newbi; 10-30-2012 at 02:54 AM.

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,439
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Registered User
    Join Date
    Oct 2012
    Posts
    21
    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);
    }

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,439
    That is because you keep printing votes[0] instead of votes[i].
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #26
    Registered User
    Join Date
    Oct 2012
    Posts
    21
    Well then would you please tell me why my 1st code wasn't working

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,439
    What code? Or rather, what is your current code?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #28
    Registered User
    Join Date
    Oct 2012
    Posts
    21
    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);
    }

  14. #29
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    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. #30
    Registered User
    Join Date
    Oct 2012
    Posts
    21
    Quote Originally Posted by AndiPersti View Post
    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 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generate numbers according to Normal Distribution
    By anirban in forum C Programming
    Replies: 1
    Last Post: 11-27-2010, 07:53 AM
  2. Ranom numbers having uniform distribution
    By edesign in forum C Programming
    Replies: 9
    Last Post: 08-16-2009, 05:56 AM
  3. z-distribution function in C
    By petermichaux in forum C Programming
    Replies: 3
    Last Post: 01-14-2004, 12:37 AM
  4. Which distribution of Linux should I get?
    By joshdick in forum A Brief History of Cprogramming.com
    Replies: 50
    Last Post: 01-19-2003, 08:26 AM
  5. Best Distribution
    By gnu-ehacks in forum Linux Programming
    Replies: 4
    Last Post: 11-21-2001, 02:59 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21