1. Write a program in c++ / cpp that initializes an array of float to contain 12
ID: 3833829 • Letter: 1
Question
1. Write a program in c++ / cpp that initializes an array of float to contain 12 monthly temperatures. It should read in the 12 months from the keyboard.
2. Program should calculate/determine the minimum, maximum, and average in the array of temperatures. It is important that we do not compute/check for these values when having user input the temperatures.
3. compute the standard deviation of the array of temperatures based on the following formula:
n The number of data points The mean of the ci Each of the values of the dataExplanation / Answer
I am providing you code for part 3. Hope you will get an idea. Please let me know if you have any doubt.
#include <iostream>
#include <string>
#include<fstream>
#include <vector>
#include <math.h>
using namespace std;
vector<int> numbers;
void readarray(){
ifstream in("input.txt",ios::in);
int number; //Variable to hold each number as it is read
//Read number using the extraction (>>) operator
while (in >> number) {
//Add the number to the end of the array
numbers.push_back(number);
}
//Close the file stream
in.close();
}
double findaverage(){
double avg,sum=0;
for (int i = 0; i < numbers.size(); i++)
{
sum = sum + numbers[i];
}
avg= sum / (double)numbers.size();
return avg ;
}
double finddev(double avg){
double variance,sum1=0;
double std_deviation ;
for (int i = 0; i < numbers.size(); i++)
{
sum1 = sum1 + pow((numbers[i] - avg), 2);
}
variance = sum1 / (double)numbers.size();
std_deviation = sqrt(variance);
return std_deviation ;
}
void writeresults(double avg,double dev){
cout << "Numbers are: ";
for (int i=0; i<numbers.size(); i++) {
cout << numbers[i] << ' ';
}
cout<<"Average of the numbers: "<<avg<<endl;
cout<<"Standard deviation: "<<dev<<endl;
}
int main()
{
double avg,dev;
readarray();
avg=findaverage();
dev=finddev(avg);
writeresults(avg,dev);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.