Problem Statement Requirements: Write a C++ program to that will use a pointer “
ID: 3767658 • Letter: P
Question
Problem Statement
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).
Help is greatly appreciated!
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
int i,n;
int *list_ptr, *list_small, *list_large;// pointers decleration
cout<<"enter the number of values you want to store ";
cin>>n;
list_ptr = new int[n]; // dynemycally allocating memory using new
for(i=0;i<n;i++)
{
cout<<"enter "<<(i+1)<<" element ";
cin>>list_ptr[i];// read numbers
}
list_small = list_large = list_ptr;
for(i=1;i<n;i++)
{
if(list_ptr[i] < (*list_small))
list_small = list_ptr+i;// finding small
else if(list_ptr[i] > (*list_large))
list_large = list_ptr+i;// finding large
}
cout<<"small = "<<(*list_small)<<" large = "<<(*list_large)<<endl; // print small and large
delete[] list_ptr;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.