Build Your Own Arcade Controls Forum

Main => Everything Else => Topic started by: HaRuMaN on February 04, 2004, 10:10:16 pm

Title: C++ help
Post by: HaRuMaN on February 04, 2004, 10:10:16 pm
I know this is kinda out there, but if any of you know C++ maybe you can help me.

I am taking a class on C++ and I've been working on a simple program.  You input 3 different intergers, and the program has to calculate and display the sum, product, average.  That's the really easy part.

The last part is that you have to have the program display shich interger is the largest, and which is the smallest.  

This is how I coded this program.

Quote
// Problem 1.26

#include <iostream>
#include <cmath>

using namespace std;

void main()
{
   int x1, x2, x3, sum, ave, pro;

   cout << "Input three different integers:\n";
   cin >> x1 >> x2 >> x3;

   sum = x1 + x2 + x3;
   cout << "Sum is " << sum << endl;

   ave = sum / 3;
   cout << "Average is " << ave << endl;

   pro = x1 * x2 * x3;
   cout << "Product is " << pro << endl;

   if ( x1 > x2 && x1 > x3 )
      cout << x1 << " is the largest.\n";

   if ( x2 > x1 && x2 > x3 )
      cout << x2 << " is the largest.\n";

   if ( x3 > x1 && x3 > x2 )
      cout << x3 << " is the largest.\n";

   if ( x1 < x2 && x1 < x3 )
      cout << x1 << " is the smallest.\n";

   if ( x2 < x1 && x2 < x3 )
      cout << x2 << " is the smallest.\n";

   if ( x3 < x1 && x3 < x2 )
      cout << x3 << " is the smallest.\n";
}


My question is, is there an easier way to have it determine and display which integer is largest/smallest?

 ???
Title: Re:C++ help
Post by: SirPoonga on February 04, 2004, 10:28:02 pm
You aren't into arrays yet, I take it?

Assuming you aren't, this is more of a math problem than a programming problem :)  If you were into arrays I'd just throught he values in an array and do a bubble sort.

There's many ways to do this.

1 way
Code: [Select]
int min, max = 0;

if (x1<x2)
{
   if(x1<x3)
   {
      min = x1;
      if(x2<x3)
      {
         max = x3;
      }
      else
      {
         max = x2;
      }
   }
   else
   {
      min = x3;
      max = x2;
   }
}
else   //x2, x1
{
   if(x2<x3)
   {
      min = x2;  
      if(x1<x3)
     {
         max = x3;
     }
      else
      {
         max = x1;
      }
   }
   else
   {
      min = x3;
      max = x1;
   }
}
//printout min and max



Another way it to assume and prove wrong
Code: [Select]
min = x1;
max = x3;

if(x2<x1)
{
   min = x2;
   if(x3<x2)
   {
      min = x3;
   }
}
else if (x3<x1)
{
   min = x3;
}

if(x2>x3)
{
   max = x2;
   if(x1>x2)
   {
      max = x1;
   }
}
else if(x1>x3)
{
   max = x1;
}
//print max and min



I think this is correct.
Title: Re:C++ help
Post by: HaRuMaN on February 04, 2004, 10:53:09 pm
No, haven't gotten to arrays, yet.  :)  Just started the class a few weeks ago.  "Computer Programming for Engineers."

Thanks for the help, I'll try your code and see what happens.  From what I can see here, it looks good.  :)
Title: Re:C++ help
Post by: ErikRuud on February 04, 2004, 10:59:56 pm
Oh!!! An Engineer! ::)  

That explains a lot.  ;D






Just kidding! :D
Title: Re:C++ help
Post by: PacManFan on February 05, 2004, 09:32:27 am
Here's another solution using an array with a linear serach

// Problem 1.26

#include <iostream>
#include <cmath>

using namespace std;

void main()
{
   int x[3], sum, ave, pro;
   int highnum,lownum; // high and low numbers

   cout << "Input three different integers:\n";
   cin >> x [ 0 ]  >> x[1] >> x[2];

   sum = x [ 0 ] + x[1] + x[2];
   cout << "Sum is " << sum << endl;

   ave = sum / 3;
   cout << "Average is " << ave << endl;

   pro = x [ 0 ] * x[1] * x[2];
   cout << "Product is " << pro << endl;

   highnum = x [ 0 ] ;
   lownum = x [ 0 ] ;

   for (int c=0;c<3;c++){
      if (x[ c ]  > highnum){
          highnum = x[ c ];
      }
      if (x[ c ] < lownum){
          lownum = x[ c ];
      }
   }

      cout << highnum << " is the largest.\n";
      cout << lownum << " is the smallest.\n";
}

Title: Re:C++ help
Post by: Buddabing on February 05, 2004, 11:31:42 am
I know this is kinda out there, but if any of you know C++ maybe you can help me.

I am taking a class on C++ and I've been working on a simple program.  You input 3 different intergers, and the program has to calculate and display the sum, product, average.  That's the really easy part.

The last part is that you have to have the program display shich interger is the largest, and which is the smallest.  

This is how I coded this program.

Quote
// Problem 1.26

#include <iostream>
#include <cmath>

using namespace std;

void main()
{
Title: Re:C++ help
Post by: HaRuMaN on February 05, 2004, 04:11:28 pm
Thanks everyone, for the help... :)

That was a good suggestion, Buddabing, thnx...

Back to coding...
 ;D
Title: Re:C++ help
Post by: jakejake28 on February 05, 2004, 06:58:30 pm
ahhh, i remember those q's from when i first took C++. man, my teacher made me comment everything, even a prog like that....
Title: Re:C++ help
Post by: HaRuMaN on February 05, 2004, 09:11:48 pm
Fortunately, my prof doesn't make us comment everything... :)  he just wants a few comment lines at the top naming the program and giving a short description of what it does.