Question

# Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics...

Topics

Arrays

Accessing Arrays

Description

Write a C++ program that will display a number of statistics relating to data supplied by the user. The program will ask the user to enter the number of items making up the data. It will then ask the user to enter data items one by one. It will store the data items in a double array. Then it will perform a number of statistical operations on the data. Finally, it will display a report including the following:

Original data - This is the original data as provided by the user.

Sorted data - This is the original data sorted in ascending order.

Min value – This is the smallest value in the data.

Max value – This is largest value in the data.

Mean value – This is the average of the data.

Median value – This is the median of the data

Implementation

·       Prompt the user for total number of items in the data.

·       Input the data items from the user and store them in array named data.

·       Copy the data from array data to another array named sdata.

·       Sort the array named sdata.

·       Using the sorted array, determine the min value.

·       Using the sorted array, determine the max value.

·       Using the sorted array, determine the mean (average) value of the array.

·       Using the sorted array, optionally determine the median value of the array.

·       Display the original data, sorted data and min, max, mean and median values of the data.

Testing:

For turning in the assignment, perform the following test run with the data shown.

Input Test Run

(User input is shown below in bold).

Enter data value count<1-20:

12

Enter a data value

7.2

Enter a data value

7.6

Enter a data value

5.1

Enter a data value

4.2

Enter a data value

2.8

Enter a data value

0.9

Enter a data value

0.8

Enter a data value

0.0

Enter a data value

0.4

Enter a data value

1.6

Enter a data value

3.2

Enter a data value

6.4

Output Test Run

Original data:

7.2 7.6 5.1 4.2 2.8 0.9 0.8 0.0 0.4 1.6 3.2 6.4

Sorted data:

0.0 0.4 0.8 0.9 1.6 2.8 3.2 4.2 5.1 6.4 7.2 7.6

Min value: 0.0

Max value: 7.6

Mean value: 3.35

Median value: 3.0

Submit

Copy the following in a file and submit the file.

Input/output dialog of the test runs.

All the C/C++ source code.

Sample Code

Inputting an Array

The function below inputs data into an array. The code below assumes that array to which data is to be inputted is big enough to accommodate the data. For inputting the data, call the function below and pass it the array in which data is to be inputted and a length value indicating the number of the data items to be inputted. For example, if 9 items are to be inputted in an array data of size 20 and 9 is stored in an int variable length. You will call the function input as below: (Call the input function only after inputting and validating the data item count. The function input will input data elements equal to the value of length provided.)

input (data, length);

//function input

void input (double x[], int length)

{

int i;

for (i=0; i<length; i++)

{

cout << "Enter a data value" << endl;

cin >> x[i];

}

}

Displaying an Array

The function below displays the content of an array. The code below assumes that array to be displayed maybe partially filled with data. For displaying an array, call the function below and pass it the array to be displayed and a length value indicating the length of the filled part of the array. For example, if an array sdata of size 20 contains 9 items and 9 is stored in an int variable length. You will call the function display is as below:

display (sdata, length);

//function display

void display (double x[], int length)

{

int i;

for (i=0; i<length; i++)

{

cout << x[i] << “ “;

}

cout << endl;

}

Copying an Array

The function below copies the content of one array into another array. The code below assumes that array to be copied from maybe only partially filled with data and the array to be copied to is big enough to accommodate the data copied. For copying an array into another array, call the function below and pass it the both the arrays and a length value indicating the length of the filled part of the array to be copied from. For example, if an array data of size 20 contains 9 items to be copied to array sdata and 9 is stored in a variable length. You will call the function copy is as below:

copy (data, sdata, length);

//function copy

void copy (double source[], double dest[], int length)

{

int i;

for (i=0; i<length; i++)

{

dest[i] = source[i];

}

}

Sorting an Array

Sorting an Array Using Bubble Sort

The function below sorts the content of an array in an ascending order. The code below assumes that array to be sorted maybe partially filled with data. For sorting an array, call the function below and pass it the array to be sorted and a length value indicating the length of the filled part of the array.

For sorting an array, call the function below and pass it the array you want to be sorted and a length value indicating the length of the filled part of the array. For example, if an array sdata of size 20 contains 9 items to be sorted and 9 is stored in a variable length. You will call the function sort is as below:

sort (sdata, length);

//function sort

void sort (double x [], int length)

{

int i;

double temp;

bool swapDone = true;

while (swapDone)

{

swapDone = false;

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

{

if (x[i] > x[i+1])

{

swapDone = true;

temp = x [i];

x[i] = x [i+1];

x [i+1] = temp;

}

}

}

}

