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

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.
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.
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...
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])...
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.
Program in C: Write a program in C that reorders the elements in an array in...
Program in C: Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
Write a program that initializes an array of 6 random integers and then prints 4 lines...
Write a program that initializes an array of 6 random integers and then prints 4 lines of output, containing the following: 1. Only the first and last element 2. Every element at an odd index 3. Every odd element 4. All elements in reverse order
write pseudocode not c program If- else programming exercises 1.    Write a C program to find...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find maximum between two numbers. 2.    Write a C program to find maximum between three numbers. 3.    Write a C program to check whether a number is negative, positive or zero. 4.    Write a C program to check whether a number is divisible by 5 and 11 or not. 5.    Write a C program to check whether a number is even or odd. 6.    Write...
C program, please Write a program that reads a sequence of 10 integer inputs and prints...
C program, please Write a program that reads a sequence of 10 integer inputs and prints the smallest and largest of the inputs and the number of even and odd inputs. for a beginner please, you could use a while loop,if-else,
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT