I have this program that is giving me some trouble. I need to write a function t
ID: 3621731 • Letter: I
Question
I have this program that is giving me some trouble. I need to write a function that will reverse an array and print the oringal and reversed orders. The program has to ask for the numbers to be entered. I got most of the program but i can not make it take the numbers i enter. Here is what i have and any help would be apperaciated. Thanks.int*ReverseArray(int*orig, int b)
{
int a=0;
int swap;
for(a;a<--b;a++)
{
swap=orig[a];
orig[a]=orig[b];
orig[b]=swap;
}
return orig;
}
#include<iostream>
using namespace std;
int main(){
int number;
cout<<"Pick an integer ";
const int SIZE=20;
int ARRAY[SIZE]={};
int*arr=ARRAY;
cout<<"Enter the amount of the interger: ";
cin>>number;
for(int i=0;i<SIZE;i++)
{
cout<<arr[i]<<' ';
}
cout<<endl;
arr=ReverseArray(arr,SIZE);
for(int i=0;i<SIZE;i++)
{
cout<<arr[i]<<' ';
}
cout<<endl;
cin.get();
system("pause");
return 0;
}
Explanation / Answer
You have it all mostly right, just the for loop for entering data into the array. Then, just display the array before reversing it, call the reverse function which you already have working, and then display it after reversing it.
To help with testing the program, you may want to lower the array size to something smaller to make it quicker to test. Good luck!
#include<iostream>
using namespace std;
int*ReverseArray(int*orig, int b)
{
int a=0;
int swap;
for(a;a<--b;a++)
{
swap=orig[a];
orig[a]=orig[b];
orig[b]=swap;
}
return orig;
}
int main(){
int number;
cout << "Pick an integer ";
const int SIZE=20;
int ARRAY[SIZE]={};
int*arr=ARRAY;
cout << "Enter the amount of the interger: " ;
for(int i=0;i<SIZE;i++)
{
cin >> arr[i] ;
}
cout<<endl;
for(int i=0;i<SIZE;i++)
{
cout<<arr[i]<<' ';
}
cout << endl;
arr=ReverseArray(arr,SIZE);
for(int i=0;i<SIZE;i++)
{
cout<<arr[i]<<' ';
}
cout<<endl;
cin.get();
system("pause");
return 0;
}