Calculating Mean

Mean is the average value of a set of data items. The code below assumes that sdata is a sorted array and maybe partially filled with data. The code below computes the mean of the data contained in the filled part of the array sdata.

In code below, length is an int variable already declared that contains a value indicating the length of the filled part of the array sdata. The double variables sum and mean are used to store the sum and mean of the data contained in the filled part of array sdata.

int i;

double sum, mean;

sum = 0;

for (i=0; i<length; i++)

{

sum = sum + sdata [i];

}

mean sum/length;

Calculating Median

Median is the middle number. It has as many numbers above it as below it. For example for an odd size data, such as 2, 4, 6, 8 and 10, the number 6 is median because it has two numbers smaller than it and two numbers larger than it. For even size data such as 2, 4, 6, 7, 8 and10, two middle numbers can qualify as medians. In this case, the median is the average of the two middle numbers. So the median for the above data is 6.5 i.e. (6+7)/2.

The code below computes the median of the data in a sorted array sdata. The code below assumes that sdata is a sorted array and maybe partially filled with data. The code below computes the median of the data contained in the filled part of the array sdata.

In code below, assume that length is an int variable already declared that contains a value indicating the length of the filled part of the array sdata.

int index, indexHi, indexLo;

double median=0;

//Determine if the length is odd or even.

if ( (length %2) != 0 )

{

index = length / 2;

median = sdata [index];

}

else

{

indexHi = length / 2;

indexLo = indexHi – 1;

median = (sdata[indexLo] + sdata[indexHi] ) / 2;

}

main function

//main function

int main ( )

{

int length;

double data[20];

double sdata[20];

cout << "Enter data value count <1-20>" << endl;

cin >> length;

//call input function to input the array

input (data, length);

//call copy function to copy data array into sdata array

//call sort function to sort sdata array

//find min using array sdata

double min=sdata[0];

//find max using array sdata

double max=sdata[length -1];

//find mean using array sdata

//find median using array sdata

cout << "Original data: " << endl;

//call display function to display data array

cout << "Sorted data: " << endl;

//call display function to display sdata array

//display min value

//display max value

//display mean value

//display median value

return 0;

}

Here is the complete code of your problem

output

here is the copiable code

#include <iostream>
using namespace std;
void input (double x[], int length)
{
int i;
for (i=0; i<length; i++)
{
cout << "Enter a data value" << endl;
cin >> x[i];
}
}
//copy function defination
void copy (double source[], double dest[], int length)
{
int i;
for (i=0; i<length; i++)
{
dest[i] = source[i];
}
}
//function sort
void sort (double x [], int length)
{
int i;
double temp;
bool swapDone = true;
while (swapDone)
{
swapDone = false;
for (i=0; i<length-1; i++)
{
if (x[i] > x[i+1])
{
swapDone = true;
temp = x [i];
x[i] = x [i+1];
x[i+1] = temp;
}
}
}
}
//function display
void display (double x[], int length)
{
int i;
for (i=0;i<length;i++)
{
cout<<x[i]<<" ";
}
cout<<endl;
}
//main function
int main ( )
{
int length;
double data[20];
double sdata[20];
cout << "Enter data value count <1-20>" << endl;
cin >> length;
//call input function to input the array
input (data, length);
//call copy function to copy data array into sdata array
copy(data,sdata,length);
//call sort function to sort sdata array
sort(sdata,length);
//find min using array sdata
double min=sdata[0];
//find max using array sdata
double max=sdata[length -1];
//find mean using array sdata
int i;
double sum, mean;
sum = 0;
for (i=0; i<length; i++)
{
sum = sum + sdata [i];
}
mean=sum/length;
//find median using array sdata
int index,indexHi,indexLo;
double median=0;
//Determine if the length is odd or even.
if((length%2)!=0)
{
index=length/2;
median=sdata[index];
}
else
{
indexHi=length/2;
indexLo=indexHi-1;
median=(sdata[indexLo]+sdata[indexHi])/2;
}
cout<<"Original data: " <<endl;
//call display function to display data array
display(data,length);
cout<<"Sorted data: "<<endl;
//call display function to display sdata array
display(sdata,length);
//display min value
cout<<"min value:"<<min<<endl;
//display max value
cout<<"max value:"<<max<<endl;
//display mean value
cout<<"Mean value:"<<mean<<endl;
//display median value
cout<<"median value:"<<median<<endl;
return 0;
}