Ce diaporama a bien été signalé.
Le téléchargement de votre SlideShare est en cours. ×

# Write a C++ program that includes two functions named calcavg() and va.docx

Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité

## Consultez-les par la suite

1 sur 2 Publicité

# Write a C++ program that includes two functions named calcavg() and va.docx

Write a C++ program that includes two functions named calcavg() and variance(). The calcavg() function should calculate and return the average of values stored in an array named testvals. The array should be declared in main() and include the values 89,95. 72, 83, 99, 54, 86, 75, 92, 73, 79, 75, 82, and 73. The variance() function should calculate and return the variance of the data. The variance is obtained by subtracting the average from each value in testvals, squaring the values obtained, adding them, and dividing by the number of elements in testvals. The values returned from calcavg() and variance() should be displayed by using cout statements in main().
Solution
Ans;
#include <iostream>
using namespace std;
double calcavg(int arr[], int size){
double sum = 0;
for(int i = 0; i < size; ++i){
sum += arr[i];
}
return sum / size;
}
double variance(int arr[], int size, double avg){
double var = 0;
for(int i = 0; i < size; ++i){
var += (arr[i] - avg) * (arr[i] - avg);
}
return var / size;
}
int main(){
int arr[] = {89, 95, 72, 83, 99, 54, 86, 75, 92, 73, 79, 75, 82, 73};
int size = sizeof(arr) / sizeof(arr[0]);
double avg = calcavg(arr, size);
double var = variance(arr, size, avg);
cout << \"Average: \" << avg << endl;
cout << \"Variance: \" << var << endl;
return 0;
}
.

Write a C++ program that includes two functions named calcavg() and variance(). The calcavg() function should calculate and return the average of values stored in an array named testvals. The array should be declared in main() and include the values 89,95. 72, 83, 99, 54, 86, 75, 92, 73, 79, 75, 82, and 73. The variance() function should calculate and return the variance of the data. The variance is obtained by subtracting the average from each value in testvals, squaring the values obtained, adding them, and dividing by the number of elements in testvals. The values returned from calcavg() and variance() should be displayed by using cout statements in main().
Solution
Ans;
#include <iostream>
using namespace std;
double calcavg(int arr[], int size){
double sum = 0;
for(int i = 0; i < size; ++i){
sum += arr[i];
}
return sum / size;
}
double variance(int arr[], int size, double avg){
double var = 0;
for(int i = 0; i < size; ++i){
var += (arr[i] - avg) * (arr[i] - avg);
}
return var / size;
}
int main(){
int arr[] = {89, 95, 72, 83, 99, 54, 86, 75, 92, 73, 79, 75, 82, 73};
int size = sizeof(arr) / sizeof(arr[0]);
double avg = calcavg(arr, size);
double var = variance(arr, size, avg);
cout << \"Average: \" << avg << endl;
cout << \"Variance: \" << var << endl;
return 0;
}
.

Publicité
Publicité

## Plus De Contenu Connexe

Publicité

### Write a C++ program that includes two functions named calcavg() and va.docx

1. 1. Write a C++ program that includes two functions named calcavg() and variance(). The calcavg() function should calculate and return the average of values stored in an array named testvals. The array should be declared in main() and include the values 89,95. 72, 83, 99, 54, 86, 75, 92, 73, 79, 75, 82, and 73. The variance() function should calculate and return the variance of the data. The variance is obtained by subtracting the average from each value in testvals, squaring the values obtained, adding them, and dividing by the number of elements in testvals. The values returned from calcavg() and variance() should be displayed by using cout statements in main(). Solution Ans; #include <iostream> using namespace std; double calcavg(int arr[], int size){ double sum = 0; for(int i = 0; i < size; ++i){ sum += arr[i]; } return sum / size; } double variance(int arr[], int size, double avg){ double var = 0; for(int i = 0; i < size; ++i){ var += (arr[i] - avg) * (arr[i] - avg); } return var / size; } int main(){ int arr[] = {89, 95, 72, 83, 99, 54, 86, 75, 92, 73, 79, 75, 82, 73}; int size = sizeof(arr) / sizeof(arr[0]); double avg = calcavg(arr, size);
2. 2. double var = variance(arr, size, avg); cout << "Average: " << avg << endl; cout << "Variance: " << var << endl; return 0; }