0

I am trying to write a program that fills an array with 11 spots with random numbers between 1-100. It seems like the random stuff works, and the minimum works, but the maximum I am getting crazy high numbers that aren't even part of the 11 numbers that were tossed in the array.

Not quite sure what the problem is, but I am pretty sure it is something ridiculously simple I am looking past.

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main( void )
{
   int i  = 0;
   int a[11];

   int min = a[0];
   int max = a[0];

   srandom( (unsigned) time(NULL) );

   for (i=0;i<11;i++)
   {
       a[i]=random( ) % 100 ;

       printf("%d\n", a[i]);

       if (a[i] > max)
       {
           max = a[i];
       }
       else if (a[i] < min)
       {
           min = a[i];
       }
   }

   printf("Min: %d\n", min);
   printf("Max: %d\n", max);

   return ( 0 ) ;
} 

OUTPUT:

16
28
27
58
8
53
76
35
27
19
41
Min: 8
Max: 152908968

7 Answers 7

2

max is initialized to a very high value .. initialize max to 0;

int max = 0;

Here is the fixed code :

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main( void )
{
   int i  = 0;
   int a[11];

   int min = 0;
   int max = 0;

   srandom( (unsigned) time(NULL) );
   a[0]=random( ) % 100 ;
   min = a[0];

for (i=1;i<11;i++)
     {
       a[i]=random( ) % 100 ;


       printf("%d\n", a[i]);

       if (a[i] > max)
            {
          max = a[i];
            }
       if (a[i] < min)
            {
          min = a[i];
            }
    }
            printf("Min: %d\n", min);
            printf("Max: %d\n", max);

return 0;
}

Output:

Notra:Desktop Sukhvir$ ./try
82
91
33
8
60
48
60
6
59
62
60
Min: 6
Max: 91
Sign up to request clarification or add additional context in comments.

2 Comments

min and max should both be set to the same random as a[0] initially; not just min. I.e. a[0] = min = max = random() % 100;
Awesome! thank you @sukhvir, I like how you put your code in here i could compare to what i had,
2

Your problem is right here:

int a[11];

int min = a[0];
int max = a[0];

You're using a[0] to initialize min and max, before a[0] itself has been initialized.

Initialize a[0], then set min and max to its value, then proceed from there.

1 Comment

Adding to that - assignment is difficult for some learners, who often think they're defining two things to be equal from now on. You're really just copying a value from one memory slot to another. Implicit pointers/references can confuse the issue in some languages, as two variables can reference the same memory slot so changes to one affect the other, but the link can still be broken by changing the pointers. There are languages in the declarative/logic paradigm (particularly Prolog) where you can define two variables to be equal from here on - this action is called "unification".
1

First you are initializing min & max with garbage values & then you are comparing them with other elements of array?

  • First assign values to each element of the array.

  • Set min & max values to a[0] (for simplicity sake).

  • Then start comparison of max with other elements of the array.

Try the following code:

int main( void )
{
   int i  = 0;
   int a[11];

   int min;
   int max;

   srandom( (unsigned) time(NULL) );

for (i=0;i<11;i++)
       a[i]=random( ) % 100 ;

min = a[0];
max = a[0];

for (i=1;i<11;i++)
     {
       printf("%d\n", a[i]);

       if (a[i] > max)
            {
          max = a[i];
            }
       else if (a[i] < min)
            {
          min = a[i];
            }
    }
            printf("Min: %d\n", min);
            printf("Max: %d\n", max);

return ( 0 ) ;

Comments

1

Problem is your code snippet

int min = a[0];
int max = a[0];  

you are assigning a[0] to max and min before initializing elements of a. First initialize elements of array then you can assign its elements to other variables.

 int min ;
 int max ;

 srandom( (unsigned) time(NULL) );   

 min = max = a[0] = random() % 100 + 1;  

 for (i = 1; i < 11; i++) 
 { 
        a[i]=random( ) % 100  + 1; // to generate numbers between 1 to 100  
        ...

1 Comment

You can do this in one pass. Outside the loop, a[0] = min = max = random() % 100;. The only other thing that needs changing in the OP's code is starting the generator loop at 1 rather than 0. Regardless, this will still work, so +1.
1

Just for the alternative since you already got 4 correct answers.

In order to initialize your variables min and max, you can either:

  • initialize them to get the value of a[0] (after a[0] has received its value ;)).
  • initialize min to 0 and max to 100 since you know that your values can't be less than 0 or greater than 100.
  • add an extra condition in your test: if (i == 0 || a[i] > max) and start your loop from 0.

The two other alternatives I am providing allows you to deal with cases where the data are not available yet. For example, if the values come from the user or the network or whatever.

Comments

0
//Just another version...

void minArray(int v[])
{

    int m=100,len=0;   //for max, put m=0
    for(int i=0;v[i];i++)
        len++;

    for(int i=0;i<len;i++)
        if(v[i]<m)   //for max, change '<' to '>'
            m=v[i];

    printf("%d",m);
}

int main(void)
{
    int v[100]={5,2,3,4,22,5};
    minArray(v);

    return 0;
}

Comments

0

Your problem lies with the min and max. You are assigning them values which you don't know. You are filling the array with values more than 0 and less than 100. So, Why not set the values of min as 100 and max as 0. So that whenever the program finds a number greater or lesser than the min or max. The program will change their values.

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main( void )
{
   int i  = 0;
   int a[11];

   int min = 100;
   int max = 0;

   srandom( (unsigned) time(NULL) );

   for (i=0;i<11;i++)
   {
       a[i]=random( ) % 100 ;

       printf("%d\n", a[i]);

       if (a[i] > max)
       {
           max = a[i];
       }
       else if (a[i] < min)
       {
           min = a[i];
       }
   }

   printf("Min: %d\n", min);
   printf("Max: %d\n", max);

   return ( 0 ) ;
} 

Output:

gcc version 4.6.3

36
47
42
64
21
78
66
23
44
20
95
Min: 20
Max: 95

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.