In: Computer Science
Initialize and Print an Array
Write a program that accepts two integer values, called "arraySize" and "multiplier", as user input. Create an array of integers with arraySize elements. 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.
As output, print the array once forward and once backward. (C++) Code with an array, not a vector.
Code:
#include <iostream>
using namespace std;
void PrintForward(int arr[], int arraySize){
cout << "\nPrinting forwards..." << endl;
for(int i = 0; i < arraySize; i++)
cout << arr[i] << " ";
cout << endl;
}
void PrintBackward(int arr[], int arraySize){
cout << "\nPrinting backwards..." << endl;
for(int i = arraySize - 1; i >= 0; i--)
cout << arr[i] << " ";
cout << endl;
}
int main(){
int arraySize, multiplier;
cout << "Enter arraySize: ";
cin >> arraySize;
cout << "Enter Multiplier: ";
cin >> multiplier;
int arr[arraySize];
cout << "Creating array..." << endl;
for(int i = 0; i < arraySize; i++)
arr[i] = i * arraySize;
PrintForward(arr, arraySize);
PrintBackward(arr, arraySize);
}
Output: