Question

In: Computer Science

Initialize and Print an Array Write a program that accepts two integer values, called "arraySize" and...

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.

Solutions

Expert Solution

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:


Related Solutions

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])...
Write a c program Write a function to swap two elements of an integer array. Call...
Write a c program Write a function to swap two elements of an integer array. Call the function to swap the first element, i[0] with last element i[n], second element i[1] with the last but one element i[n-1] and so on. Should handle arrays with even and odd number of elements. Call the swap function with the following arrays and print results in each case before and after swapping. i. int arr1[] = {0, 1, 2, 3, 30, 20, 10,...
Write a python function that accepts two integer values corresponding to the point (x, y). Check...
Write a python function that accepts two integer values corresponding to the point (x, y). Check whether the point is within the rectangle centered at (0, 0) with width 20 and height 15. For example, (-9, 7) is inside the rectangle and (11, 4) is outside the rectangle, as shown in the figure. Return True if the point falls within the rectangle and False otherwise
Write a program that asks the user to type in two integer values. Test these two...
Write a program that asks the user to type in two integer values. Test these two numbers to determine whether the first is evenly divisible by the second and then display the appropriate message to the terminal. Objective C
Write a Java program to initialize an array with the even integers from 2 to 20...
Write a Java program to initialize an array with the even integers from 2 to 20 and print the result then pass this array to a method in order to create a new array which is the inverse of the array you passed (print the result). You cannot use a third array. Insert comments and version control in the program to document the program.
Question 2. Write a MARIE program that accepts an integer from the user, and if it...
Question 2. Write a MARIE program that accepts an integer from the user, and if it is a prime number the program will output 1, otherwise, the program will output 0. Examples: If the user input is 17, the output would be 1 If the user input is 2, the output would be 1 If the user input is 15, the output would be 0 If the user input is -2, the output would be 0 You should write and...
program c Write a program called filesearch that accepts two command-line arguments: A string A filename...
program c Write a program called filesearch that accepts two command-line arguments: A string A filename If the user did not supply both arguments, the program should display an error message and exit. The program opens the given filename. Each line that contains the given string is displayed. Use the strstr function to search each line for the string. You may assume no line is longer than 255 characters. The matching lines are displayed to standard output (normally the screen).
• Write a C++ program that asks the user to input two integer values, then calls...
• Write a C++ program that asks the user to input two integer values, then calls a void function "swap" to swap the values for the first and second variable. • As we mentioned before, in order to swap the valors of two variables, one can use the following: temp= variable1; variable1 = variable2; variable2 = temp; • Display the two variables before you call swap and after you call that function. Comment in code would be greatly appreciated to...
Write a function called ReturnOddEntries.m that accepts as input a column or row array (vector) and...
Write a function called ReturnOddEntries.m that accepts as input a column or row array (vector) and returns only the odd index entries. Do this by first setting the even entries to 0, and then removing the 0 entries by using a logical array. The first line of your code should read function p = ReturnOddEntries(p) For example, if you run in the command window p = ReturnOddEntries([1.2 7.1 8.4 -42 100.1 7 -2 4 6]), then you should get p...
Write a C++ program that accepts a single integer value entered by user. If the value...
Write a C++ program that accepts a single integer value entered by user. If the value entered is less than one the program prints nothing. If the user enters a positive integer n. The program prints n x n box drawn with * characters. If the user enters 1 , for example the program prints *. If the user enter a 2, it prints ** ** that is , a 2x2 box of * symbols.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT