4- Print the array in reverse . Your program (via a function) should create a ne
ID: 3702780 • Letter: 4
Question
4- Print the array in reverse . Your program (via a function) should create a new copy of the array, except that the elements should be in reverse. The function should return a pointer to the new array. Then call printValues to show the values of the new array.
I need help trying to get the reverse values. can someone do it with the code provided.
#include <iostream>;
#include <fstream>;
#include <ctime>;
#include <cstdlib>;
using namespace std;
void RandomNumbers();
int* ReadFile(int&);
void PrintValue(const int*, int);
int* Keynumber(const int*, int, int&);
int main()
{
int* arr = nullptr;
int size;
RandomNumbers();
arr = ReadFile(size);
cout << "Here are the numbers from the file" << endl;
PrintValue(arr, size);
int search;
int* searchresults = nullptr;
searchresults = Keynumber(arr, size, search);
PrintValue(searchresults, search);
system("Pause");
return 0;
}
void RandomNumbers()
{
srand(time(NULL));
ofstream banana;
int RNG;
int num;
banana.open("beta.txt");
for (int x = 0; x < rand() % (150 - 100) + 100; x++)
{
RNG = rand() % (200 - 1) + 1;
banana << RNG << endl;
}
num = rand() % (200 - 1) + 1;
banana << num;
banana.close();
}
int* ReadFile(int &size)
{
ifstream banana;
banana.open("beta.txt");
int count = 0;
int orange;
int *arr;
if (banana.is_open())
{
while (!banana.eof())
{
banana >> orange;
count++;
}
size = count;
banana.seekg(0, banana.beg);
arr = new int[size];
int i = 0;
while (!banana.eof())
{
banana >> arr[i];
i++;
}
return arr;
}
else
cout << " Unable to open file";
banana.close();
}
void PrintValue(const int* banana, int size)
{
for (int i = 0; i < size; i++)
{
cout << (*(banana + i)) << " ";
}
cout << endl;
}
int* Keynumber(const int *banana, int size, int &searchSize)
{
int keynumber;
int *searchResults;
cout << " Enter the key value : ";
cin >> keynumber;
int count = 0;
for (int i = 0; i <size; i++)
{
if (*(banana + i) > keynumber)
count++;
}
searchResults = new int[count];
searchSize = count;
count = 0;
for (int i = 0; i<size; i++)
{
if (*(banana + i) > keynumber)
{
*(searchResults + count) = *(banana + i);
count++;
}
}
cout << "Array with numbers > " << keynumber << endl;
return searchResults;
}
Explanation / Answer
#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
using namespace std;
void RandomNumbers();
int* ReadFile(int&);
void PrintValue(const int*, int);
int* Keynumber(const int*, int, int&);
// return the reverse array
int* reverse(int *, int);
int main()
{
int* arr = nullptr;
int size;
RandomNumbers();
arr = ReadFile(size);
cout << "Here are the numbers from the file" << endl;
PrintValue(arr, size);
// get the reverse array
int *rev_arr = reverse(arr, size);
cout<<"Reverse array is : ";
// print the reverse array
PrintValue(rev_arr, size);
int search;
int* searchresults = nullptr;
searchresults = Keynumber(arr, size, search);
PrintValue(searchresults, search);
system("Pause");
return 0;
}
void RandomNumbers()
{
srand(time(NULL));
ofstream banana;
int RNG;
int num;
banana.open("beta.txt");
for (int x = 0; x < rand() % (150 - 100) + 100; x++)
{
RNG = rand() % (200 - 1) + 1;
banana << RNG << endl;
}
num = rand() % (200 - 1) + 1;
banana << num;
banana.close();
}
int* ReadFile(int &size)
{
ifstream banana;
banana.open("beta.txt");
int count = 0;
int orange;
int *arr;
if (banana.is_open())
{
while (!banana.eof())
{
banana >> orange;
count++;
}
size = count;
banana.seekg(0, banana.beg);
arr = new int[size];
int i = 0;
while (!banana.eof())
{
banana >> arr[i];
i++;
}
return arr;
}
else
cout << " Unable to open file";
banana.close();
}
void PrintValue(const int* banana, int size)
{
for (int i = 0; i < size; i++)
{
cout << (*(banana + i)) << " ";
}
cout << endl;
}
int* Keynumber(const int *banana, int size, int &searchSize)
{
int keynumber;
int *searchResults;
cout << " Enter the key value : ";
cin >> keynumber;
int count = 0;
for (int i = 0; i <size; i++)
{
if (*(banana + i) > keynumber)
count++;
}
searchResults = new int[count];
searchSize = count;
count = 0;
for (int i = 0; i<size; i++)
{
if (*(banana + i) > keynumber)
{
*(searchResults + count) = *(banana + i);
count++;
}
}
cout << "Array with numbers > " << keynumber << endl;
return searchResults;
}
// return the reverse array
int* reverse(int *arr, int size)
{
int *rev_arr = new int[size];
int i;
// copy content of arr to rev_ar in reverse order
for( i = 0 ; i < size ; i++ )
rev_arr[ size - i - 1 ] = arr[i];
return rev_arr;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.