Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++: Update the program below by adding the new requirements in this task. Pleas

ID: 3729570 • Letter: C

Question

C++: Update the program below by adding the new requirements in this task. Please describe the results and add short comments to explain program.

#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

class Arr {
private:
int size;
float* value;

public:
Arr(){
}

Arr(int size) {
this->size = size;
value = new float[size];
}

void setSize(int size) {
Arr c(size);
this->size = size;
}

int getSize() {
return size;
}

void inputValue() {
srand(time(NULL));
  
for (int i = 0; i < size; i++) {
//cout << "Enter value(s): ";
//cin >> value[i];

value[i] = rand() % (100-1) + 1;
}

}

float addSum() {
float total = 0.0;

for (int i = 0; i < size; i++) {
total += value[i];
}

return total;
}

float calcAvg() {
return addSum() / size;
}

void displayValue() {
cout << "Display of value(s): " << endl;
for (int i = 0; i < size; i++) {
cout << value[i] << " ";
}
cout << endl;
}

int binarySearch() {
int i, j;

for (i = 0; i < size-1; i++) {

for (j = 0; j < size-i-1; j++) {

if (value[j] > value[j+1]) {
int temp = value[j];
value[j] = value[j+1];
value[j+1] = temp;
}
}
}
  
int x, low = 0, high = size-1;

cout << "Enter a search value: ";

cin >> x;

while (low <= high) {

int mid = low + (high - low)/2;

if (value[mid] == x)
return mid;

if (value[mid] < x)
low = mid + 1;

else
high = mid - 1;

}

return -1;
}

};

int main() {
int size = 1000;
int x;

//cout << "How many values?: ";
//cin >> size;

Arr c(size);

c.inputValue();

c.displayValue();

float sum = c.addSum();

float avg = c.calcAvg();

int index = c.binarySearch();

cout << "Sum of numbers: " << sum << endl;
cout << "Average of numbers: " << avg << endl;

if(index != -1)
cout << "Index location of search value is: " << index << endl;

return 0;

}

Task 2: Create an array that holds 1000 random integers between 1-1000. Allow the user to enter an integer to search Create and implement a Linear Search Algorithm If the number exists in the array and output the position If the search key does not exist in the Array simple output value not found. Make sure to try and simulate value in the array and the value not appearing in the array. Execute the program 3 times Describe your results Attach Photos of Source Code and Output

Explanation / Answer

#include <iostream>

#include <stdlib.h>

#include <time.h>

#include <ctime>

using namespace std;

int start_s=clock();

class Arr {

private:

int size;

float* value;

public:

Arr(){

}

Arr(int size) {

this->size = size;

value = new float[size];

}

void setSize(int size) {

Arr c(size);

this->size = size;

}

int getSize() {

return size;

}

void inputValue() {

srand(time(NULL));

  

for (int i = 0; i < size; i++) {

//cout << "Enter value(s): ";

//cin >> value[i];

value[i] = rand() % (100-1) + 1;

}

}

float addSum() {

float total = 0.0;

for (int i = 0; i < size; i++) {

total += value[i];

}

return total;

}

float calcAvg() {

return addSum() / size;

}

void displayValue() {

cout << "Display of value(s): " << endl;

for (int i = 0; i < size; i++) {

cout << value[i] << " ";

}

cout << endl;

}

int binarySearch() {

int i, j;

for (i = 0; i < size-1; i++) {

for (j = 0; j < size-i-1; j++) {

if (value[j] > value[j+1]) {

int temp = value[j];

value[j] = value[j+1];

value[j+1] = temp;

}

}

}

  

int x, low = 0, high = size-1;

cout << "Enter a search value: ";

cin >> x;

while (low <= high) {

int mid = low + (high - low)/2;

if (value[mid] == x)

return mid;

if (value[mid] < x)

low = mid + 1;

else

high = mid - 1;

}

return -1;

}

int linearsearch()

{

int i,num,c=0,pos=-1;

cout<< "enter the value to be searched : ";

cin>>num;

for(i=0; i<size-1; i++)

{

if(value[i]==num)

{

c=1;

pos=i+1;

break;

}

}

if(c==0)

cout<<"element not found";

else

return pos;

}

};

int main() {

int size = 1000;

int x;

//cout << "How many values?: ";

//cin >> size;

Arr c(size);

c.inputValue();

c.displayValue();

float sum = c.addSum();

float avg = c.calcAvg();

int index = c.binarySearch();//linearsearch()

cout << "Sum of numbers: " << sum << endl;

cout << "Average of numbers: " << avg << endl;

if(index != -1)

cout << "Index location of search value is: " << index << endl;

int stop_s=clock();

cout << "time: " << (stop_s-start_s)/double(CLOCKS_PER_SEC)*1000 << endl;

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote