198k views
0 votes
Write a program that accepts an integer value called multiplier as user input. Create an array of integers with ARRAY_SIZE elements. This constant has been declared for you in main, and you should leave it in main. Set each array element to the value i*multiplier, where i is the element's index. Next create two functions, called PrintForward() and PrintBackward(), that each accept two parameters: (a) the array to print, (b) the size of the array. The PrintForward() function should print each integer in the array, beginning with index 0. The PrintBackward() function should print the array in reverse order, beginning with the last element in the array and concluding with the element at index 0. (Hint: for help passing an array as a function parameter, see zybooks section 6.23) As output, print the array once forward and once backward.

1 Answer

5 votes

Answer:

The program in C++ is as follows:

#include <iostream>

using namespace std;

void PrintForward(int myarray[], int size){

for(int i = 0; i<size;i++){ cout<<myarray[i]<<" "; }

}

void PrintBackward(int myarray[], int size){

for(int i = size-1; i>=0;i--){ cout<<myarray[i]<<" "; }

}

int main(){

const int ARRAY_SIZE = 12;

int multiplier;

cout<<"Multiplier: ";

cin>>multiplier;

int myarray [ARRAY_SIZE];

for(int i = 0; i<ARRAY_SIZE;i++){ myarray[i] = i * multiplier; }

PrintForward(myarray,ARRAY_SIZE);

PrintBackward(myarray,ARRAY_SIZE);

return 0;}

Step-by-step explanation:

The PrintForward function begins here

void PrintForward(int myarray[], int size){

This iterates through the array in ascending order and print each array element

for(int i = 0; i<size;i++){ cout<<myarray[i]<<" "; }

}

The PrintBackward function begins here

void PrintBackward(int myarray[], int size){

This iterates through the array in descending order and print each array element

for(int i = size-1; i>=0;i--){ cout<<myarray[i]<<" "; }

}

The main begins here

int main(){

This declares and initializes the array size

const int ARRAY_SIZE = 12;

This declares the multiplier as an integer

int multiplier;

This gets input for the multiplier

cout<<"Multiplier: "; cin>>multiplier;

This declares the array

int myarray [ARRAY_SIZE];

This iterates through the array and populate the array by i * multiplier

for(int i = 0; i<ARRAY_SIZE;i++){ myarray[i] = i * multiplier; }

This calls the PrintForward method

PrintForward(myarray,ARRAY_SIZE);

This calls the PrintBackward method

PrintBackward(myarray,ARRAY_SIZE);

return 0;}

User Arku
by
5.4k points