C++ Questions please help!! Write a program that allows user to enter a array of
ID: 3567263 • Letter: C
Question
C++ Questions please help!!
Write a program that allows user to enter a array of integer numbers and then reverses this array of integer numbers. This program works for arrays of any size by using a dynamic array to hold the numbers. The user needs to first enter the size of the array then enter the numbers to the array elements. We need a function that takes a integer array and the size as input parameters and reverses the array. The function should use two pointers, front and rear. The front pointer should initially reference the first number in the array and the rear pointer should initially reference the last number in the array.
Reverse the array by swapping the numbers refernced by front and rear, then increment front to point to the next number and decrement rear to point to the preceding number, and so on, until the entire array is reversed. Write a main program to test your function on various sizes of arrays, both even and odd length. Deallocate the dynamic array at the end of the main function.
Thank you in advance!!
Explanation / Answer
#include <iostream>
using namespace std;
void Reverse(int * a, int size) {
int * f = a;
int * e = (a+size-1);
int i = 0;
int temp;
if(size % 2 == 0) {
while (i < size/2) {
temp = *f;
*f = *e;
*e = temp;
f = f++;
e = e--;
i++;
}
}
else {
while( *f != *e) {
temp = *f;
*f = *e;
*e = temp;
f = f++;
e = e--;
}
}
}
int main() {
int size;
int* array;
cout << "Please enter a size of an array: " << endl;
cin >> size;
array = new int[size];
for( int i = 0; i < size; i++) {
cout << "Please enter a number: " << endl;
cin >> array[i];
}
cout << "Your array is:" << endl;
for( int i = 0; i < size; i++) {
cout << array[i] << " ";
}
cout << endl;
Reverse(array, size);
cout << " Your new array is:" << endl;
for( int i = 0; i < size; i++) {
cout << array[i] << " ";
}
delete [] array;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.