+ 1

How to write a program of arrays that display the largest array in c++

array

11th Apr 2018, 2:52 PM
ngopnado
ngopnado - avatar
5 Antworten
11th Apr 2018, 3:30 PM
ARIF ANSARI
ARIF ANSARI - avatar
+ 3
You'll want to use a loop for this purpose. Have a variable that keeps track of the current largest number. So each loop through, check if the current element is greater than the stored largest number; if it is then store the new number in the largest number variable instead. By the end of the loop, it'll have stored the largest number for you. Note: You can accomplish the same thing for the smallest number by simply checking for if it's less than the current element. https://code.sololearn.com/cqGo8AuTD13J/#cpp #include <iostream> using namespace std; int main() { int myArray[] = {53, 23, 7, 96, 56, 4, 19, 36}; int largestNum = myArray[0]; int smallestNum = myArray[0]; for(int i = 0; i < sizeof(myArray)/sizeof(myArray[0]); ++i){ if(myArray[i] > largestNum){ largestNum = myArray[i]; } if(myArray[i] < smallestNum){ smallestNum = myArray[i]; } } cout << "Largest Num: " << largestNum << "\n"; cout << "Smallest Num: " << smallestNum << endl; return 0; } :::: OUTPUT :::::: Largest Num: 96 Smallest Num: 4
11th Apr 2018, 3:06 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 2
thank's a lot man
11th Apr 2018, 3:08 PM
ngopnado
ngopnado - avatar
+ 2
thanks ARIF
13th Apr 2018, 12:09 PM
ngopnado
ngopnado - avatar
+ 1
U welcome bro ngopnado..
13th Apr 2018, 12:56 PM
ARIF ANSARI
ARIF ANSARI - avatar