Main Restorations Software Audio/Jukebox/MP3 Everything Else Buy/Sell/Trade
Project Announcements Monitor/Video GroovyMAME Merit/JVL Touchscreen Meet Up Retail Vendors
Driving & Racing Woodworking Software Support Forums Consoles Project Arcade Reviews
Automated Projects Artwork Frontend Support Forums Pinball Forum Discussion Old Boards
Raspberry Pi & Dev Board controls.dat Linux Miscellaneous Arcade Wiki Discussion Old Archives
Lightguns Arcade1Up Try the site in https mode Site News

Unread posts | New Replies | Recent posts | Rules | Chatroom | Wiki | File Repository | RSS | Submit news

  

Author Topic: C++ help  (Read 1127 times)

0 Members and 1 Guest are viewing this topic.

HaRuMaN

  • Supreme Solder King
  • Global Moderator
  • Trade Count: (+45)
  • Full Member
  • *****
  • Offline Offline
  • Posts: 10328
  • Last login:July 14, 2025, 02:03:34 pm
  • boom
    • Arcade Madness
C++ help
« 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?

 ???

SirPoonga

  • Puck'em Up
  • Global Moderator
  • Trade Count: (+1)
  • Full Member
  • *****
  • Offline Offline
  • Posts: 8188
  • Last login:July 17, 2025, 11:04:07 pm
  • The Bears Still Suck!
Re:C++ help
« Reply #1 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.
« Last Edit: February 04, 2004, 10:32:00 pm by SirPoonga »

HaRuMaN

  • Supreme Solder King
  • Global Moderator
  • Trade Count: (+45)
  • Full Member
  • *****
  • Offline Offline
  • Posts: 10328
  • Last login:July 14, 2025, 02:03:34 pm
  • boom
    • Arcade Madness
Re:C++ help
« Reply #2 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.  :)

ErikRuud

  • Trade Count: (+1)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 1709
  • Last login:March 05, 2021, 10:20:27 am
  • I'll build a cab for only 99.99.99!!!
    • Erik's humble video game page
Re:C++ help
« Reply #3 on: February 04, 2004, 10:59:56 pm »
Oh!!! An Engineer! ::)  

That explains a lot.  ;D






Just kidding! :D
Real Life.  Still a poor substitute for video games!       
American Laser Games Wrapper
O2em Rom Utility

PacManFan

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 858
  • Last login:December 06, 2005, 12:18:56 pm
    • Kymaera Home Page
Re:C++ help
« Reply #4 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";
}

« Last Edit: February 05, 2004, 09:33:53 am by PacManFan »
All Hail Smezznar! The Giant purple centipede of Omnicron 5. Regail him with your odiferous offerings of onion powder!

Buddabing

  • Wiki Master
  • Trade Count: (0)
  • Full Member
  • *****
  • Offline Offline
  • Posts: 1845
  • Last login:February 12, 2015, 02:51:45 pm
  • I'm a llama!
Re:C++ help
« Reply #5 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()
{
I have changed my nickname to "Cakemeister". Please do not PM the Buddabing account because I do not check it anymore.

Please read the wiki!

HaRuMaN

  • Supreme Solder King
  • Global Moderator
  • Trade Count: (+45)
  • Full Member
  • *****
  • Offline Offline
  • Posts: 10328
  • Last login:July 14, 2025, 02:03:34 pm
  • boom
    • Arcade Madness
Re:C++ help
« Reply #6 on: February 05, 2004, 04:11:28 pm »
Thanks everyone, for the help... :)

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

Back to coding...
 ;D

jakejake28

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 683
  • Last login:September 02, 2005, 07:23:54 pm
  • Thank you sir, may I have another?
Re:C++ help
« Reply #7 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....
It's all about the Pentiums

HaRuMaN

  • Supreme Solder King
  • Global Moderator
  • Trade Count: (+45)
  • Full Member
  • *****
  • Offline Offline
  • Posts: 10328
  • Last login:July 14, 2025, 02:03:34 pm
  • boom
    • Arcade Madness
Re:C++ help
« Reply #8 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.