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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.