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; Originally Posted by newbi well its either 1-4 I suggest that you rewrite your program slightly: Code: #include <stdio.h> #include ...

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,711
    Quote Originally Posted by newbi
    well its either 1-4
    I suggest that you rewrite your program slightly:
    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;
        }
        else if (i == 2)
        {
            C_B = C_B + 1;
        }
        else if (i == 3)
        { 
            C_C = C_C + 1;
        }
        else if (i == 4)
        {
            C_D = C_D + 1;
        }
        else
        {
            printf("DESPITE CLAIMS TO THE CONTRARY, THE VALUE OF i = %d\n", i);
        }
    
        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);
    }
    Notice my more consistent indentation. Anyway, compile and run your program with my modifications. What do you see?
    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

  2. #32
    Registered User
    Join Date
    Oct 2012
    Posts
    21
    Well apart from the "else" change, its all the same. Still "0" value for the Candidates at the end.

    4


    4


    1


    2


    2


    1


    2


    Despite Claims i = 500


    0


    0


    0


    0

  3. #33
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,711
    Did you not see this?
    Code:
    DESPITE CLAIMS TO THE CONTRARY, THE VALUE OF i = 500
    EDIT:
    Well, okay, in case you missed it among all that other output, compile and run this program:
    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("DESPITE CLAIMS TO THE CONTRARY, THE VALUE OF i = %d\n", i);
    
        return (0);
    }
    The whole point here is that you are saying that the value of i will be either 1, 2, 3 or 4. We're telling you that that is not the case. The value of i will be 500. As such, your if-else chain to increment the different variables fails.

    If you want to do things this way with the array, then for each candidate, you need to loop over the array and count the number of votes the candidate was given. Just a single if-else chain that is not within a loop will not do.
    Last edited by laserlight; 10-30-2012 at 04:11 AM.
    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

  4. #34
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,711
    Right, so I repeat:

    The whole point here is that you are saying that the value of i will be either 1, 2, 3 or 4. We're telling you that that is not the case. The value of i will be 500. As such, your if-else chain to increment the different variables fails.

    If you want to do things this way with the array, then for each candidate, you need to loop over the array and count the number of votes the candidate was given. Just a single if-else chain that is not within a loop will not do.

    Of course, in this case you probably do not have to do things this way with the array, but it is really up to you.
    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. #35
    Registered User
    Join Date
    Oct 2012
    Posts
    21
    Alright....I guess i was a bit sleepy b4 i read ur reply. Well i got things working now. Thanks. Now I just have to loop it 5 times for 5 election come out.

  6. #36
    Registered User
    Join Date
    Oct 2011
    Posts
    847
    Wow, I seriously admire laserlight's patience!

    The algorithm she was expecting newbi to arrive at up till post #21 is very simple. In pseudocode:
    Code:
    Initialize an array containing the number of votes per candidate to zero
    Loop for each vote:
        Generate a random integer to match a candidate (number of candidates)
        Increase the number of votes cast for that candidate by one
    End loop
    After the loop you end up with the number of votes given to each candidate. (Note, the loop simulates a voter, not the results. Like laserlight stated, this approach does not record which voter voted for whom, only the totals.)

    In C, I'd definitely use a function for this. A good prototype for such a function would be
    Code:
    void vote(unsigned long candidate[], const int candidates, const unsigned long votes)
    where the first parameter would be an array containing the number of votes per candidate, the second parameter is the number of candidates (entries in the array), and the third parameter the number of votes to cast. When the function returns, you'd know that the sum of entries in candidates[] would be votes.

    I believe unsigned long is the best integer type for number of votes, but feel free to use int instead if it freaks you out.
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    Throwing a curve-ball for advanced programmers who are interested in the subject:

    If you have a vote distribution you wish to simulate, then you could use
    Code:
    void vote(unsigned long candidate[], const unsigned long expected[], const int candidates, const unsigned long votes);
    where the expected[] array contain the distribution of votes, by stating expected number of votes per candidate for some unspecified number of total votes. You sum the total expected number of votes, and generate a random number between zero and that number (excluding the upper limit), and pick the candidate that has that range.

    For example, if you have three candidates, A, B, and C, with their expected vote distribution being 2, 1, and 3 votes, then your random number range would be
    Code:
    0 1 2 3 4 5
    A A B C C C
    and you'd generate a random number between 0 and 6 (excluding the upper limit, so [0, 5], really). If it is 0 or 1, you give the vote to candidate A. If it is 2, you give the vote to candidate B. Otherwise it is 3, 4, or 5, and you give the vote to candidate C.

    There are a large number of different ways to implement this.

    One robust way is to use an array to hold the sum total of estimates for candidates prior to the current candidate, plus the total sum in an extra final element. In the above case, that would be 0 2 3 6. After generating a random number n = [0,6], you find the index i in the array that fulfils cumulativei <= n < cumulativei+1. For a reasonably small number of candidates (say, up to at least a dozen candidates) a linear search will work just fine, but for a much larger number a binary search will usually be more efficient.
    laserlight likes this.

Page 3 of 3 FirstFirst 123
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