190k views
4 votes
Write a complete program in C++ that lets the user enter 10 integer values into an array. The program should then display the data from the first to the last, and from the last to the first, on two different lines, separated by spaces.

1 Answer

7 votes

Answer:

#include <iostream>

using namespace std;

int main()

{

int arr[] = {1, 2, 3, 4, 5, 6,7,8,9,10};

cout <<"Printing from first to last element\\"<<endl;

for (int i= 0; i < 10; i++)

{

cout <<arr[i]<<endl;

}

cout <<"Printing from last to first element\\"<<endl;

for (int i= 9; i >=0; i--)

{

cout <<arr[i]<<endl;;

}

return 0;

}

Step-by-step explanation:

We start off by creating an array of 10 integers as stated in the question. Then we use a for loop to print the values from the first element (index 0) to the last.... The logic for printing in reverse order is simple, we start from the last element (index 9 since there are ten elements) then keep subtracting one until i is equal to 0 (that is the last element)

User Brent D
by
5.6k points