Write a C++ program that will use a pointer “list_ptr” to dynamically allocate m
ID: 3767992 • Letter: W
Question
Write a C++ program that will use a pointer “list_ptr” to dynamically allocate memory for a list of integer values. The program will search for the largest and smallest values within the list and use the pointers “list_small” and “list_large” to store the locations of the smallest and largest values respectively. The program will prompt the user to enter the number of elements in the list and then ask the user to input the values one at a time using the keyboard
The dynamic memory allocated by the code (using new) should be returned to the system at the end of the program (using delete).
Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
int main(){
int * arr;
int n,x;
cout<<"Please enter the number of integers in the list:";
cin>>n;
arr=new int[n];
for(int i=0;i<n;i++){
cout<<"Enter a integer of your list:";
cin>>x;
arr[i]=x;
}
int * list_small=arr;
int * list_large=arr;
for(int i=1;i<n;i++){
if(arr[i]< *list_small){
list_small=arr+i;
}
if(arr[i]> *list_large){
list_large=arr+i;
}
}
cout<< * list_small<<endl;
cout<< * list_large<<endl;
delete [] arr;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.