-
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!
-
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.
-
What about the part w/ the function? And how will it work? Sorry, i'm a newbie. Just trying to learn. Any more suggestions?
-
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
-
sorry, missed a } at the end as well
-
int max(int a, int b, int c)
{return (a>b?b:a)>c?c: (a>b?b:a);}
Isn't it pretty?:D
-
>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;
}
-
Yes, Shiro. Thanks! I really appreciate all of your help. =)
-
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;
}
-
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
-
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=?
-
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;
-
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!!!