Skip to content

Instantly share code, notes, and snippets.

@kevinmungai
Created May 17, 2017 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kevinmungai/4f6b9f6a3133ea2984e3165a81a27413 to your computer and use it in GitHub Desktop.
Save kevinmungai/4f6b9f6a3133ea2984e3165a81a27413 to your computer and use it in GitHub Desktop.
Finding the maximum value in an array
#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