In: Computer Science
C++ programming test 2, chapters 6,7,& 9 on Functions, Arrays, & Pointers
1. Create a one dimensional array, Ages, which will hold 4 ages. Each age is an int.
b. Write a for loop and print, in reverse order the 4 values stored in memory assuming that the ages in the previous question have already been entered with a space between each value. Use subscript notation.
short cnt;
c. Do the same as above, but use pointer notation.
2. Create, but do not initialize with data, one array for each of the following situations. Use an appropriate data type and name for each array:
a. holding the height of 10 students - 1 dimension - use most efficient storage size
b. holding payments per year arranged like this table to be printed in a book
(Year and 1,2,3,4,5 are NOT part of the array. Neither are 6.5%, 6.75%, 7.0%)
Year
1 2 3 4 5
6.5% 123.23 104.32 99.23 80.75 67.45
6.75% 131.24 111.54 103.23 90.55 78.95
7.0% 141.54 120.54 107.43 98.67 88.98
There will be 75 tables like this for different amounts in the book.
Create the array to hold these 75 tables.
3. Using the array name and proper subscripts, print the 120.54 from the above array considering it is the 62nd table in the array of tables.
4. Convert to pointer notation using the same variable:
data[9]
ddata[2][5]
5. Use cout to print the address at which the following array is stored in the computer memory.
short YourData[12];
a. Print the address of the first element of the array
b. Print the address of the fifth element of the array
6. a. Given the following array and variables, print it so that all the items from 1 column are printed before any items on the second column, etc. Use for loop constructs, NOT while loop. Use subscript notation.
float TestData[10][20];
int row, col;
b. Dos the same as above using pointer notation.
7. Given the following, write the code to have C++ calculate:
long Arr [100];
a. Using the array name, Arr, calculate NUMBER OF BYTES IN ONE ELEMENT:
b. Calculate the NUMBER OF BYTES IN ONE ELEMENT using the data type
(problematic for future code maintenance).
c. Calculate the total number of bytes in the entire array.
d. Calculate the number of elements in the array.
Anything relating to reference variables or arrays will be so described. Assume any others are by value
8. Write the header line for the following functions - each function is named F1
b. A function returning a reference to an int and receiving a 2 dimensional array of double with 23 columns.
c. Returning an int and receiving 3 int reference variables.
9. Write the following functions in full, including header line and body. Call each F1.
a. Receives 4 arguments, two arrays of short and two int's, one for each array, containing the count of array elements in that array. The function computes one grand total for both arrays. It then returns the grand total.
b. Receives an array of long. Returns the 2nd value stored in the array.
c. Write a function to receive a 2 dimensional array of long double with 10 columns. Also you will receive a short value representing the number of rows. The function totals all of the amounts in the array. The total of the values is returned as a long double.
d. . This function, F1, receives a pointer to an int array and a pointer to a double as arguments. It then calls the function called F2, which has the following prototype:
void F2( int *arr, double num );
F1 is going to call F2 and send the array and the double value as arguments, receiving nothing back.
Write the entire function F1 which calls F2 as its only operation.
e. Change this function to reference variables:
float * fun1( float *a, int *b, float d)
{
*a = *b * d;
return a;
}
Rewrite the entire function below:
Question 1:
a)
#include <stdio.h>
int main()
{
int ages[4]={13,23,45,60};
for(int i=3;i>=0;i--)
{
printf("%d ",ages[i]);
}
}
b)
#include <stdio.h>
int main()
{
int ages[4]={13,23,45,60};
int *start=ages;
int *end=ages+4;
for (int *i=end-1;i-ages>=0;i--)
{
printf("%d ",*i);
}
}