# Alternate Way

• 11-23-2003
Alternate Way
Ok this is kind of a bizzarre question but it's really bugging me.

The book I'm reading has barely covered the basics (only cout, cin, math, small if statements (not else if or anything like that), and that's about it. One of the excersizes at the end of the chapter is:

Quote:

Write a program that inputs three integers from the keyboard and prints the sum, average, product, smallest and largest of these numbers. The screen dialogue should appear as follows:

Input three different integers: 13 27 14
Sum is 54
Average is 18
Product is 4914
Smallest is 13
Largest is 27
Now since I do know a little more about programming than the chapter I am currently in I did this:
Code:

```//Outputs various facts. #include <iostream> using std::cout; using std::cin; using std::endl; int main() {   int n1, n2, n3, sum, average, product;   cout<<"Input 3 integers: ";   cin>>n1>>n2>>n3;   sum = n1+n2+n3;   average = sum/3;   product = n1*n2*n3;   cout<<"Sum is "<<sum<<"\n";   cout<<"Average is "<<average<<"\n";   cout<<"Product is "<<product<<"\n";   if ( n1 < n2 && n1 < n3 )     cout<<"Smallest is "<<n1<<"\n";   if ( n2 < n1 && n2 < n3 )     cout<<"Smallest is "<<n2<<"\n";   if ( n3 < n1 && n3 < n2 )     cout<<"Smallest is "<<n3<<"\n";   if ( n1 > n2 && n1 > n3 )     cout<<"Largest is "<<n1<<"\n";   if ( n2 > n1 && n2 > n3 )     cout<<"Largest is "<<n2<<"\n";   if ( n3 > n1 && n3 > n2 )     cout<<"Largest is "<<n3<<"\n";   return 0; }```
Thing is, he's never taught the && yet. So I'm thinking there is a way to do this without it. Is there? Please try to keep it basic. Yes, I know there are shorter ways to doing this, but I am just trying to follow my book right now. Thanks.
• 11-24-2003
bennyandthejets
Code:

```if (n1>n2) {         if (n2>n3); //n1 is greatest         else if (n3>n1); //n3 is greatest } else {         if (n3>n2); //n3 is greatest         else; //n2 is greatest }```
Or something like this. You should check for equality too.

EDIT: Here is another one using a function:
Code:

```int largest(int a,int b) {     if (a>b)         return a;     else         return b; } . . . max=largest( largest(n1,n2),n3);```
It's technically just a bunch of if statements.
• 11-24-2003
Sebastiani
It's ugly, either way:

Code:

```int a = 4, b = 3, c = 5; int largest, smallest = largest = a; if(b > largest)  largest = b; else if(b < smallest)  smallest = b;   if(c > largest)  largest = c; else if(c < smallest)  smallest = c; cout << "Max: " << largest << endl; cout << "Min: " << smallest << endl;```

The problem is primarily that it only solves *this* particular problem. You need an automated way of doing it in such a way that works with any amount of data. Naturally, arrays enable that to work so that you can simply plug them into a function like:

int largest(int array[], int length);

You may already know how to do this (as you mentioned you were ahead of your class), but if not, I would strongly advise that you give it a go.
• 11-24-2003
LOL You guys I found what I had to do. Check this out:

Code:

```//determines largest and smallest out of 5 integers #include <iostream> using std::cout; using std::cin; int main() {   int n1, n2, n3, n4, n5;   cout<<"Type some integers: ";   cin>>n1>>n2>>n3>>n4>>n5;   if ( n1 > n2 )     if ( n1 > n3 )     if ( n1 > n4 )       if ( n1 > n5 )       cout<<"The largest integer is: "<<n1<<"\n";   if ( n2 > n1 )     if ( n2 > n3 )     if ( n2 > n4 )       if ( n2 > n5 )       cout<<"The largest integer is: "<<n2<<"\n";   if ( n3 > n1 )     if ( n3 > n2 )     if ( n3 > n4 )       if ( n3 > n5 )       cout<<"The largest integer is: "<<n3<<"\n";   if ( n4 > n1 )     if ( n4 > n3 )     if ( n4 > n2 )       if ( n4 > n5 )       cout<<"The largest integer is: "<<n4<<"\n";   if ( n5 > n1 )     if ( n5 > n3 )     if ( n5 > n4 )       if ( n5 > n2 )       cout<<"The largest integer is: "<<n5<<"\n";   if ( n1 < n2 )     if ( n1 < n3 )     if ( n1 < n4 )       if ( n1 < n5 )       cout<<"The smallest integer is: "<<n1<<"\n";   if ( n2 < n1 )     if ( n2 < n3 )     if ( n2 < n4 )       if ( n2 < n5 )       cout<<"The smallest integer is: "<<n2<<"\n";   if ( n3 < n1 )     if ( n3 < n2 )     if ( n3 < n4 )       if ( n3 < n5 )       cout<<"The smallest integer is: "<<n3<<"\n";   if ( n4 < n1 )     if ( n4 < n3 )     if ( n4 < n2 )       if ( n4 < n5 )       cout<<"The smallest integer is: "<<n4<<"\n";   if ( n5 < n1 )     if ( n5 < n3 )     if ( n5 < n4 )       if ( n5 < n2 )       cout<<"The smallest integer is: "<<n5<<"\n";   return 0; }```
Boy that was one heck of a mind jogger. At one point I made an error and my compiler said "Confused by earlier errors, bailing out" LOL.
• 11-24-2003
Prelude
That's really fugly. If you're going to write crazy code, at least do it right:
Code:

```#include <iostream> using namespace std; #define max(a,b) ( (a) > (b) ? (a) : (b) ) #define min(a,b) ( (a) < (b) ? (a) : (b) ) int main() {   int n[5];   cout<<"Enter five numbers: "<<flush;   cin>> n[0] >> n[1] >> n[2] >> n[3] >> n[4];   cout<<"The largest is "<< max ( max ( n[0], n[1] ), max ( max ( n[2], n[3] ), n[4] ) ) <<endl;   cout<<"The smallest is "<< min ( min ( n[0], n[1] ), min ( min ( n[2], n[3] ), n[4] ) ) <<endl; }```
:D
• 11-24-2003
Salem
Still pretty lumpy, in a chewy crunchy kinda way

Code:

```#include <climits> int n[5]; int max = INT_MIN;  // lowest possible max for ( int i = 0 ; i < 5 ; i++ ) {     cin >> n[i];     if ( n[i] > max ) max = n[i]; }```