Question
All instructions/questions are referring to one code, please anwser them all Use this source code as a guide: Code the following: a) Allocate the array
All instructions/questions are referring to one code, please anwser them all
Use this source code as a guide:
Code the following:
a) Allocate the array in main at the location indicated by the comment. Answer this question in your mind: why can you not allocate the array inside of the function fillArray()? Try playing with the location where you allocate it (main vs. inside fillArray() ) to convince yourself of how pass-by-pointer works.
b) Complete the definition of the function fillArray(), in which the function places values into the array based on the arguments passed: the pointer to the array, a count of the number of elements, and a factor f. The function should set Array[i] equal to i * f, for all valid elements of the array.
2) Convert the for loop in main to use the base + offset pointer notation. This changes element access to a[i] into *(a + i) notation.
3) If you have't already (from step (1), code the for loop in the function to use a temporary pointer that you increment, instead of the array index. In this case, you can use the passed-in parameter Array as a pointer and increment it (it doesn't matter that you'll lose the pointer inside the function). Use pointer arithmetic (i.e. Array++) to get to the "next" location in the array, and dereference the Array pointer to access the elements using (*Array).
#include #include #include using namespace std; //******* // This is the prototype. Define it below main. //***************************************** void fillArray(int *a, int count, int f); int main() { int i; int count; int *a; int b[20]; int factor; // Loop iterator // count of no. of array elements // dynamically allocated array // Const pointer array // Multiply factor cout < < "How many array elements? "; cin>>count; cout < < "Enter multiply factor: "; cin >> factor; // Statement to allocate the array goes here. } fillArray(a, count, factor); fillArray(b, count, factor + 1); for (i = 0; i < count; i++) //* cout < < a[i] < < " - " < < b[i] < < endl; return 0; // Complete this function. It is to fill the // array as specified in the requirements of the ICE. // The function should "fill" the array by making each // element be "i * f" where i is a loop variable // iterating through, and f is the third parameter // listed here. //******************** void fillArray(int *Array, int n, int f) { } ******
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started