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

Write a program in which you create a 2D array of type double with 40 rows and 5

ID: 3814638 • Letter: W

Question

Write a program in which you create a 2D array of type double with 40 rows and 5 columns. Populate it with random real numbers between 1 and 1000 and print out the array in tabular format. Next, copy the values into a 1D array and sort the array. Display the median value. Finally, search for the median value and output the results of the linear search. The search function should return the first index that is found. Modify the search function to allow a tolerance of +/- 5%.

Please use C++ programmimg.

Explanation / Answer

#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

int main()
{
double a[40][5];
srand(static_cast <unsigned>(time(0)));

for(int i=0;i<40;i++)
{
for(int j=0;j<5;j++)
{
a[i][j]=static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/1000));
}
}
for(int i=0;i<40;i++)
{
for(int j=0;j<5;j++)
{
cout<<a[i][j]<<" ";
}
cout<<" ";
}

double b[200];


for(int i=0;i<40;i++)
{
for(int j=0;j<5;j++)
{
b[i*5+j]=a[i][j];
}
}
//Sorting begins
for (int i = 0 ; i <200 ; i++)
{
for (int j = 0 ; j <200 ; j++)
{
if (b[j] <= b[j+1])
{
int t = b[j];
b[j] = b[j+1];
b[j+1] = t;
}
}
} /* sorting ends */
/* calculation of median */

int median = (b[100] + b[99])/2.0 ;

cout<<" Median is "<<median;


return 1;
}

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