Write a snippet of C++ code that does the following: Create an array of one hund
ID: 3882447 • Letter: W
Question
Write a snippet of C++ code that does the following: Create an array of one hundred thousand int elements that is allocated on the heap. Initialize the array with consecutive values, starting at zero. Free the memory associated with the array. The following C++ function is supposed to read the size of an array and each element of the array from the user, but actually has a bug. Both the size of the array and the array itself are given as parameters to the function. void readData(int size, int x[]) { cin > > size: for(int i=0: i > x[i];}} a) Describe why the code of this function is wrong using full sentences. b) Rewrite the function in a way that solves the bug.Explanation / Answer
Dear Student,
here are the answers...
-----------------------------------------------------------------------------------------------------------------------------------------
Answer No: #2
----------------------------------------------------------------------------------------------------------------------------------------
C++ Code:
----------------------------------------------------------------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int main()
{
//memory allocation for 100000 arrays
int *arr = new int(100000);
//here for loop will initialize the values starting from 0
for(int i=0;i<100000;i++)
{
arr[i] = i;
}
//freeing the memory
delete arr;
return 0;
}
------------------------------------------------------------------------------------------------------------------------------------------
Answer #3:
-----------------------------------------------------------------------------------------------------------------------------------------
Answer No-3 below i have explained you by the program..
Answer(a)- The given code is wrong because size of the array is passed in the the function as parameter so there is no need to input the size again by the user.
hence below is the complete correct program...
-----------------------------------------------------------------------------------------------------------------------------------------
//header file declration
#include<iostream>
using namespace std;
//function declration and definition
int readData(int size, int x[])
{
//for loop to enter the data
for(int i =0; i< size; i++)
{
cin>>x[i];
}
//print the data
for(int i =0; i< size; i++)
{
cout<<x[i]<<" ";
}
cout<<endl;
}
int main()
{
//data varibles
int size, i;
//input size
cin>>size;
int x[size];
//call to function
readData(size, x);
return 0;
}
Answe NO: 3.(b)....below i have re-written the function
int readData(int size, int x[])
{
//for loop to enter the data
for(int i =0; i< size; i++)
{
cin>>x[i];
}
//print the data
for(int i =0; i< sizer; i++)
{
cout<<x[i]<<" ";
}
cout<<endl;
}
int main()
{
//data varibles
int size, i;
//input size
cin>>size;
int x[size];
//call to function
readData(size, x);
return 0;
}
----------------------------------------------------------------------------------------------------------------------------------------
Kindly Check and Verify Thanks...!!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.