Assume you have functions available called as follows: allocate( ), which accept
ID: 3618674 • Letter: A
Question
Assume you have functions available called as follows: allocate( ), which accepts an int returns a pointer to an intwhich points to a new dynamic array of the size paasedin getData( ) which accepts a pointer to an int and an intcontaining the size of the array pointed to and which returnsnothing. it in some way or anohter populates the array witharbitrary data. write a function called totals( ) with no parameters or returnvalue. totals( ) contains a single pointer to an int as a localvariable. Assign this pointer to the result of allocate( )and then pass it into getData( ) for array sizes of 1 through5. For each of these sizes output the sum of the contents of thearray. Thus you will be calling both functions 5 times and addingtogether 1, then 2, then 3, then 4, then 5 values and outputtingthem for each of 5 separate arrays. Naturally this should be doneusing loops. Assume you have functions available called as follows: allocate( ), which accepts an int returns a pointer to an intwhich points to a new dynamic array of the size paasedin getData( ) which accepts a pointer to an int and an intcontaining the size of the array pointed to and which returnsnothing. it in some way or anohter populates the array witharbitrary data. write a function called totals( ) with no parameters or returnvalue. totals( ) contains a single pointer to an int as a localvariable. Assign this pointer to the result of allocate( )and then pass it into getData( ) for array sizes of 1 through5. For each of these sizes output the sum of the contents of thearray. Thus you will be calling both functions 5 times and addingtogether 1, then 2, then 3, then 4, then 5 values and outputtingthem for each of 5 separate arrays. Naturally this should be doneusing loops.Explanation / Answer
void getData(int* data,intsize)
{
int total = 0;
for(int i=0;i<size;i++)
{
data[i] = (int) rand()/101;//populating the data withrandom numbers
total = total + data[i];
}
cout<<" Total of Array contents "<<total;
}
void getTotals()
{
int* arrays;
for(int i=1;i<=5;i++)
{
arrays = allocate(i);
getData(arrays,i);
}
}
int main()
{
getTotals();
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.