Write the following c++ program Write a program that does the following: Dynamic
ID: 3688239 • Letter: W
Question
Write the following c++ program
Write a program that does the following: Dynamically allocates an array of 5 integer elements Prompts the user for five integer values and stores them in the array. Prints the five integer values that were stored in the array. Prompts the user, asking by how much the array size should be expanded. Valid values are between 1 and 5. Expands the array by the requested amount (how?) Prompts the user for the remaining integers, so that the expanded array could be fully filled. Prints the contents of the entire array.Explanation / Answer
#include <iostream>
using namespace std;
void readInput(int no[],int n)
{
cout<<"Enter elements of array: ";
for (int i=0;i<n;i++)
cin>>no[i];
}
void printArray(int no[],int n)
{
cout<<"Elements of array: ";
for (int i=0;i<n;i++)
cout<<no[i]<<" ";
}
int main()
{
int *no=(int*) malloc (5),size=0;
readInput(no,5);
printArray(no,5);
cout <<endl<< "Enter by how much size the array has to be expanded? ";
cin>>size;
no=(int*) malloc (size);
readInput(no,size);
printArray(no,5+size);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.