Answer to Question #300884 in C++ for Kelly

Question #300884

How to execute this using vector



#include <cmath>



#include <iostream>




const short int SIZE = 10;




double mean(const double x[], int size);




// Compute the deviation of double values



double deviation(const double x[], int size);




int main() {



double myarray[SIZE];




std::cout << "Enter the number: ";




for (int index = 0; index < SIZE; index++)



std::cin >> myarray[index];




std::cout << "The mean is " << mean(myarray, SIZE) << ".\n";



std::cout << "The standard deviation is " << deviation(myarray, SIZE)



<< ".\n";




return 0;



}




double mean(const double x[], int size) {




double total = 0;




for (int index = 0; index < size; index++)



total += x[index];




return total / size;



}




double deviation(const double x[], int size) {




double themean = mean(x, size);




double total = 0;




for (int index = 0; index < size; index++) {



total += pow(x[index] - themean, 2);



}




return sqrt(total / size - 1);



}




1
Expert's answer
2022-02-22T06:44:19-0500
#include <cmath>
#include <iostream>

const short int SIZE = 10;

double mean(const vector<double> x, int size);
double deviation(const vector<double> x, int size);

int main() {
  vector<double> myvec(SIZE);
  std::cout << "Enter the number: ";
  for (int index = 0; index < SIZE; index++) {
    std::cin >> myvec[index];
  }
  std::cout << "The mean is " << mean(myvec, SIZE) << ".\n";
  std::cout << "The standard deviation is " << deviation(myvec, SIZE)
            << ".\n";
  return 0;
}
double mean(const vector<double> x, int size) {
  double total = 0;
  for (int index = 0; index < size; index++) {
    total += x[index];
  }
  return total / size;
}
double deviation(const vector<double> x, int size) {
  double themean = mean(x, size);
  double total = 0;
  for (int index = 0; index < size; index++) {
    total += pow(x[index] - themean, 2);
  }
  return sqrt(total / size - 1);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog