Create the logic for a program that prompts a user for 10 nu

Create the logic for a program that prompts a user for 10 numbers and stores them in an array. Pass the array to a method that reverses the order of the numbers. Display the reversed numbers in the main program.

// doing this for an intro to prog/ intro to c++ class

Solution

ReverseArray.cpp

#include <iostream>

using namespace std;
void reverseArray(int a[], int n);
void printArray(int a[], int n);

int main()
{
int a[10];
cout<<\"Enter 10 numbers: \";
for(int i=0; i<10; i++){
cin >> a[i];
}
cout<<\"Given array numbers are : \"<<endl;
printArray(a, 10);
reverseArray(a,10);
cout<<\"Reverse array numbers are : \"<<endl;
printArray(a, 10);


return 0;
}
void printArray(int a[], int n){
for(int i=0; i<n; i++){
cout<<a[i]<<\" \";
}
cout<<endl;
}
void reverseArray(int a[], int n){
int temp = 0;
for(int i=0; i<n/2; i++){
temp = a[i];
a[i] = a[n-1-i];
a[n-1-i] = temp;
}
}

Output:

sh-4.3$ g++ -std=c++11 -o main *.cpp                                                                                                                                                                                        

sh-4.3$ main                                                                                                                                                                                                                

Enter 10 numbers: 10 20 30 40 50 60 70 80 90 15                                                                                                                                                                             

Given array numbers are :                                                                                                                                                                                                   

10 20 30 40 50 60 70 80 90 15                                                                                                                                                                                               

Reverse array numbers are :                                                                                                                                                                                                 

15 90 80 70 60 50 40 30 20 10

Create the logic for a program that prompts a user for 10 numbers and stores them in an array. Pass the array to a method that reverses the order of the numbers
Create the logic for a program that prompts a user for 10 numbers and stores them in an array. Pass the array to a method that reverses the order of the numbers

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site