### can anyone help me here?

is there any chance to shorten this code? " {
largest = a;
smallest = b;
} " i will paste my codes below

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889`` `````` #include #include using namespace std; int main () { int a,b,c,d,e,f,g,h; int largest, smallest; cout<<"Enter a 1st number "; cin>>a; cout<<"Enter a 2nd number "; cin>>b; cout<<"Enter a 3rd number "; cin>>c; cout<<"Enter a 4th number "; cin>>d; cout<<"Enter a 5th number "; cin>>e; cout<<"Enter a 6th number "; cin>>f; cout<<"Enter a 7th number "; cin>>g; cout<<"Enter a 8th number "; cin>>h; if (a>b) { largest = a; smallest = b; } else { largest = b; smallest = a; } if (largest < c) { largest = c; } if (largest < d) { largest = d; } if (largest < e) { largest = e; } if (largest < f) { largest = f; } if (largest < g) { largest = g; } if (largest < h) { largest = h; } if (smallest > c) { smallest = c; } if (smallest > d) { smallest = d; } if (smallest > e) { smallest = e; } if (smallest > f) { smallest = f; } if (smallest > g) { smallest = g; } if (smallest > h) { smallest = h; } cout<<"The largest is "<
Well, you only need to use three variables.
 ``123`` `````` int a; // current number int largest; int smallest;``````

And instead of lots of similar-looking cin and cout statements, use a loop. Have you learned about loops yet? (You will need another variable to control the counting of the loop).
See tutorial:
http://www.cplusplus.com/doc/tutorial/control/
Last edited on
I guess it will be shorter one.

 ``1234567891011121314151617181920212223242526`` `````` #include #include #include using namespace std; ... vector total_sequence; int curr_num; while(cin >> curr_num) { total_sequence.push_back(curr_num); } if (total_sequence.size() == 0) return 0; sort(begin(total_sequence), end(total_sequence)); cout << "The largest " << total_sequence[total_sequence.size() - 1] << endl << "The smallest " << total_sequence[0] << endl; .... ``````
Last edited on