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

Trying to write a program in C++.. Write a program in which you create a 2D arra

ID: 3776042 • Letter: T

Question

Trying to write a program in C++.. Write a program in which you create a 2D array of type double with 30 rows and 10 columns. Populate it with random real numbers between 10.0 and 20.0 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, ask the user to enter a search value and output the results of the linear search. The search function should return the first index that is found. Modify the search functions to allow a tolerance of +/- 1%.

I have some of the program written but stuck on populating the array with the real numbers between 10.0 and 20.0. I know how to populate it with integers but can't get the decimal places. Also stuck on finding the median value. It's not in a library so I have to create my own variable correct? Here is some of the code I have so far:

Source.cpp

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int A[30][10], B[300], count=0;
{
  for (int i=0; i<30; i++)
  {
   for (int j=0; j<10; j++)
   {
    B[count]=A[i][j]= rand()/(20.0-10.0+1)+10.0;
    count++;
   }
  }
}
{
  for (int i=0; i<30; i++)
  {
   for (int j=0; j<10; j++)
   {
    cout<<A[i][j]<<"   ";
   }
cout<<endl;
  }
}

}

Library.cpp

int linear_search (double x[], int n, double value);
{
for (int i=0; i<n; i++)
  {
   if(x[i]>=0.99*value && x[i]<=1.1*value)
   {
    return i;
   }
  }
return -1;
}

Explanation / Answer

You can use this code to generate random real numbers from range 10.0 to 20.0.

#include <iostream>
#include <cmath>
#include <cstdlib>
#define LOW 10.0
#define HIGH 20.0
using namespace std;

int main()
{
float A[30][10], B[300]; int count=0;

for (int i=0; i<30; i++)
{
for (int j=0; j<10; j++)
{
//B[count]=A[i][j]= (rand()%10)+10.0;
B[count] = A[i][j] = LOW + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(HIGH-LOW)));

count++;
}
}


for (int i=0; i<30; i++)
{
for (int j=0; j<10; j++)
{
cout<<A[i][j]<<" ";
}
cout<<endl;
}
}

In the code shown 10.0 is the lower limit and 20.0 is the upper limit. Note that RAND_MAX is the maximum value returned by the rand function.

And Yes you have to write your own Median function. It's simple.

First sort the elelemts in the Array. Say A. Now if A has odd number of element then Median will be A[n/2] , now if A has even number of elements then median would be (A[n/2]+A[n/2 - 1])/2;

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