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);
}