Syntax_Error
Syntax_Error

Reputation: 89

I am trying to display the largest and smallest number of the three values I input

It is an assignment I am working on. I'm pretty sure it is something minor that I have missed, but seeing that I am trying to work on this late in the night after studying for other classes, I believe another set of eyes can help me. I am supposed to have something like this:

please enter first value---> 5.2
please enter second value---> 1.0
please enter third value-->7.1

smallest value: 1.0
largest value: 7.1

However is get some weird numbers like 002C128F for smallest value and 002C128A for largest value. I would appreciate any feedback given. I am not asking to have my homework done. I just want some guidance seeing that I am stuck. Perhaps my if statements are wrong? Thank you for your time.

#include <iostream>

using namespace std;

double min2 (double &a, double &b,double &c)
{  
    double min=0;

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

    if (c<b)
        min=c;

    return min;
}    

double max2(double &a, double &b, double &c)
{   
    double max=0;

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

    if (c>b)
        max=c;

    return max;        
}

int main()
{ 
    double a,b,c=0;

    min2(a,b,c);
    max2(a, b, c);

    cout << "Author: Jose Soto.\n";

    cout<<"enter first value: ";
    cin>>a;
    cout<<"enter second value: ";
    cin>>b;
    cout<<"enter third value:";
    cin>>c;

    cout<<"The smallest value is: "<<min2<<endl;
    cout<<"The largest value is: "<<max2<<endl;

}

Upvotes: 0

Views: 4006

Answers (6)

Vlad from Moscow
Vlad from Moscow

Reputation: 310950

You get some weird numbers because you pass to functions min2 and max2 uninitialized variables a and b. You only initialized variable c with 0.

int main()
{ double a,b,c=0;

   min2(a,b,c);
   max2(a, b, c);

At first you have to enter values for variables a, b, c and only after that to call min2 and max2.

Also the functions theirselves are invalid. And there is no any need to declare their parameters as references.

And in these statements

cout<<"The smallest value is: "<<min2<<endl;
cout<<"The largest value is: "<<max2<<endl;

you do not call the functions. You output their addresses.

The valid code could look the following way:

#include <iostream>

using namespace std;

double min( double a, double b, double c )
{  
   double min = a;

   if ( b < min ) min = b;
   if ( c < min ) min = c;

   return min;
}

double max( double a, double b, double c )
{   
    double max = a;

    if ( max < b ) max = b;
    if ( max < c ) max = c;

    return max;
}

int main()
{ 
    double a, b, c;

    cout << "Author: Jose Soto.\n";

    cout << "enter first value: ";
        cin >> a;
    cout << "enter second value: ";
        cin >> b;
    cout << "enter third value:";
        cin >> c;

   cout << "The smallest value is: " << min( a, b, c ) << endl;
   cout << "The largest  value is: " << max( a, b, c ) << endl;

    return 0;
}

Take into account that standard C++ library has already functions std::min and std::max. For example you could call them as

std::min( { a, b, c } );
std::max( { a, b, c } );

Upvotes: 1

vvv
vvv

Reputation: 53

The following two functions:

double min2 (double &a, double &b,double &c)
double max2(double &a, double &b, double &c)

You have used & followed by the variable name.The & is used to call the function using-call by address(i.e it uses a pointer which stores the address of the parameter sent during function call. There are 2 types of function calls: 1.Call by Value 2.Call by Address Call by value only copies the value and is usually used if the changes made aren't to be reflected onto the original variable. eg:

void increment(int a)
{
a++;
cout<<a;
}
int main()
{
int x=5;
cout<<x;
increment(x);
cout<<x;
return 0;
}

Output: 5 6 5

So use the functions without &s

Upvotes: -1

Ajay
Ajay

Reputation: 6590

Try this

#include <iostream>
#include <string.h>
using namespace std;

int findMax(int a, int b, int c)
{
 if(a > b)
 {
   if(a > c)
   {
    return a; 
   }
   else
   {
     return c;
   }
 }
 if( b > c)
 {
   return b; 
 }
 else
 {
   return c;
 }
}

main()
{
  int a,b,c;

  cout<<"enter first value: ";
  cin>>a;

  cout<<"enter second value: ";
  cin>>b;

  cout<<"enter third value:";
  cin>>c;

  cout<<"\nA : "<<a<<endl;
  cout<<"\nB : "<<b<<endl;
  cout<<"\nC : "<<c<<endl;
  cout<<"\nMax : "<<findMax(a,b,c)<<endl;
}

Upvotes: 0

user3154219
user3154219

Reputation: 83

Apart from what has already been told, you also have a logical error in your functions:

if (c<b)
  min=c;

Should be

if (c<min)
  min=c;

Similarly for max.

Upvotes: 1

Luchian Grigore
Luchian Grigore

Reputation: 258568

cout<<"The smallest value is: "<<min2<<endl; prints the address of the function, it doesn't call it.

Change it to

cout<<"The smallest value is: "<<min2(a,b,c)<<endl;

Upvotes: 1

Alexander
Alexander

Reputation: 8147

The return values from your min2 and max2 functions are never used. You're printing the addresses of the functions.

Maybe you're looking for:

// ...
cout << "The smallest value is: " << min2(a, b, c) << endl;
cout << "The largest value is: " << max2(a, b, c) << endl;

You have to call the functions after you've got the user input, not before that.

Upvotes: 5

Related Questions