Question

Problem 3 (25 points) Redo problem l using an array generated from the method new to dynamically allocate memory for your arr

Here is the Prompt for problem 1:

Write a C++ program that reads in a test file. The first number in the file will be an integer, and will indicate the amount of decimal numbers to follow. Once you have the read the numbers from the file into an array then compute the following properties on the set of numbers: the sum, the average (mean), the variance, the standard deviation, and the median. Don’t try to do the entire program at once. Build a little, test a little. Your program should contain 5 functions (methods). One method to compute the sum, a method to compute the average (mean), a method to compute the variance, a method to compute the standard deviation, and finally a method to compute the median. Think carefully about the parameters for each method. For example the method double Sum(double[] nums) might take an array as a parameter, and have a return type of double in order to return the sum of the array. The method double Average(double sum, int n) might have a parameter for sum, and a parameter for the number of numbers in the array, and a return type of double to return the decimal average. The method to compute the median should work regardless of whether n is even or odd, where n is the length of the array.

Example for an odd sized array: {5, 7, 8, 9, 2}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here I provided with the c++ codes of problem3 and problem4 as asked.

Code For PROBLEM 3

#include <iostream>
#include <fstream>
#include <algorithm>
#include <math.h>
using namespace std;

double arraySum(double* a, int n){
   double sum = 0;
   for(int i=0;i<n;i++){
       sum+=a[i]; //update sum
   }
   return sum;
}

double mean(double *a, int n){
   double m;
   m = arraySum(a,n)/n; // calculates the sum and divides by n
   return m;
}

double median(double *a,int n){
   double med = 0;
sort(a,a+n);
// if number of elements are even
if(n%2 == 0)
med = (a[(n-1)/2] + a[n/2])/2.0;
// if number of elements are odd
else
med = a[n/2];
  
return med;
}

double sd(double* a,int n){
   double sum = 0.0, m, standardDeviation = 0.0;

int i;
m = mean(a,n);

for(i=0; i<n; ++i)
standardDeviation += pow(a[i] - m, 2);

return sqrt(standardDeviation/n);
}

double variance(double* a,int n){
   double sum = 0.0, m, v = 0.0;

int i;
m = mean(a,n);

for(i=0; i<n; ++i)
v += pow(a[i] - m, 2);

return v/n;
}

int main() {

   int n; //Number of decimal numbers to follow

   fstream file("test.txt");
   file >> n; // similar to cin but this takes input from the file instead of console

   double* a = NULL; // Pointer to int, initialize to nothing.
   a = new double[n]; // Allocate n ints and save ptr in a.
   for (int i=0; i<n; i++) {
   file >> a[i] ; // Initialize all elements to zero.
   }

   //Remove this part of the code(below 5 lines) if you don't want to print given numbers
   printf("The given numbers are \n");
   for (int i=0; i<n; i++) {
   cout<< a[i] <<endl;   
   }
   cout<<endl;

   cout << "The mean of given numbers is " << mean(a,n) << endl;
   cout << "The median of the given numbers is " << median(a,n) << endl;
   cout << "The Standard Deviation of given numbers is " << sd(a,n) << endl;
   cout<< "The Variance of given numbers is " << variance(a,n) <<endl;

return 0;
}

CODE FOR PROBLEM 3 ENDS HERE

CODE FOR PROBLEM 4

#include <iostream>
#include <fstream>
#include <algorithm>
#include <math.h>
#include <vector>
using namespace std;

double arraySum(std::vector<double> a){
   int n = a.size();
   double sum = 0;
   for(int i=0;i<n;i++){
       sum+=a[i]; //update sum
   }
   return sum;
}

double mean(std::vector<double> a){
   int n = a.size();
   double m;
   m = arraySum(a)/n; // calculates the sum and divides by n
   return m;
}

double median(std::vector<double> a){
   int n = a.size();
   double med = 0;
sort(a.begin(),a.end());
// if number of elements are even
if(n%2 == 0)
med = (a[(n-1)/2] + a[n/2])/2.0;
// if number of elements are odd
else
med = a[n/2];
  
return med;
}

double sd(std::vector<double> a){
   int n = a.size();
   double sum = 0.0, m, standardDeviation = 0.0;

int i;
m = mean(a);

for(i=0; i<n; ++i)
standardDeviation += pow(a[i] - m, 2);

return sqrt(standardDeviation/n);
}

double variance(std::vector<double> a){
   int n = a.size();
   double sum = 0.0, m, v = 0.0;

int i;
m = mean(a);

for(i=0; i<n; ++i)
v += pow(a[i] - m, 2);

return v/n;
}

int main() {

   double k;

   fstream file("test.txt");
  
   std::vector<double> v; //declare vector
   while(file >> k){
       v.push_back(k); //while there are numbers in the file input them
   }

   //Remove this part of the code(below 5 lines) if you don't want to print given numbers
   printf("The given numbers are \n");
   vector < double> :: iterator it;
   for(it=v.begin();it!=v.end();it++){
       cout<<*it<<endl;
   }
   cout<<endl;

   cout << "The mean of given numbers is " << mean(v) << endl;
   cout << "The median of the given numbers is " << median(v) << endl;
   cout << "The Standard Deviation of given numbers is " << sd(v) << endl;
   cout<< "The Variance of given numbers is " << variance(v) <<endl;

return 0;
}

