Thread: Find the average of random numbers:

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499

    Find the average of random numbers:

    I have the random values down but when I try to assign randValue to another integer I get an error.

    I want to make an inner loop where I find 5 random number 50 times and print the average of those numbers.

    I know I have the bare bones of the below, but without giving me a huge shove in the right direction can I have a poke? I have been programing for three months now. I have noticed when I do simple fun projects I learn more then what my professor assigned to me. The class is over but I want to keep building.

    C++ starts in one week...

    Code:
     #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define NUMBERS 5
    
    int main(int argc, const char * argv[])
    {
        float randValue = 0.0;
        int i;
    
        srand(time(NULL));
        
        for (i=0; i<NUMBERS; i++) {
            randValue= 1 + (int)rand() % 10;
            printf("%6.0f\n",randValue );
        }
        
        
        
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Add up the random values, and divide by NUMBERS. You can do the adding up by starting with a variable that is initialised to zero, and adding every random value to it. Print that variable out after the loop.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    I tried add them but I was printing out the last variable so I decided to make an array. Now I get garbage values. The mathematical implementation is easy its the syntax is once again kicking my butt.


    Code:
     #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define NUMBERS 5
    
    int main(int argc, const char * argv[])
    {
        float randValue = 0.0;
        int i;
        int values[NUMBERS];
        
        srand(time(NULL));
        
        for (i=0; i<NUMBERS; i++) {
            randValue= 1 + (int)rand() % 10;
            printf("%6.0f\n",randValue );
        }
        
        for (i=0; i<NUMBERS; i++) {
        values[5]=+randValue;
        printf("%d",values[i]);
        }
        
        
        
        
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Feb 2013
    Location
    Buea Cameroon
    Posts
    64
    you have a mistake in line 21
    Code:
    values[5]=+randValue;
    

    The array named value can only be indexed from 0 t0 4 since it was declared as
    Code:
    
    
    Code:
     intvalues[5]
    In line 11

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you do not need array
    you need 2 variables: sum and count

    both should be updated in the same loop where you are generating the random values, not in the other loop

    After the loop is finished - you will show the value of sum/count
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    I was asking the wrong question. What I want to do is randomly choose 6 numbers 1-99, and do that 1000 times. Out of each set find the mode. Lastly the output will be 6 random number that occurred the most.

    I was thinking a struct and each node will have six random numbers. I then have to scan each node, and find the mode output the top six numbers generated randomly.

    Any ideas to make this work? I am doing this for fun and I think this will test me a many areas.

  7. #7
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Or do I need a really big array and scan the array for the top 6 numbers that occurred the most.

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if values are in range 1-99 - you need array of 99 mmbers

    Code:
    int count[99] = {0};
    then you generate 1000 numbers increasing corresponding counter

    Code:
    generated = ...;
    counter[generated-1] ++;
    Then you find the biggest counter and store the index+1 as first median

    And repeat all process five more times
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    @CSharpener, thank you and I will use that advice and I will implement that very shortly. Since the semester is over and I have some free time I am trying to apply what I have learned,brain is in fun over drive at the moment. I will post the code in question below.
    I am doing the same thing, but now I have two loops & when they both are == the number is outputted, else keep looping.
    I will have a different function later for using the advice @CSharpener gave me.

    I have a feeling I the code is not working correctly because my first number is always 20 which is why I switched it to 7 numbers instead of 6. Seems the numbers below 20 are random, but the first number is not.
    Code:
     #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define NUMBERS 7
    
    int main(int argc, const char * argv[])
    {
        float randValue = 0.0;
        float randValue2=0.0;
        
        int i=0,j = 0;
       
        srand(time(NULL));
        
        while (randValue!=randValue2) 
        
        for (i=0; i<NUMBERS; i++) {
            randValue= 1 + (int)rand() % 49;
            printf("%6.0f\n",randValue );
        }
        
        for (j=0; j<NUMBERS; j++) {
            randValue2= 1 + (int)rand() % 49;
            printf("%6.0f\n", randValue2 );
        }
        
        return 0;
    
    }

  10. #10
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by jocdrew21 View Post
    @CSharpener, thank you and I will use that advice and I will implement that very shortly. Since the semester is over and I have some free time I am trying to apply what I have learned,brain is in fun over drive at the moment. I will post the code in question below.
    I am doing the same thing, but now I have two loops & when they both are == the number is outputted, else keep looping.
    I will have a different function later for using the advice @CSharpener gave me.

    I have a feeling I the code is not working correctly because my first number is always 20 which is why I switched it to 7 numbers instead of 6. Seems the numbers below 20 are random, but the first number is not.
    Code:
     #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define NUMBERS 7
    
    int main(int argc, const char * argv[])
    {
        float randValue = 0.0;
        float randValue2=0.0;
        
        int i=0,j = 0;
       
        srand(time(NULL));
        
        while (randValue!=randValue2) 
        
        for (i=0; i<NUMBERS; i++) {
            randValue= 1 + (int)rand() % 49;
            printf("%6.0f\n",randValue );
        }
        
        for (j=0; j<NUMBERS; j++) {
            randValue2= 1 + (int)rand() % 49;
            printf("%6.0f\n", randValue2 );
        }
        
        return 0;
    
    }
    Why are you trying to use floats with this? Rand() doesn't return a float, so there is no point. Also, if you are trying to just print numbers and find the average, you don't need two loops. You could handle all of it in one loop.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    #define NUMBERS 7
    
    
    int main( )
    {
        int randValues[NUMBERS] = {0}, sum = 0, i = 0;
    
    
        srand(time(NULL));
    
    
        for (i = 0; i < NUMBERS; sum += randValues[i], i++)
            printf("%d ", randValues[i] = ((rand() % 49)+1));
    
    
        sum /= NUMBERS;
    
    
        printf("\nAverage : %d", sum);
    
    
        return 0;
    }
    Edit : I just read that you wanted mode not average, but my advice about floats still applies.
    Last edited by HelpfulPerson; 08-18-2013 at 03:42 PM.
    "Some people think they can outsmart me, maybe. Maybe. I've yet to meet one that can outsmart bullet" - Meet the Heavy, Team Fortress 2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read n numbers and find their total, min, max, average
    By jackson6612 in forum C++ Programming
    Replies: 2
    Last Post: 04-23-2011, 04:04 PM
  2. find & print the average of the odd numbers
    By zozo995 in forum C++ Programming
    Replies: 1
    Last Post: 10-27-2010, 10:31 PM
  3. function to find average
    By nynicue in forum C Programming
    Replies: 4
    Last Post: 03-24-2009, 05:30 PM
  4. Replies: 4
    Last Post: 03-03-2003, 03:52 PM
  5. How can I find the average in the ARRAYS?
    By Nate2430 in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2001, 11:21 AM