Thread: Need Help with Smallest and Biggest number from array

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    6

    Need Help with Smallest and Biggest number from array

    Hello,
    I need help with my code.
    I need to get an average smallest and biggest number from the 20 randomly generated numbers, I have the average but cant get the smallest number and biggest to work.
    Btw the random generated numbers have to be up to 8 numbers in a line.

    here is my code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    
    int main( )
    {
    int randValues[20] = {0}, sum = 0, i = 0;
    
    
    srand(time(NULL));
    
    
    for (i = 0; i < 8; sum += randValues[i], i++)
    printf("%d ", randValues[i] = ((rand() % 100)+1));
    printf("\n");
    for (i = 0; i < 8; sum += randValues[i], i++)
    printf("%d ", randValues[i] = ((rand() % 100)+1));
    printf("\n");
    for (i = 0; i < 4; sum += randValues[i], i++)
    printf("%d ", randValues[i] = ((rand() % 100)+1));
    printf("\n");
    
    
    sum /= 20;
    
    printf("\nAverage : %d", sum);
    
    return 0;
    }
    thanks

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    First off, make sure your code has proper indentation:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
     
    int main( )
    {
        int randValues[20] = {0}, sum = 0, i = 0;
    
        srand(time(NULL));
    
        for (i = 0; i < 8; sum += randValues[i], i++)
            printf("%d ", randValues[i] = ((rand() % 100)+1));
        printf("\n");
    
        for (i = 0; i < 8; sum += randValues[i], i++)
            printf("%d ", randValues[i] = ((rand() % 100)+1));
        printf("\n");
    
        for (i = 0; i < 4; sum += randValues[i], i++)
            printf("%d ", randValues[i] = ((rand() % 100)+1));
        printf("\n");
     
        sum /= 20;
     
        printf("\nAverage : %d", sum);
     
        return 0;
    }
    Why are you using three separate loops to generate your 20 random numbers? Not only does this not make sense, you're also not assigning each random number to a unique position in the array. The second loop overwrites the first 8 elements in the array, and the third loop overwrite the first 4 elements. This means the last 12 elements do not contain any valid values. If you need to print up to 8 numbers per line, just figure out the logic to print a newline when "i" is at specific values.

    I'd also suggest putting the sum additions into the body of the loop, rather than in the increment portion of the "for()", for clarity. You might also want to move the assignments to "randValues[i]" out of the "printf()", for the same reason.

    Finally, "sum" is in integer, so the resulting "average" will be an integer (any decimal portion will be truncated).

    I would suggest putting the min/max aside until you get the unnecessary multiple-loop scenario straightened out. Also, when you're ready, you should show your attempt at finding the min/max.
    Last edited by Matticus; 01-06-2016 at 10:24 AM.

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    68
    20 random INTs that I assume can be really high numbers, will overflow sum as that one is also a INT

    Why are you doing 8+8+4 loops?, as is you never go over [7] in array, so why even bather having 20 array size
    if you want to print a new line after each 8, make the loop 20 but charge to this: if ((i & 7) == 7) printf("\n");

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by tonyp12
    20 random INTs that I assume can be really high numbers, will overflow sum as that one is also a INT
    The expression (rand() % 100)+1 means that the numbers actually assigned will be in the range [1, 100], so at most the sum will be 20 * 100 = 2000, which is well within the minimum range guaranteed for int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2015
    Posts
    6
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    int main(void)
    
    
    {
        int sum, average, small, big, x=0;
        int num[20]={0};
        srand(time(NULL));
        
        sum=small=big=num[20]=rand()%100+1;
        printf("%d ",num[x]);
        
        for(x=1;x<20;x++)
        {
            num[x]=rand()%100+1;
            if(num[x]>big)
            {
                big=num[x];
            }
            else if(num[x]<small)
            {
                small=num[x];
            }
            if(x%8==0)
            {
                printf("\n");
            }
            sum+=num[x];
            printf("%d ",num[x]);
        }
        average=sum/20;
        printf("\nBig: %d ",big);
        printf("\nSmall: %d ",small);
        printf("\nAverage: %d ",average);
        
        return 0;
        
    }
    I've done it changed some names too but there is one small problem now. Every time I run the program the first number is 0 but I want it to be random.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Wlod
    I've done it changed some names too but there is one small problem now. Every time I run the program the first number is 0 but I want it to be random.
    This looks problematic:
    Code:
    sum=small=big=num[20]=rand()%100+1;
    num is an array of 20 elements, hence num[20] does not exist. You should have written:
    Code:
    sum = small = big = num[0] = rand() % 100 + 1;
    By the way, this could give you unexpected results:
    Code:
    average=sum/20;
    The reason is that there is integer division. You might want to declare average as a double instead, then write:
    Code:
    average = sum / 20.0;
    Of course, you must then also change the printf that prints the average.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87
    Here is a corrected version:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
     
     
    int main(void)
     
     
    {
        int small, big;
        float num[21];
        float average;
        float sum;
        srand(time(NULL));
    
    
     
     
        /* here is the loading part*/
        int i;
        for(i=1;i<=20;i++){
        num[i]=rand()%100+1;
        //printf("%f\n",num[i]);
        }
    
    
        /* initializing */
        sum=0;
        small=num[1];
        big=num[1];
    
    
        /* running the cycle */
        for(i=1;i<=20;i++)
        {
            if(num[i]>big)
            {
                big=num[i];
            }
            if(num[i]<small)
            {
                small=num[i];
            }
    
    
            sum=sum+num[i];
        }
    
        /* computing and displaying results */
        average=sum/20.0;
        printf("\nBiggest: %d ",big);
        printf("\nSmallest: %d ",small);
        printf("\nAverage: %f ",average);
         
        return 0;
         
    }
    The main mistakes in your code is that variable average has to be a float, as laserlight pointed out already.

    sum=small=big=num[20]=rand()%100+1;
    This doesn't sound good. If you want to find a minimum of an array, the way to do it is to:

    Code:
        /* initializing */
        sum=0;
        small=num[1];
        big=num[1];
    Assign to two variables the first value of the array. And then run a cycle where you compare the first (if you need the sum also) and all the following elements of the array, whether they are smaller than the already found minimum and / or bigger than the already found maximum.
    Last edited by nerio; 01-08-2016 at 05:17 AM.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    @ nerio: Why are you using an array of size 21 and starting your loops at 1? If the program requires 20 elements, you should just make the array with a size of 20 and start looping at 0.

  9. #9
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87
    @ Matticus: because I am used from Pascal that many things just starts from 1. So in C I have these old habits, since C is my first imperative language that I have learned after Pascal.

    Code:
    myCoolArray : Array[1..20] of Integer;
    Last edited by nerio; 01-08-2016 at 08:15 AM.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by nerio View Post
    @ Matticus: because I am used from Pascal that many things just starts from 1. So in C I have these old habit, since C is my first imperative language that I have learned after Pascal.
    But on another thread, you said:

    Quote Originally Posted by nerio View Post
    Another issue, an array in C starts with 0. So the for cycle should go from...
    So you are clearly aware of this fact.

    I would suggest you stick to common C idioms on the C board so as not to confuse new programmers.

  11. #11
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87
    ok, here is the corrected code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
      
      
    int main(void)
      
      
    {
        int small, big;
        float num[20];
        float average;
        float sum;
        srand(time(NULL));
     
     
      
      
        /* here is the loading part*/
        int i;
        for(i=0;i<20;i++){
        num[i]=rand()%100+1;
        //printf("%f\n",num[i]);
        }
     
     
        /* initializing */
        sum=0;
        small=num[0];
        big=num[0];
     
     
        /* running the cycle */
        for(i=0;i<20;i++)
        {
            if(num[i]>big)
            {
                big=num[i];
            }
            if(num[i]<small)
            {
                small=num[i];
            }
     
     
            sum=sum+num[i];
        }
     
        /* computing and displaying results */
        average=sum/20.0;
        printf("\nBiggest: %d ",big);
        printf("\nSmallest: %d ",small);
        printf("\nAverage: %f ",average);
          
        return 0;
          
    }
    Quote Originally Posted by Matticus View Post
    common C idioms
    I guess that there is something new I learn every day:

    https://en.wikipedia.org/wiki/Programming_idiom
    commonly used idioms
    arrays - C Idioms and little known facts - Stack Overflow
    Idioms for C programmers (COMP 40)

    Thank you Matticus.

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by nerio View Post
    I guess that there is something new I learn every day:
    That's why I enjoy spending my time here.

    Nice finds, by the way. Thanks for sharing the links.

    Quote Originally Posted by nerio View Post
    Thank you Matticus.
    My pleasure!

  13. #13
    Registered User
    Join Date
    Nov 2015
    Posts
    6
    Thanks Everyone It works now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Find smallest and largest Number of Array.
    By Kexplx in forum C Programming
    Replies: 1
    Last Post: 11-02-2015, 05:47 PM
  2. Finding the biggest number in an array.
    By LSD in forum C Programming
    Replies: 11
    Last Post: 02-02-2014, 08:59 AM
  3. Numbers, biggest from smallest
    By .C-Man. in forum C Programming
    Replies: 3
    Last Post: 10-27-2010, 02:21 PM
  4. Finding the biggest number?
    By Tarento in forum C Programming
    Replies: 2
    Last Post: 05-17-2006, 09:18 PM
  5. sorting from smallest to biggest
    By nextus in forum C++ Programming
    Replies: 0
    Last Post: 02-15-2003, 03:01 PM

Tags for this Thread