+ 1
How to write a program of arrays that display the largest array in c++
array
5 Answers
+ 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
+ 2
thank's a lot man
+ 2
thanks ARIF
+ 1
U welcome bro ngopnado..