Thread: Need help w/ program!

  1. #1
    Unregistered
    Guest

    Unhappy Need help w/ program!

    I desperately need help on program. I'm not sure on what to do. I don't even know where to begin actually. But this program I'm trying to write is to get 3 numbers from main and to call 1 function to determine the maximum and minumum numbers. No global variables. And use call-by-reference. I'd appreciate any help I can get. Even an example or something would be great. Thanks!

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Code:
    int max(int a, int b, int c) {
    int x=a;
    if (b>x) x=b;
    if (c>x) x=c;
    return x;
    }
    min works the same way. there's probably a more cryptic and smaller way to do this, but this will work.

  3. #3
    Unregistered
    Guest
    What about the part w/ the function? And how will it work? Sorry, i'm a newbie. Just trying to learn. Any more suggestions?

  4. #4
    Unregistered
    Guest
    do you need to get them as command line arguments? the function could look like this:

    void minandmax( int& min, int& max, int array[], int arraySize )
    {
    for( int i = 0; i < arraysize; i++ )
    {
    min = max = array[ 0 ];

    if( array[ i ] > max )
    {
    max = array[ i ];
    }

    if( array[ i ] < min )
    {
    min = array[ i ]
    }
    }

    at the end of this, min will be the minimum and max the maximum values. This is not the easiest (and probbaly not the best!) way to do it though

  5. #5
    Unregistered
    Guest
    sorry, missed a } at the end as well

  6. #6
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    int max(int a, int b, int c)
    {return (a>b?b:a)>c?c: (a>b?b:a);}

    Isn't it pretty?

  7. #7
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >to call 1 function to determine the maximum and minumum
    >numbers.

    I guess unregistered wanted a function which determins the maximum AND the minimum value and not two seperate functions for determining maximum and minimum.

    Code:
    int main (void)
    {
        int a, b, c, min, max;
    
        // give a, b and c a value
    
        minmax (a, b, c, &min, &max);
    
        return 1;
    }
    
    void minmax (int a, int b, int c, int *min, int *max)
    {    
        *min = a;
        if (b < *min) *min = b;
        if (c < *min) *min = c;
        
        *max = a;
        if (b > *max)*max = b;
        if (c > *max)*max = c;
    }

  8. #8
    Unregistered
    Guest
    Yes, Shiro. Thanks! I really appreciate all of your help. =)

  9. #9
    Unregistered
    Guest
    Hi! It's me again. Here's my program. It runs fine but I'm getting the wrong minimum and maximum answers. Can anyone tell me why? And why I have 2 warnings? It's probably why my answers are wrong. I tried to look up the warnings in the Help Index, but it keeps freezing on my crappy computer. Hehe.
    ------------------------------------------------------------------------------------
    warning C4700: local variable 'min' used without having been initialized
    warning C4700: local variable 'max' used without having been initialized
    ------------------------------------------------------------------------------------
    #include <iostream.h>
    #include <iomanip.h>

    void minmax_fn ();

    int main ()

    {
    int a;
    int b;
    int c;
    int min;
    int max;

    cout<<"\n Enter first number: ";
    cin>> a;
    cout<<"\n Enter second number: ";
    cin>> b;
    cout<<"\n Enter third number: ";
    cin>> c;

    cout<<"\n The minimum value is: "<< min;
    cout<<"\n The maximum value is: "<< max;

    return 0;

    }

    minmax_fn (int a, int b, int c)

    {

    int min = a;
    if (b < min)
    min = b;

    if (c < min)
    min = c;

    return min;

    int max = a;
    if (b > max)
    max = b;

    if (c > max)
    max = c;

    return max;

    }

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    95
    didn't he say he needed to use passing by refrence so shouldn't it be somthing like this

    void minmax (int a, int b, int c, int & min, int & max)

    using pointers to access the original variables is still passing by value

  11. #11
    Unregistered
    Guest
    Yeah, I went back to fix it. I just figured out why I had the warnings! durh~! But what do i set min and max equal to? I've tried the formulas.. tested it.. thought it worked.. but it didn't.

    min= ?
    max=?

  12. #12
    Unregistered
    Guest

    Unhappy

    Anyone out there? I just need the formula(s) to initialize min and max. Please help!

    cout<<"\n Enter first number: ";
    cin>> a;
    cout<<"\n Enter second number: ";
    cin>> b;
    cout<<"\n Enter third number: ";
    cin>> c;

    min= //initialization
    max= //initialization

    cout<<"\n The minimum value is: "<<min;
    cout<<"\n The maximum value is: "<<max;

  13. #13
    Unregistered
    Guest

    Talking

    Nevermind!!!!!! I got it! I feel kind dumb, but it didn't work b/c I had the formulas on one line. Durh! Should've known!! Perserverance is key!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM