Question

In: Computer Science

Write a test program that prompts the user to enter 10 numbers and displays the mean...

Write a test program that prompts the user to enter 10 numbers and displays the mean and deviation, as shown in the following sample run:

Your program should contain the following functions:

// Compute the mean of an array of double values
double mean(const double x[], int size)
// Compute the deviation of double values
double deviation(const double x[], int size)

Write a test program that prompts the user to enter 10 numbers and displays the mean and deviation, as shown in the following sample run:

ex:

Enter ten numbers: 1.9 2.5 3.7 2 1 6 3 4 5 2
The mean is 3.11
The standard deviation is 1.55738

So this what I have:

#include <iostream>
#include <cmath>

using namespace std;

void displayVals(int vals[], int numVals, int sum);
void getVals(int vals[], int numVals);
double average(int sum, int numVals);
double stanDev(int vals[], double mean, int numVals);

int main()
{
   int numVals=10;
   int sum = 0;
   int vals[9];

   getVals(vals, numVals);
   cout << endl;
   displayVals(vals, numVals, sum);
   cout << endl;

}

void getVals(int vals[], int numVals)
{
   int index;
   cout << "Enter ten numbers :" << "\n";
   for (index = 0; index < numVals; index++) {
       cout << index + 1;
       cin >> vals[index];
   }
}

void displayVals(int vals[], int numVals, int sum)
{
   int index;
   for (index = 0; index < numVals; index++) {
       cout << index + 1;
       cout << vals[index] << ".\n";
   }

   cout << endl;
   cout << "The mean is ";
   cout << average(vals, numVals, sum) << ".\n";

   cout << endl;
   cout << "The standard deviation is: ";
   cout << stanDev(vals, numVals, sum) << ".\n";

}

double average(int sum, int numVals)
{
   double dsum = (double)sum;
   double dnumVals = (double)numVals;
   return dsum / dnumVals;
}

double stanDev(int vals[], double mean, int numVals)
{
   double sum = 0, dVals = 0, value = 0, variance = 0;
   for (int i = 0; i < numVals; i++)
   {
       dVals = (double)vals[i];
       value = (dVals - mean)*(dVals - mean);
       sum += value;
       variance = sum / (numVals);
   }
   return sqrt(variance);
}

Solutions

Expert Solution

#include <iostream>
#include <cmath>

using namespace std;

void displayVals(double vals[], int numVals);
void getVals(double vals[], int numVals);
double average(double sum, int numVals);
double stanDev(double vals[], double mean, int numVals);

int main() {
    int numVals = 10;
    double sum = 0;
    double vals[10];

    getVals(vals, numVals);
    cout << endl;
    displayVals(vals, numVals);
    cout << endl;
}

void getVals(double vals[], int numVals) {
    int index;
    cout << "Enter ten numbers : ";
    for (index = 0; index < numVals; index++) {
        cin >> vals[index];
    }
}

void displayVals(double vals[], int numVals) {
    int index;
    double sum = 0;
    for (index = 0; index < numVals; index++) {
        cout << vals[index] << " ";
        sum += vals[index];
    }
    cout << endl;

    cout << "The mean is ";
    cout << average(sum, numVals) << ".\n";

    cout << endl;
    cout << "The standard deviation is: ";
    cout << stanDev(vals, average(sum, numVals), numVals) << ".\n";
}

double average(double sum, int numVals) {
    double dsum = sum;
    double dnumVals = (double) numVals;
    return dsum / dnumVals;
}

double stanDev(double vals[], double mean, int numVals) {
    double sum = 0, dVals = 0, value = 0, variance = 0;
    for (int i = 0; i < numVals; i++) {
        dVals = vals[i];
        value = (dVals - mean) * (dVals - mean);
        sum += value;
    }
    variance = sum / (numVals - 1);
    return sqrt(variance);
}

Related Solutions

JAVA Write a test program that prompts the user to enter a list and displays whether...
JAVA Write a test program that prompts the user to enter a list and displays whether the list is sorted or not. Here is a sample run. Note that the program first prompts the user to enter the size of the list. Using Array My output should look like this Enter the size of the list: 8 Enter the contents of the list: 10 1 5 16 61 9 11 1 The list has 8 integers 10 1 5 16...
Write a test program that prompts the user to enter a sequence of numbers ending with...
Write a test program that prompts the user to enter a sequence of numbers ending with 0, and invokes this method to return the largest number in the input. Use inheritance and polymorphism approach. in java please
write a c++ program that prompts a user to enter 10 numbers. this program should read...
write a c++ program that prompts a user to enter 10 numbers. this program should read the numbers into an array and find the smallest number in the list, the largest numbers in the list the sum of the two numbers and the average of the 10 numbers PS use file I/o and input error checking methods
Problem Description: Write a program that prompts the user to enter a directory and displays the...
Problem Description: Write a program that prompts the user to enter a directory and displays the number of the files in the directory. Analysis: (Describe the problem including input and output in your own words.) Design: (Describe the major steps for solving the problem. How do you use recursion to solve this problem.) Coding: (Copy and Paste Source Code here. Format your code using Courier 10pts) Name the public class Exercise18_29 Testing: (Describe how you test this program)
Write a C++ program that asks the user to enter in three numbers and displays the...
Write a C++ program that asks the user to enter in three numbers and displays the numbers in ascending order. If the three numbers are all the same the program should tell the user that all the numbers are equal and exits the program. Be sure to think about all the possible cases of three numbers. Be sure to test all possible paths. Sample Runs: NOTE: not all possible runs are shown below. Sample Run 1 Welcome to the order...
JAVA Write a test program that prompts the user to enter a series of integers and...
JAVA Write a test program that prompts the user to enter a series of integers and displays whether the series contains runLength consecutive same-valued elements. Your program’s main() must prompt the user to enter the input size - i.e., the number of values in the series, the number of consecutive same-valued elements to match, and the sequence of integer values to check. The return value indicates whether at least one run of runLength elements exists in values. Implement the test...
Write a program that prompts the user to enter five test scores and then print the...
Write a program that prompts the user to enter five test scores and then print the average test score to the screen. (Assume that the test scores are decimal numbers). Use the following 5 numbers:     10.5      5.5        20.0      10.0      5.5. I am having trouble understanding where the 5 values are entered in the program. The program has been provided: #include <iostream>     #include <iomanip>         using namespace std;          int main()     {         double testScore1;         double testScore2;         double testScore3;        ...
You will write a program that prompts the user to enter a 7-digit phone numbers, and...
You will write a program that prompts the user to enter a 7-digit phone numbers, and finds the 3- and 4-letter words that map to the phone number, according to the restrictions outlined earlier. A sample run: unixlab% java MapNumbers Enter name of dictionary file: words10683 Enter a test word (3 letters): cat Test word maps to 228 Enter telephone number (7 digits, no 0's or 1's, negative to quit): 2282273 Options for first 3 digits: act cat bat Options...
USE PYTHON. Write a program that prompts the user to enter 5 test scores. The program...
USE PYTHON. Write a program that prompts the user to enter 5 test scores. The program should display a letter grade for each score and the average test score. Hint: Declare local variables under main() program Prompts the user to enter 5 test scores Define a function to calculate the average score: this should accept 5 test scores as argument and return the avg Define a function to determine the letter grade: this should accept a test score as argument...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and print the average of the numbers. 2. Write a Java program that uses a for loop to print the odd numbers from 1 to 20. Print one number per line in the command line window. 3. Write a program which asks the user to input the size of potatoe fries she would like to purchase, and based on the size, it will tell her...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT