Can the code be in a begginer friendly way im trying to understand what i did wr
ID: 3701679 • Letter: C
Question
Can the code be in a begginer friendly way im trying to understand what i did wrong when it came to my code.
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
#include<iostream>
#include<stdlib.h>
#include<fstream>
#include<string>
#include<sstream>
#include<time.h>
using namespace std;
int k,ar[150], s;
void insert()
{
int n,num;
string str;
srand(time(NULL));
s=((rand())%(150-100+1))+100;
ofstream outfile;
outfile.open("numbers.txt");
for(int i=0;i<s;i++)
{
n=((rand())%200)+1;
outfile<<n<<" ";
}
outfile.close();
}
void retrieve()
{
ifstream infile;
infile.open("numbers.txt");
string str;
for(int i=0;i<s;i++)
{
infile>>str;
stringstream geek(str);// converting string to int
int num = 0;
geek >> num;
ar[i]=num;
}
}
void printValues(const int*arr1,int size)
{
for(int i=0;i<size;i++)
{
cout<<(*(arr1+i))<<" ";
}
cout<<" ";
}
int* keyValue(int ar[],int size)
{
int key,arr[size];
cout<<"enter key value";
cin>>key;
for(int i=0;i<size;i++)
{
if(ar[i]>key)
{
arr[k]=ar[i];
k++;
}
}
return arr;
}
int* reverse(int ar[],int size)
{
int j=0,arr[size];
for(int i=size-1;i>=0;i--)
{
arr[j]=ar[i];
j++;
}
return arr;
}
int main()
{
insert();
retrieve();
int *arr;
cout<<" ";
printValues(ar,s);
arr=keyValue(ar,s);
printValues(arr,k);
int *arr_new=reverse(arr,k);
printValues(arr_new,k);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.