#include #include #include #include using namespace std; void RandomNumbers(int*
ID: 3701147 • Letter: #
Question
#include
#include
#include
#include
using namespace std;
void RandomNumbers(int*);
void ReadFile(int*);
void PrintValue(const int* [], int);
int main()
{
int number;
RandomNumbers(&number);
ReadFile(&number);
system("Pause");
return 0;
}
void RandomNumbers(int* RandomNumbers)
{
ofstream banana;
int RNG;
banana.open("beta.txt");
srand(time(0));
for (int x = 0; x < rand() % (150 - 100) + 100; x++)
{
RNG = rand() % (200 - 1) + 1;
banana << RNG << endl;
}
banana.close();
}
void ReadFile(int* ReadFile)
{
ifstream banana;
int orange;
int count = 0;
int* fruits = nullptr;
banana.open("beta.txt");
while (!banana.eof()) {
banana >> orange;
count++;
}
banana.close();
fruits = new int[count];
PrintValue(fruits[] , count);
}
void PrintValue(const int* banana[] , int size)
{
for (int i = 0; i < size; i++)
{
cout << *(banana + i) << endl;
}
}
This is the code that i currently have and i want to know how to make part C and D work.
Document1 Wrd File Insert Design Lxext References Mailings Review VirHrp Tell me what you want to do Home Cut Copy Format Painter IU- Sharee Find Replace Faste Blu-ab x,x'A-3?,A- =?,A-EL, 1Norma. No Spac Heading 1 Heading 2 Title Subtitle Subte Em Emphassitense E Strong Quote ?b Select Paragrap Ecting This HW is a practice on using pointers. Use pointer notation instead of common subscript array notation (i.e. *(xti) instead x]) Your program should A- At startup: generate integer random numbers and place them in a file. Each number on a single line The numbers should range between 1-200 and the program should generate between 100-150 numbers. This means each time the program is executed, a file is created containing between 100-150 numbers with values between 1-200. Make sure you close the file once the numbers are generated. This should be done in a separate function B- The program should then read the file and create a dynamic array that holds the numbers. The size of the array is the number of elements in the file just read. This should be in a separate function C- Print values of the array: this should be done by creating a function that accepts two arguments, a const integer pointer to the array and size printValues const int "arr, int size) D- The program should ask the user to enter a "key value" and then find all numbers that are above that key value, store them in a dynamic array and return that array. Then your program should call the printValues function passing the new array. This should be implemented in as a pointer returning from a function (i.e. the function should return a pointer to the new array) 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 Make sure you do not forget to deallocate memory after use Pae 1 uf1 2 words +137 O Type here to search 1:03 I'M 45/2018Explanation / Answer
// C++ program to work with dynamic arrays
#include <iostream>
#include <stdlib.h>
#include <time.h> /* time */
#include <fstream>
using namespace std;
// function declaration
void RandomNumbers();
int* ReadFile(int&);
void printValues(const int*, int);
int* searchNumbers(const int*,int,int&);
int main() {
int *arr =NULL; // array storing elements of file
int arrSize; // array size
RandomNumbers(); // function to populate file
arr = ReadFile(arrSize); // function to read numbers from file and return the array and update the size of array
cout<<"Initial array elements : "<<endl;
printValues(arr,arrSize); // function to print array elements
int searchSize;
int *searchResults = searchNumbers(arr,arrSize,searchSize); // function to return the array containing elements > key value
// print the new array
printValues(searchResults,searchSize);
return 0;
}
// function to generate random number and populate the file
void RandomNumbers()
{
srand(time(NULL));
ofstream fileOut;
fileOut.open("beta.txt");
int n = rand()%50 +100; // total number of random numbers generated
int num;
// loop to generate n random numbers and write to file
for(int i=0;i<n-1;i++)
{
num = rand() % 199 + 1;
fileOut<<num<<endl;
}
num = rand()%199 +1;
fileOut<<num;
fileOut.close();
}
// function to read random numbers from file to array and return the dynamic array and update size in argument
int* ReadFile(int &size)
{
ifstream fileIn;
fileIn.open("beta.txt");
int count=0;
int num;
int *arr;
if(fileIn.is_open())
{
while(!fileIn.eof())
{
fileIn>>num;
count++;
}
size=count;
fileIn.seekg (0, fileIn.beg);
arr = new int[size];
int i=0;
while(!fileIn.eof())
{
fileIn>>arr[i];
i++;
}
}else
cout<<" Unable to open file";
fileIn.close();
return arr;
}
// function to print values of the array
void printValues(const int *arr, int n )
{
for(int i=0;i<n;i++)
{
cout<<*(arr+i)<<endl;
}
}
// function to ask for a key value and return all the elements in the array greater than the key and update size of new array in searchSize
int* searchNumbers(const int *arr,int n,int &searchSize)
{
int key;
int *searchResults ;
cout<<" Enter the key value : ";
cin>>key;
int count=0;
for(int i=0;i<n;i++)
{
if(*(arr+i) > key)
count++;
}
searchResults = new int[count];
searchSize = count;
count=0;
for(int i=0;i<n;i++)
{
if(*(arr+i) > key)
{
*(searchResults+count) = *(arr+i);
count++;
}
}
cout<<"Array with numbers > "<<key<<endl;
return searchResults;
}
//end of program
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.