Created
May 17, 2017 09:34
-
-
Save kevinmungai/4f6b9f6a3133ea2984e3165a81a27413 to your computer and use it in GitHub Desktop.
Finding the maximum value in an array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main() { | |
int i,n; | |
//float arr[100]; | |
cout << "Enter total number of elements (1 to 100)"<< endl; | |
cin >> n; | |
//creating a dynamic array | |
int *arr = new int[n]; | |
cout << endl; | |
// store numbers entered by the user | |
for (i = 0; i < n; i++) { | |
cout << "Enter number" << i + 1 << " : " << endl; | |
cin >> arr[i]; | |
} | |
//loop to store the largest interger | |
for (i = 1 ; i < n ; i++) { | |
// change to '<' or '>' to find the smallest elememt | |
if (arr[0] < arr[i]) arr[0] = arr[i]; | |
} | |
cout << "The largest element is : " << arr[0] << endl; | |
// deleting the array | |
delete [] arr; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment