I have with the help of Chegg the following C++ program however the output is wr
ID: 3676206 • Letter: I
Question
I have with the help of Chegg the following C++ program however the output is wrong according to the test run.
#include<iostream>
#include<iomanip>
#include<stdlib.h>
#include<time.h>
using namespace std;
void printLarge(int arr[], int size, int element);
void fillarray(int arr[], int size);
int main()
{
const int SIZE=20;
int arr[SIZE];
int mode;
int search;
cout<<" 1.Production mode"<<endl;
cout<<" 2.Test mode"<<endl;
cin>>mode;
if(mode==1)
{
fillarray(arr,SIZE);
cout<<"Production Mode "<<endl;
do
{
cout<<"Enter search element : ";
cin>>search;
if(search<0 || search>1000)
cout<<"Search element in a range of 0 -1000"<<endl;
}while(search<0 || search>1000);
printLarge(arr,SIZE,search);
}
else if(mode==2)
{
fillarray(arr,SIZE);
cout<<"Test Mode "<<endl;
do
{
cout<<"Enter search element : ";
cin>>search;
if(search<0 || search>20)
cout<<"Search element in a range of 0 -20"<<endl;
}while(search<0 || search>20);
printLarge(arr,SIZE,search);
}
system("pause");
return 0;
}
void printLarge(int arr[], int size, int element)
{
for(int i=0;i<20;i++)
{
if(arr[i]>element)
{
cout<<left<<"arr"<<"["<<i<<"] = "<<
left<<arr[i]<<endl;
}
}
}
void fillarray(int arr[], int size)
{
srand(time(0));
for(int i=0;i<20;i++)
{
arr[i]=rand()%1000+1;
}
}
Below is a test run I did when submitting the code on a test case. Provides what should be my input to what I have. Can someone help me to obtain the correct output as the test run below?
Description:
The input of the test case:
T 10
Your code's output:
1.Production mode
2.Test mode
sh: 1: pause: not found
The correct output of the test case:
Run in [T]est or [P]roduction: Enter a test value from: 1 to 20:
Location: 10 Value: 11
Location: 11 Value: 12
Location: 12 Value: 13
Location: 13 Value: 14
Location: 14 Value: 15
Location: 15 Value: 16
Location: 16 Value: 17
Location: 17 Value: 18
Location: 18 Value: 19
Location: 19 Value: 20
Location: 19 Value: 20
Explanation / Answer
You should first select option : 2 ; that is to run the code in Test mode.
Then give any value between 0 to 20 to search for that element in the randomly populated array. Then it will print the array with those elements in the array which are > search element. This is what I can infer from this code.