C Simple Program help

Associate
Joined
18 Aug 2010
Posts
347
Location
London
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);
}
 
Last edited:
This sounds like homework, so I'll give you a nudge rather than write the code to you ;)

Change gen_rand() to return you an array of three numbers, then use qsort to sort it.

Max = array[0]
Min = array[sizeof(array)-1]

Or min/max could be the other ends of the array, depending on your comparison function.
 
Last edited:
This sounds like homework, so I'll give you a nudge rather than write the code to you ;)

Change gen_rand() to return you an array of three numbers, then use qsort to sort it.

Max = array[0]
Min = array[sizeof(array)-1]

Or min/max could be the other ends of the array, depending on your comparison function.


Using a qsort (or any sort) to find the max and min value of an array is a truly horrible idea. Finding the max/min of a set is a linear operation with a runtime time O(n), typically sorting will be O(nlogn) but could be worse.

Anyway, with only 3 numbers some conditionals is all that Xerco needs.
 
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);
}


I spot one bug, check your find_min function carefully.
Also, you can reduce the amount of conditional checks in your find min and max functions. You should be able to do it with 2 conditions (2 if statement without the second condition check after the &&)
 
Back
Top Bottom