# Thread: C Simple Program help

1. ## C Simple Program help

Hi, i need help with a C program I need to generate 3 random numbers between 0 and 255 and then find the largest and smallest of these 3 numbers.

But my output is massively over the expected output. What am I doing wrong? Also should be be using srand? Would it stay within 255?

Thanks for your help heres what iv done so far.

Code:
```void main(void)
{
int num1, num2, num3, max, min;

num1=gen_rand();
num2=gen_rand();
num3=gen_rand();

max=find_max(num1, num2, num3);
min=find_min(num1, num2, num3);

printf("Random numbers are %d, %d, and %d\n", num1, num2, num3);

printf("Largest is %d.  Smallest is %d.\n", max, min);

}

int gen_rand(void)
/* returns random number in range of 0 to 255 */
{
int n;
n=random(256);  /* n is random number in range of 0 - 255 */
return(n);
}

int find_max( int x, int y, int z)
/* returns largest number */
{
int max;
if ((x>=y) && (x>=z))
{
max = x;
}
else if ((y>=x) && (y>=z))
{
max = y;
}
else
{
max = z;
}
return(max);
}

int find_min( int x, int y, int z)
/* returns smallest number */
{
int min;
if ((x<=y) && (x<=z))
{
min = x;
}
else if ((y<=x) && (y<=z))
{
min = y;
}
else
{
min = y;
}
return(min);
}```

2. So random() doesn't generate correct random numbers? How about posting that function's code as well?

3. `random` is not a standard function. How is it implemented?

Simple way to get a random number in certain range is:
Code:
```srand(time(NULL));  // seed the random number generator; should be done only once

int ks[3];
for (int i = 0; i < 3; ++i)
ks[i] = rand() % 256;  // get random number in range [0..255]```

4. Also, personally I would leave maximum comparisons up to a function that compares two numbers and then call it recursively:

Code:
```int max (int x, int y)
{
if (x >= y)
{
return x;
}
return y;
}

int find_max (int x, int y, int z)
{
return max(x, max (y, z));
}```

5. No kill like overkill.
Code:
```int find(int x, int y, int z, int(*comp)(int, int))
{
return comp(x, comp(y, z));
}```

6. - You don't include the header file "stdio.h", which contains a declaration for printf.

- The functions find_min, find_max, and get_rand aren't declared before their use. You could either move their definitions above main or put prototypes for them above main.

- Your definition for main is incorrect. According to the C standard its return type must be int.

- The random() function isn't a standard C function. Consider using rand(), which is made available by including stdlib.h, instead. See this reference for getting random numbers within a particular range with rand().

Aside from those errors, your program should run successfully. You might consider modifying your program so that it works with an array of ints instead of three separate ints.

7. You don't even need any functions, arrays or other crazy stuff. Just three variables, the number you read, max and min (initialized to your minimum limit, maximum limit respectively) (i.e. max = -1, min = 256).

Then as you read each number change min and max appropriately.