Write a program that asks a user to enter the size of a dynamic array that stores a list of integers. Create the dynamic array and a loop that allows the user to enter an integer value for each array element. Loop through the array, find the largest value in the array and output it, as well as its position in the array. Delete the memory allocated to your dynamic array before exiting your program
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "\nEnter the size of the dynamic array: ";
cin >>n;
int *arr = new int(n);
int largest=arr[0];
cout << "Enter " << n << " integers" << endl;
for (int i= 0; i < n; i++) {
cout<<"\nInteger "<<i+1<<": ";
cin >> arr[i];
}
for (int i= 0; i < n; i++) {
if (arr[i]>largest){
largest=arr[i];
}
}
cout<<"\nThe largest value in the array is: "<<largest;
delete [] arr;
return 0;
}
Comments
Leave a comment