The use of pointers is very common when the engineer must deal with variable len
ID: 3768783 • Letter: T
Question
The use of pointers is very common when the engineer must deal with variable length data sets and with Requirements: Write a C++ program to 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). Testing and Grading Late submissions will be handled according to the policy in the course outline. What to Submit One file called binomial.cpp that contains the main function and all other necessary functions.
Explanation / Answer
C++ code :
#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
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.