Question
Topics Parameter Passing in Arrays Hi there , this assignement is related to forst assignment that i just posted today. would you kindly provide the
Topics
Parameter Passing in Arrays
Hi there , this assignement is related to forst assignment that i just posted today. would you kindly provide the answer. Thank you so much. sahel
General Arrays Discussion
General
An array represents a collection of data elements.
Each element is of the same data type.
All elements are stored one after the other contiguously.
Each array element is associated with an index.
The index for the first element is 0.
The index for the second element is 1.
The index for the third element is 2.
The index for the 10 th element is 9.
In general, the index for the nth element is n -1.
The indices vary from 0 to length 1 where length is the size of the array.
An array element is specified using two parts: array name and index.
Example:
x is an array of 10 elements.
Its indices vary from 0 to 9.
We can put 5 in the first element as below.
index = 0;
x [index] = 5;
We can put 5 in the 10th element as below.
index = 9;
x [index] = 5;
Since an array element is accessed via an index, we can access each element of the array using a loop. This process is called iterating or traversing the array. In the loop below, we iterate through the whole array x and store 5 in each element.
int index;
for (index=0; index < 10; index ++)
{
x [index] = 5;
}
Often for simplicity, the index variable is named i as below:
int i;
for (i=0; i < 10; i ++)
{
x [i] = 5;
}
The access to an array element is done through indexed addressing. The compiler first gets the starting address of the array. It then goes to the index variable, obtains its value, computes the address of the element using the starting address and the contents of index variable. This scheme works because array elements are always stored in consecutive locations.
Parameter Passing in Arrays Discussion
Passing an Array
double dispArray (double z [], int length)
{
cout << z[0] << endl; //display contents of first element
cout << &z[0] << endl; //display address of first element
cout << z << endl; //display address of first element
//display contents of array of length 'length'.
for (int i=0; i { cout << z[i] << endl; } } int main ( ) { double x[3] = {1.1, 2.2, 3.3}; cout << x[0] << endl; //display contents of first element cout << & x[0] << endl; //display address of first element cout << x << endl; // display address of irst element dispArray (x, 3); return 0; } In the above sample code, in the function main, we declare a double array of size 3. We initialize it with values 1.1, 2.2 and 3.3. We display x[0] or the content of first element. Then we display &x[0] or the address of first element. The symbol & stands for an address. Then we display x or array name, it will also display the address of the first element. This is because, in C, an array name is a constant (read only variable) that contains the address of the start of the array i.e. the first element of array. Above, we pass the array name x to the function dispArray along with the length of the array. Essentially, we are passing the address of the first element of the array along with the length of the array. For receiving the address of the first element of the array, the function dispArray must provide an address variable i.e. a parameter that can receive an address. A paramete of type double [] is such a parameter. A parameter of type double[] is an address variable that contains an address that points to double. Using this address variable, the called method dispArray can access the first element of the array i.e. 1.1. But using this address, how can dispArray access the other elements of the array? In C, in an array, all of its elements are of the same type (size) and are stored in memory contiguously (i.e. one after the other). By knowing the address of the first element and the type (size) of the element, the compiler can figure out the addresses of all the other elements. So, in C, for an array, only the information about the address of the first element (the array name) and the type (size) of an element is kept. From that information, the compiler figures out the addresses of all the other elements. Hence, the called function dispArray, knowing the address and type (size) of the first element of the array and the length of the array, can display the whole array as shown in the code above. In summary, if a calling function passes an array double x[3] by its name x, and the called function receives that array with a parameter, double [] z, then the called function can access the array by using its name z in exactly the same fashion that the calling function can access the array by using its name x. In other words, z[0], z[1], etc. in called function will access the same elements as x[0], x[1], etc. in the calling function. So, an array is passed by name (address) and is received by name (address) while the elements of the array stay in their place in memory. Description Redo the last assignment but using functions for inputting scores, computing average and displaying scores. These functions are described below: void inputScores (double s [], int sLen) This function receives an array s of minimum length sLen It inputs from user exactly sLen scores. It prompts the user sLen times asking "Enter a test score". It inputs the scores in array s and returns. double compAverage (double s [], int sLen) This function receives an array s filled with sLen scores It computes the average of sLen scores. It returns the average. void dispScores (double s [], int sLen) This function receives an array filled with sLen scores It displays sLen scores separated by spaces. Requirements Use an array for storing scores. Testing For turning the assignment, perform the following test runs with the data shown. Test Run1 (User input is in bold) Enter class size <1-20> 10 Enter a test score 100 Enter a test score 90 Enter a test score 80 Enter a test score 70 Enter a test score 60 Enter a test score 50 Enter a test score 65 Enter a test score 75 Enter a test score 85 Enter a test score 95 Average Score: 77 Original Scores: 100 90 80 70 60 50 65 75 85 95 Test Run2 Enter class size <1-20> 21 Class size is NOT within required range. The required range is 1 to 20. Test Run3 Enter class size <1-20> -1 Class size is NOT within required range. The required range is 1 to 20. Sample Code //function main int main( ) { double scores [20]; int classSize; double sum=0; . . double average; int i; //Input class size cout << "Enter class size<1-20>" << endl; cin << classSize; //if class size is not in allowable range, display a message and return from main //call function inputScores to input scores inputScores (scores, classSize); //call compAverage to compute average average = compAverage (scores, classSize); //display average //call displayScores to display scores dispScore (scores, classSize); return 0; }
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