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);
}
#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);
}
Comments
Leave a comment