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;

}

Homework Answers

Answer #1

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;
}


Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++ 11. Write a function that will merge the contents of two sorted (ascending order) arrays of type double values, storing the result in an array out- put parameter (still in ascending order). The function shouldn’t assume that both its input parameter arrays are the same length but can assume First array 04 Second array Result array that one array doesn’t contain two copies of...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
Write a MIPS assembly program that sorts an array using bubble sort translating the C code...
Write a MIPS assembly program that sorts an array using bubble sort translating the C code int main(void) { int array[] = {10, 2, 7, 5, 15, 30, 8, 6}; // input array int arraySize = sizeof(array)/sizeof(array[0]); bool swapped = true; int j = 0; int tmp; while (swapped) { swapped = false; //Note : "j" , "arraySize - j" are optimizations to the bubble sort algorithm j++; // j= sorted elements int i=0; /* "arraySize - j" is used...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write()...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write() and read() functions with binary                                                        data, where the data is not char type.              (Typecasting is required) Fill in the blanks, then enter the code and run the program. Note:   The data is int type, so typecasting is            required in the write() and read() functions. #include <iostream> #include <fstream> using namespace std; int main() {    const int SIZE = 10;   ...
Write a program that prompts the user to input a string and outputs the string in...
Write a program that prompts the user to input a string and outputs the string in uppercase letters. (Use dynamic arrays to store the string.) my code below: /* Your code from Chapter 8, exercise 5 is below. Rewrite the following code to using dynamic arrays. */ #include <iostream> #include <cstring> #include <cctype> using namespace std; int main() { //char str[81]; //creating memory for str array of size 80 using dynamic memory allocation char *str = new char[80]; int len;...
1) Develop a C++ function that determines the average value of an array of type double...
1) Develop a C++ function that determines the average value of an array of type double elements double GetAverage(double array[], int size) The function should accept as input an array of double values The function should accept as input the number of elements in the array of double values The function should return a double value which is the array's average value 2) Develop a C++ function that determines the variance of an array of type double elements double GetVariance(double...
Assignment #4 – Student Ranking : In this assignment you are going to write a program...
Assignment #4 – Student Ranking : In this assignment you are going to write a program that ask user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’...
USING C++ Topics: Friend functions Copy constructor ----------------------------------------------------------------------------------------------------------------------------------------- Lab 3.1 Create your objects in the stack...
USING C++ Topics: Friend functions Copy constructor ----------------------------------------------------------------------------------------------------------------------------------------- Lab 3.1 Create your objects in the stack (not on the heap). Add a friend function, kilotopound, which will convert kilograms to pounds. Change your weight mutator to ask whether weight is input in kilograms or pounds. If it is kilograms, call the friend function kilotopound to convert it to pounds and return pounds. There are 2.2 pounds in one kilogram. Create an object on the stack with the following information:     ...
Lab: RectClass (constructor) Code in C++ This program creates a Rectangle object, then displays the rectangle's...
Lab: RectClass (constructor) Code in C++ This program creates a Rectangle object, then displays the rectangle's length, width, and area Define an overloaded constructor and use it when creating the Rectangle object instead of using the setters. Change this program to calculate and display the rectangle's perimeter. Example: In feet, how wide is your house? 20 In feet, how long is your house? 25 The house is 20.00 feet wide. The house is 25.00 feet long. The house has 500.00...
Hello, I feel like I am super close but I can not figure out why my...
Hello, I feel like I am super close but I can not figure out why my C++ code it not displaying the proper medium. Thank you! Here is an example of the output: Input : a[] = {1, 3, 4, 2, 6, 5, 8, 7} Output : Mean = 4.5 Median = 4.5 Code so far:   #include <iostream> using namespace std; int main() { int a[100]; int n,i,sum=0; float mean, medium; //read array size // read array cout<<"Enter array size:...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT