Need help

Write a program that computes the central tendencies mean (average), mode, median and the variation: variance, standard deviation and range for a set of numbers given in the data file ArsenicGroundWaterFile.txt.

The following data represent (naturally occurring) concentration of arsenic in ground water for a random sample of 102 Northwest Texas wells. Units are parts per billion.
Reference: Nichols, C.E. and Kane, V.E., Union Carbide Technical Report K/UR-1

The output should be as follows to two decimal places:

Central Tendency Variation

Mean xxx.xx Variance xxx.xx

Median xxx.xx Std. Dev xxx.xx

Mode xxx.xx Range xxx.xx


I have no idea how to do this assignment, my professor hasn't been of any help. Please help me get started.
Start by reading the values from the file into an array. Do you know how to do that?
No I am not familiar with how the do that.
Okay i read over that and it helped some but i'm still very confused.
Could you post what you have?
I just have it right now to where it will read the file and display what is in it.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>


using namespace std;

ifstream myFile;
double Value;
int main()
{



    ifstream myfile("numbers.txt");
        if(myfile.is_open()){

                while(myfile >> Value){

                    cout << Value << endl;
                }
        }



  }
After that you need to store the values, the C++ way is to use a std::vector but you may be expected to use an array.

By the way, avoid global variables - lines 9 and 10 in above code, better removed and declared locally instead.

1. use a vector

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include <vector>

using namespace std;

int main()
{   
    // Open file and check it is open
    ifstream myfile("numbers.txt");
    if (!myfile)
    {
        cout << "could not open input file\n";
        return 1;
    }
    
    // read all values from file into a vector
    vector<double> data;
    double value;
    while (myfile >> value)
    {
        data.push_back(value);
    }
    
    // Output contents of vector
    cout << "Number of values: " << data.size() << '\n';
    for (size_t i = 0; i < data.size(); i++)
    {
        cout << data[i] << endl;
    }
        
    
  }


2. same functionality (more or less) using an array instead:
I added more comments because arrays require more care and attention.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>

using namespace std;

int main()
{   
    // Open file and check it is open
    ifstream myfile("numbers.txt");
    if (!myfile)
    {
        cout << "could not open input file\n";
        return 1;
    }
    

    // Allocate an array - estimate size required
    const int SIZE = 1000; // Maximum number of values
    double data[SIZE];     // define an array we hope is large enough
    int count = 0;         // keep track of how many values are stored.
    
    // read all values from file into an array
    double value;
    while (count < SIZE && myfile >> value) // while array is not full, read a value
    {
        data[count] = value;  // store it
        count++;              // increaase count
    }
    
    // Output contents of array
    cout << "Number of values: " << count << '\n';
    for (int i = 0; i < count; i++)
    {
        cout << data[i] << endl;
    }
        
    
  }


Once you reach this stage, it's time to move on to writing separate functions to generate the each of the various statistics required.
Last edited on
Registered users can post here. Sign in or register to post.