Like Tree1Likes
  • 1 Post By msh

C Simple Program help

This is a discussion on C Simple Program help within the C Programming forums, part of the General Programming Boards category; Hi, i need help with a C program I need to generate 3 random numbers between 0 and 255 and ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    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. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    So random() doesn't generate correct random numbers? How about posting that function's code as well?
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    `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]
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    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));
    }
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #5
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    No kill like overkill.
    Code:
    int find(int x, int y, int z, int(*comp)(int, int))
    {
            return comp(x, comp(y, z));
    }
    QuantumPete likes this.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    5
    - 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. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.


    EDIT: Replace read with generate.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program, simple problem
    By KAUFMANN in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 12:16 PM
  2. simple program, simple error? HELP!
    By colonelhogan44 in forum C Programming
    Replies: 4
    Last Post: 03-21-2009, 11:21 AM
  3. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  4. Simple program, not so simple problem
    By nolsen in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2008, 09:28 AM
  5. Need help with simple, simple program.
    By LightsOut06 in forum C Programming
    Replies: 5
    Last Post: 09-01-2005, 08:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21