CODE FOR PROBLEM 4 ENDS HERE

The above two codes are implemented as asked, please do change the name of the file from which you are reading before you run the code. Here I also attached the screenshots of some test runs.

Output for question 3

The given numbers are 1.3423 23.3423 2.23432 3.32423 5.4542 The mean of given numbers is 7.13947 The median of the given numb

Output for question 4

The given numbers are 43.323 43.3432 23.234 23.242 23.2343 4.42332 3.23432 34.342 42.44 The mean of given numbers is 26.7574

Thanks, comment for any queries.

Add a comment
Know the answer?
Add Answer to:
Here is the Prompt for problem 1: Write a C++ program that reads in a test file. The first numbe...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that reads in two hexadecimal numbers from a file, hex.dat, and prints out...

    Write a program that reads in two hexadecimal numbers from a file, hex.dat, and prints out the sum of the two numbers in hexadecimal. (As noted in class, first do this without using a file and by reading using the cin > > command) From Wikipedia: "In mathematics and computer science, hexadecimal (also base 16, or hex) is a positional numeral system with a radix, or base, of 16. It uses sixteen distinct symbols, most often the symbols 0-9 to...

  • Write a Java program that reads a file until the end of the file is encountered....

    Write a Java program that reads a file until the end of the file is encountered. The file consists of an unknown number of students' last names and their corresponding test scores. The scores should be integer values and be within the range of 0 to 100. The first line of the file is the number of tests taken by each student. You may assume the data on the file is valid. The program should display the student's name, average...

  • Write a test program that prompt the user to enter seven numbers, stores them in an...

    Write a test program that prompt the user to enter seven numbers, stores them in an array list and do the following: 1. Displays its elements (numbers) in increasing order by invoking a method with the following header that sorts the array: public static void sort(ArrayList<Integer>list) 2. Write a method that returns and displays the sum of all numbers (elements) of the ArrayList. Using the following header: public static double sum(ArrayList<Integer>list) 3. Write a method that removes the duplicate numbers...

  • Write a program that will take input from a file of numbers of type double and...

    Write a program that will take input from a file of numbers of type double and output the average of the numbers in the file to the screen. Output the file name and average. Allow the user to process multiple files in one run. Part A use an array to hold the values read from the file modify your average function so that it receives an array as input parameter, averages values in an array and returns the average Part...

  • Write a complete C++ program that reads students names and their test scores from an input...

    Write a complete C++ program that reads students names and their test scores from an input text file. The program should output each student’s name followed by the test scores and the relevant grade in an output text file. It should also find and display on screen the highest/lowest test score and the name of the students having the highest/lowest test score, average and variance of all test scores. Student data obtained from the input text file should be stored...

  • The first programming project involves writing a program that computes the minimum, the maximum and the...

    The first programming project involves writing a program that computes the minimum, the maximum and the average weight of a collection of weights represented in pounds and ounces that are read from an input file. This program consists of two classes. The first class is the Weight class, which is specified in integer pounds and ounces stored as a double precision floating point number. It should have five public methods and two private methods: 1. A public constructor that allows...

  • Write a java program to read from a file “input4_02.txt” an integer N (0? N ?...

    Write a java program to read from a file “input4_02.txt” an integer N (0? N ? 100) and then read N double numbers from this file to an array. Write to file “output4_02.txt” this array but in the reverse order and also the sum of all elements in the array. For example File: “input4_02.txt” 5 1.30 2.22 4.00 17.60 3.14 File “output4_02.txt” 3.14 17.60 4.00 2.22 1.30 Sum = 28.26

  • Java programming: Write a program called Distribution that reads a file of double values into an...

    Java programming: Write a program called Distribution that reads a file of double values into an array and computes and prints the percentage of those numbers within 1 standard deviation (SD), within 2 SDs, and within 3 SDs of the mean. The program should be modular and should at least contain the main method and a method for computing the above percentages given the numbers, the mean, and the standard deviation. Other required statistics should be computed in their own...

  • (C++ programming) Need help with homework. Write a program that can be used to gather statistical...

    (C++ programming) Need help with homework. Write a program that can be used to gather statistical data about the number of hours per week college students play video games. The program should perform the following steps: 1). Read data from the input file into a dynamically allocated array. The first number in the input file, n, represents the number of students that were surveyed. First read this number then use it to dynamically allocate an array of n integers. On...

  • I need to Write a test program that prompts the user to enter 10 double values...

    I need to Write a test program that prompts the user to enter 10 double values into an array, calls a method to calculate the average of the numbers, and displays the average. Next, write a method that returns the lowest value in the array and display the lowest number. The program should then prompt the user to enter 10 integer values into an array, calculates the average, and displays the average. The program should have two overloaded methods to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT