In: Computer Science
Programming in C++
Write a program that prints the values in an array and the addresses of the array’s elements using four different techniques, as follows:
Learning Objectives
In this assignment, you will:
Requirements
Your code must use these eight functions, using these names (in addition to main):
Requirements for the printValuesNameIndex Function
Purpose: |
This function prints the values in the array. |
Parameter: |
integer array (square bracket notation) |
Algorithm: |
Print the name of the function, then print the values of the
input array with array index notation. You must use the parameter.
Do not declare any other variables except a loop control
variable. |
Return value: |
None |
Requirements for the printValuesPointerIndex Function
Purpose: |
This function prints the values in the array. |
Parameter: |
Pointer to an int array |
Algorithm: |
Print the name of the function, then print the values of the
input array with pointer index notation. You must use the
parameter. Do not declare any other variables except a loop control
variable. |
Return value: |
None |
Requirements for the printValuesNameOffset Function
Purpose: |
This function prints the values in the array. |
Parameter: |
integer array (square bracket notation) |
Algorithm: |
Print the name of the function, then print the values of the
input array with pointer offset notation using the array name. You
must use the parameter. Do not declare any other variables except a
loop control variable. |
Return value: |
None |
Requirements for the printValuesPointerOffset Function
Purpose: |
This function prints the values in the array. |
Parameter: |
Pointer to an int array |
Algorithm: |
Print the name of the function, then print the values of the
input array with pointer offset notation using the pointer. You
must use the parameter. Do not declare any other variables except a
loop control variable. |
Return value: |
None |
Requirements for the printAddressesNameIndex Function
Purpose: |
This function prints the address of each element of the array. |
Parameter: |
integer array (square bracket notation) |
Algorithm: |
Print the name of the function, then print the address of each
element of the array with array index notation. You must use the
parameter. Do not declare any other variables except a loop control
variable. Use the appropriate format control string for printing
addresses. |
Return value: |
None |
Requirements for the printAddressesPointerIndex Function
Purpose: |
This function prints the address of each element of the array. |
Parameter: |
Pointer to an int array |
Algorithm: |
Print the name of the function, then print the address of each
element of the array with pointer index notation. You must use the
parameter. Do not declare any other variables except a loop control
variable. Use the appropriate format control string for printing
addresses. |
Return value: |
None |
Requirements for the printAddressesNameOffset Function
Purpose: |
This function prints the address of each element of the array. |
Parameter: |
integer array (square bracket notation) |
Algorithm: |
Print the name of the function, then print the address of each
element of the array with pointer offset notation using the array
name. You must use the parameter. Do not declare any other
variables except a loop control variable. Use the appropriate
format control string for printing addresses. |
Return value: |
None |
Requirements for the printAddressesPointerOffset Function
Purpose: |
This function prints the address of each element of the array. |
Parameter: |
Pointer to an int array |
Algorithm: |
Print the name of the function, then print the address of each
element of the array with pointer offset notation using the
pointer. You must use the parameter. Do not declare any other
variables except a loop control variable. Use the appropriate
format control string for printing addresses. |
Return value: |
None |
Requirements for main:
Other Requirements for this Program
No other variables should be declared in main or in any function or in any function parameter list, except those explicitly noted in the instructions.
You must a symbolic constant for the array size wherever it is needed.
All output must display exactly as it appears in the sample run. Note that your actual addresses may differ.
Sample Run
Printing array values from function printValuesNameIndex
a[0] = 10
a[1] = 20
a[2] = 30
a[3] = 40
a[4] = 50
Printing array values from function printValuesPointerIndex
a[0] = 10
a[1] = 20
a[2] = 30
a[3] = 40
a[4] = 50
Printing array values from function printValuesNameOffset
a[0] = 10
a[1] = 20
a[2] = 30
a[3] = 40
a[4] = 50
Printing array values from function printValuesPointerOffset
a[0] = 10
a[1] = 20
a[2] = 30
a[3] = 40
a[4] = 50
Printing array addresses from function printAddressesNameIndex
&a[0] = 62fe00
&a[1] = 62fe04
&a[2] = 62fe08
&a[3] = 62fe0c
&a[4] = 62fe10
Printing array addresses from function printAddressesPointerIndex
&a[0] = 62fe00
&a[1] = 62fe04
&a[2] = 62fe08
&a[3] = 62fe0c
&a[4] = 62fe10
Printing array addresses from function printAddressesNameOffset
&a[0] = 62fe00
&a[1] = 62fe04
&a[2] = 62fe08
&a[3] = 62fe0c
&a[4] = 62fe10
Printing array addresses from function printAddressesPointerOffset
&a[0] = 62fe00
&a[1] = 62fe04
&a[2] = 62fe08
&a[3] = 62fe0c
&a[4] = 62fe10
C++ code for the above program specification is given below: FileName: array.cpp
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
void printValuesNameIndex(int arr[], int n) {
cout << "\nPrinting array values from function printValuesNameIndex\n";
for(int i=0; i<n; i++) {
cout << "a[" << i << "] = " << arr[i] << endl;
}
}
void printValuesPointerIndex(int* ptr, int n) {
cout << "\nPrinting array values from function printValuesPointerIndex\n";
for(int i=0; i<n; i++) {
cout << "a[" << i << "] = " << *ptr << endl;
ptr++;
}
}
void printValuesNameOffset(int arr[], int n) {
cout << "\nPrinting array values from function printValuesNameOffset\n";
for(int i=0; i<n; i++) {
cout << "a[" << i << "] = " << *(arr + i) << endl;
}
}
void printValuesPointerOffset(int* ptr, int n) {
cout << "\nPrinting array values from function printValuesPointerOffset\n";
for(int i=0; i<n; i++) {
cout << "a[" << i << "] = " << *(ptr + i) << endl;
}
}
void printAddressesNameIndex(int arr[], int n) {
cout << "\nPrinting array values from function printAddressesNameIndex\n";
for(int i=0; i<n; i++) {
cout << "&a[" << i << "] = " << &arr[i] << endl;
}
}
void printAddressesPointerIndex(int* ptr, int n) {
cout << "\nPrinting array values from function printAddressesPointerIndex\n";
for(int i=0; i<n; i++) {
cout << "&a[" << i << "] = " << ptr << endl;
ptr++;
}
}
void printAddressesNameOffset(int arr[], int n) {
cout << "\nPrinting array values from function printAddressesNameOffset\n";
for(int i=0; i<n; i++) {
cout << "&a[" << i << "] = " << arr + i << endl;
}
}
void printAddressesPointerOffset(int* ptr, int n) {
cout << "\nPrinting array values from function printAddressesPointerOffset\n";
for(int i=0; i<n; i++) {
cout << "&a[" << i << "] = " << ptr + i << endl;
}
}
int main() {
int n;
int arr[] = {10, 20, 30, 40, 50};
n = sizeof(arr)/sizeof(int);
int *ptr = arr;
printValuesNameIndex(arr, n);
printValuesPointerIndex(ptr, n);
printValuesNameOffset(arr, n);
ptr = arr;
printValuesPointerOffset(ptr, n);
printAddressesNameIndex(arr, n);
ptr = arr;
printAddressesPointerIndex(ptr, n);
printAddressesNameOffset(arr, n);
ptr = arr;
printAddressesPointerOffset(ptr, n);
return 0;
}