Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

?Write a C++ program that is going to compute the mean, variance, and standard d

ID: 3708532 • Letter: #

Question

?Write a C++ program that is going to compute the mean, variance, and standard deviation for 3 month's temperatures.

1. Your task is to use your skills involving file I/O and 2D arrays and write a program in C++ that is going to compute the mean, variance and standard deviation for all the 3 months. 2. Also you will compute the mean, variance and standard deviation for each of the individual months 3, You will write all the computed results to a file, Your results will also include the number of readings that are not present in the range. Regarding input file: The first three lines of your input file consists of the number of days for March, April and May. The next 15 rows consist of temperature recordings for the 3 months. The input file is given as input-1.txt in canvas Regarding output file: Your output file will look like this Mean(march) Variance(march) Standard Deviation(march) No.of.bad.readings(march) Mean(April) Variance(April) Standard Deviation(April) No.of.bad.readings(April) Mean(may) Variance(may) Standard Deviation(may) No.of.bad.readings(may) Mean(total) Variance(total) Standard Deviation(total) No.of.bad.readings(total)

Explanation / Answer

#include<iostream>

#include<fstream>

#include<cmath>

using namespace std;

int main()

{

    // store the number of days in each month

    int n1, n2, n3;

   

    // open file in read mode

    ifstream in("input-1.txt");

   

    // read the number of days in month

    in>>n1>>n2>>n3;

   

    // create 2D array of appropriate sizes

    int **temp = new int*[3];

   

    temp[0] = new int[n1];

    temp[1] = new int[n2];

    temp[2] = new int[n3];

   

    int i;

   

    // store the number of bad readings in each month

    int bad1 = 0, bad2 = 0, bad3 = 0;

   

    // read temperature for month 1

    for( i = 0 ; i < n1 ; i++ )

    {

        in>>temp[0][i];

       

        // if the reading is a bad reading

        if( temp[0][i] < 0 )

            bad1++;

    }

       

    // read temperature for month 2

    for( i = 0 ; i < n2 ; i++ )

    {

        in>>temp[1][i];

       

        // if the reading is a bad reading

        if( temp[1][i] < 0 )

            bad2++;

    }

       

    // read temperature for month 3

    for( i = 0 ; i < n3 ; i++ )

    {

        in>>temp[2][i];

       

        // if the reading is a bad reading

        if( temp[2][i] < 0 )

            bad2++;

    }

   

    // store the sum of readings of each month

    int sum1 = 0, sum2 = 0, sum3 = 0;

   

    // compute sum of temperature for month 1

    for( i = 0 ; i < n1 ; i++ )

        sum1 += temp[0][i];

       

    // compute sum of temperature for month 2

    for( i = 0 ; i < n2 ; i++ )

        sum2 += temp[1][i];

       

    // compute sum of temperature for month 3

    for( i = 0 ; i < n3 ; i++ )

        sum3 += temp[2][i];

       

    // calculate mean for month 1

    double mean1 = (double)sum1 / (double)n1;

   

    // calculate mean for month 2

    double mean2 = (double)sum2 / (double)n2;

   

    // calculate mean for month 3

    double mean3 = (double)sum3 / (double)n3;

   

    double sd1 = 0.0;

    double sd2 = 0.0;

    double sd3 = 0.0;

   

    for( i = 0 ; i < n1 ; i++ )

        sd1 += pow( temp[0][i] - mean1 , 2 );

       

    sd1 = sqrt( sd1 / (double)n1 );

       

    for( i = 0 ; i < n2 ; i++ )

        sd2 += pow( temp[1][i] - mean2 , 2 );

       

    sd2 = sqrt( sd2 / (double)n2 );

   

    for( i = 0 ; i < n3 ; i++ )

        sd3 += pow( temp[2][i] - mean3 , 2 );

       

    sd3 = sqrt( sd3 / (double)n3 );

       

    double variance1 = pow( sd1 , 2 );

    double variance2 = pow( sd2 , 2 );

    double variance3 = pow( sd3 , 2 );

   

    // open file to write

    ofstream out("output.txt");

   

    out<<"Mean(March) : "<<mean1<<" Variance(March) : "<<variance1<<" Standard Deviation(March) : "<<sd1<<" No of bad readings(March) : "<<bad1<<endl;

    out<<"Mean(April) : "<<mean2<<" Variance(April) : "<<variance2<<" Standard Deviation(April) : "<<sd2<<" No of bad readings(April) : "<<bad2<<endl;

    out<<"Mean(May) : "<<mean3<<" Variance(May) : "<<variance3<<" Standard Deviation(May) : "<<sd3<<" No of bad readings(May) : "<<bad3<<endl;

   

    return 0;

}

--------------------------input-1.txt-----------------------------

31
30
31
13 3 26 22 50 -2 25
6 39 37 5 2 46 9
26 37 48 30 38 28 34
14 43 12 19 26 49 26
44 27 21
41 50 3 14 22 4 46
45 16 47 17 22 13 33
31 -2 7 36 37 39 27
13 5 0 21 16 8 10
4 23
25 25 10 33 3 33 41
1 40 39 -1 34 15 35
15 42 39 15 33 17 11
22 -5 10 22 29 1 -3
19 13 44

--------------------------output.txt------------------------------

Mean(March) : 25.9032 Variance(March) : 221.313 Standard Deviation(March) : 14.8766 No of bad readings(March) : 1
Mean(April) : 21.6 Variance(April) : 235.173 Standard Deviation(April) : 15.3354 No of bad readings(April) : 4
Mean(May) : 21.1935 Variance(May) : 215.059 Standard Deviation(May) : 14.6649 No of bad readings(May) :

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote