Question

In: Computer Science

Programming in C++ Write a program that prints the values in an array and the addresses...

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:

  1. Array index notation using array name
  2. Pointer/offset notation using array name
  3. Array index notation using a pointer
  4. Pointer/offset notation using a pointer

Learning Objectives

In this assignment, you will:

  • Use functions with array and pointer arguments
  • Use indexing and offset notations to access arrays

Requirements

Your code must use these eight functions, using these names (in addition to main):

  1. printValuesNameIndex
  2. printValuesPointerIndex
  3. printValuesNameOffset
  4. printValuesPointerOffset
  5. printAddressesNameIndex
  6. printAddressesPointerIndex
  7. printAddressesNameOffset
  8. printAddressesPointerOffset

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:

  1. Declare and initialize an integer array of size 5, using the values 10, 20, 30, 40 & 50.
  2. Call each function with the appropriate argument.

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

Solutions

Expert Solution

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;
}

Related Solutions

Programming In C Write a program that prints the values in an array and the addresses...
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: Array index notation using array name Pointer/offset notation using array name Array index notation using a pointer Pointer/offset notation using a pointer Learning Objectives In this assignment, you will: Use functions with array and pointer arguments Use indexing and offset notations to access arrays Requirements Your code must use these eight functions, using these...
In C programming: Write a program that initializes an array-of-double and then copies the contents of...
In C programming: Write a program that initializes an array-of-double and then copies the contents of the array into another arrays. To make the copy, use a function with array notation. This function takes two arguments the name of the target array and the number of elements to be copied. That is, the function calls would look like this, given the following declarations: double source[5] ={1.1, 2.2, 3.3., 4.4, 5.5}; double target1[5]; double target2[5]; copyarr(source, target1, 5);
C programming. Write a program that prompts the user to enter a 6x6 array with 0...
C programming. Write a program that prompts the user to enter a 6x6 array with 0 and 1, displays the matrix, and checks if every row and every column have the even number of 1’s.
Programming lang C++ Write a program that reads 10,000 words into an array of strings. The...
Programming lang C++ Write a program that reads 10,000 words into an array of strings. The program will then read a second file that contains an undetermined number of words and search the first array for each word. The program will then report the number of words in the second list that were found on the first list.
Write a c program that prints the final sum of all values from 1 to n...
Write a c program that prints the final sum of all values from 1 to n only when n is a positive value. The program is to print "Poor!" when the final sum is less than 70, print "Good" when the sum is between 71 and 90. or "Great!" when the sum is 91 or better.
Write a C program to Declare an integer array of size 10 with values initialized as...
Write a C program to Declare an integer array of size 10 with values initialized as follows. int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Compute each item of a new array of same size derived from the above array by: adding first item (intArray[0]) of the array with 3rd, 2nd with 4th, 3rd with 5th and so on. For the last-but-one item intArray[8], add it with first item and for the last item (intArray[9])...
Using the C Programming language, write a program that sums an array of 50 elements. Next,...
Using the C Programming language, write a program that sums an array of 50 elements. Next, optimize the code using loop unrolling. Loop unrolling is a program transformation that reduces the number of iterations for a loop by increasing the number of elements computed on each iteration. Generate a graph of performance improvement. Tip: Figure 5.17 in the textbook provides an example of a graph depicting performance improvements associated with loop unrolling. Marking:- Optimize the code for an array of...
JAVA PROGRAMMING Write a program that ask the user for a number then prints the cube...
JAVA PROGRAMMING Write a program that ask the user for a number then prints the cube of the number. The program should be called CubeNumbers. Use a value returning method with parameter, call the method myCube.
C Programming Only Write a program that declares a one-dimensional array of integers with 24 elements....
C Programming Only Write a program that declares a one-dimensional array of integers with 24 elements. Fill the array with random integers (use a loop). Neatly output each element in the one-dimensional array. Next convert your one-dimensional array of 24 elements into a two-dimensional array of 6 x 4 elements. Neatly output each element of the two-dimensional array. The values will be identical to the one-dimensional array – you’re just converting from one dimension to two.
PRACTICAL 10 C PROGRAMMING. Question 1 - Reading into a dynamic array. Write a program called...
PRACTICAL 10 C PROGRAMMING. Question 1 - Reading into a dynamic array. Write a program called temperatures01 that reads a (non-empty) sequence maximum daily temperatures. Your program should first ask for the number of temperatures to read and dynamically allocate an array just big enough to hold the number of temperatures you read. You should then read in the elements of the array using a loop. You then should print out the elements of the array in reverse order (from...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT