In: Computer Science
C++ programing
Write a main function that reads a list of integers
from a user, adds to an array using dynamic memory allocation, and
then displays the array. The program also displays the the largest
element in the integer array.
Requirement: Using pointer notation.
Source Code:
Output:
Code in text format (See above images of code for indentation):
#include<iostream>
using namespace std;
/*main function*/
int main()
{
/*variables*/
int n,i,max;
/*read size from the user*/
cout<<"Enter the size of the array: ";
cin>>n;
/*declare a dynamic array*/
int *a=new int(n);
/*read elements from the user*/
cout<<"Enter elements of array:\n";
for(i=0;i<n;i++)
{
cin>>*(a+i);
}
cout<<"\nYou entered array is:\n";
max=*(a+0);
/*display array and find maximum*/
for(i=0;i<n;i++)
{
cout<<*(a+i)<<"
";
/*check for max and assign element
to max*/
if(max<*(a+i))
max=*(a+i);
}
/*print largest*/
cout<<"\nThe largest element is:
"<<max;
return 0;
}