Help Passing input file into arrays

Pages: 12345
i keep getting this. i use xcode and i am pressing Command+R
Mean deaths = 9.30566
Mean doctors = 116.094
Mean hospitals = 589.792
Mean income = 9.43585
Mean population = 110.642
(lldb) 
Last edited on
i just cant get it to work. i keep getting a debugging error and you saying that you got it to work with what i posted makes me think there's maybe something wrong with xcode. unless my teacher made an error somewhere down the line of the input file that prevents me from getting a successful output.
ok pheww.. i did get the right answers for all 3 categories. But when it showed me the debugging after the mean, i had to manually click continue program execution for to get each max and sum value. My program had line 98 as the issue and then once i clicked continue program execution for the max's, then the sum's were fine. Why did that happen if the answer is right ? i just dont want my teacher to have to manually do that himself ya know ?

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <iomanip>

using namespace std;

const int SIZE = 100; // Maximum size of array
double CalculateMean(const double x[], int cnt); // Mean of each category
double CalculateMax(const double x[], int cnt); // Maximum of each category
double CalculateSum(const double x[], int cnt); // Sum of each category

int main ()
{   double Deaths[SIZE];    //  Changed to double to match input
    double Doctors[SIZE];
    double Hospitals[SIZE];
    double Income[SIZE];    //  Changed to double to match input
    double Population[SIZE];
    ifstream health("health.txt");  // Open health.txt
    string firstline;
    int cnt = 0;
    double MeanDeaths, MeanDoctors, MeanHospitals, MeanIncome, MeanPopulation;
    double MaxDeaths, MaxDoctors, MaxHospitals, MaxIncome,MaxPopulation;
    double SumDeaths, SumDoctors, SumHospitals, SumIncome, SumPopulation;
    
    //  Check if open failed
    if (! health.is_open())
    {   cout << "Failed to open input file" << endl;
        return 1;
    }
    //  Eat the column headers
    getline(health, firstline);
    
    //  Read the input
    //  Note that we don't use eof()
    while (health >> Deaths[cnt] >> Doctors[cnt] >> Hospitals[cnt] >> Income[cnt] >> Population[cnt])
    {   cnt++;      //  Increment numbeer of records read
    }
    
    MeanDeaths = CalculateMean (Deaths, cnt);
    cout << "Mean deaths = " << MeanDeaths << endl;
    MeanDoctors = CalculateMean (Doctors, cnt);
    cout << "Mean doctors = " << MeanDoctors << endl;
    MeanHospitals = CalculateMean (Hospitals, cnt);
    cout << "Mean hospitals = " << MeanHospitals << endl;
    MeanIncome = CalculateMean (Income, cnt);
    cout << "Mean income = " << MeanIncome << endl;
    MeanPopulation = CalculateMean (Population, cnt);
    cout << "Mean population = " << MeanPopulation << endl;
    
    MaxDeaths = CalculateMax (Deaths, cnt);
    cout << "Max Deaths = " << MaxDeaths << endl;
    MaxDoctors = CalculateMax (Doctors, cnt);
    cout << "Max doctors = " << MaxDoctors << endl;
    MaxHospitals = CalculateMax (Hospitals, cnt);
    cout << "Max hospitals = " << MaxHospitals << endl;
    MaxIncome = CalculateMax (Income, cnt);
    cout << "Max income = " << MaxIncome << endl;
    MaxPopulation = CalculateMax (Population, cnt);
    cout << "Max population = " << MaxPopulation << endl;
    
    SumDeaths = CalculateSum(Deaths, cnt);
    cout << "Sum deaths = " << SumDeaths << endl;
    SumDoctors = CalculateSum(Doctors, cnt);
    cout << "Sum doctors = " << SumDoctors << endl;
    SumHospitals = CalculateSum(Hospitals, cnt);
    cout << "Sum hospitals = " << SumHospitals << endl;
    SumIncome = CalculateSum(Income, cnt);
    cout << "Sum income = " << SumIncome << endl;
    SumPopulation = CalculateSum(Population, cnt);
    cout << "Sum population = " << SumPopulation << endl;
    
    
    
    //  Perform other calculations and outputs here
    system ("pause");
    return 0;
}

double CalculateMean(const double x[], int cnt)
{
    double sum(0);
    
    for (int i=0; i<cnt; i++)
    {
        sum += x[i];
    }
    return sum/cnt;}


double CalculateMax (const double x[], int cnt)
{
    // Determine local objects
    double Max;
    
    // Determine maximum value in the array
    Max = x[0];
    for (int i=0; i<cnt; i++)
    {
        if (x[i] > Max)
            Max = x[i];
    }
    // Return maximum value. /
    return Max;
}

double CalculateSum(const double x[], int cnt)
{
    double sumOfFile = 0.0;
    
    for (int i=0; i<cnt; i++)
    {
        sumOfFile += x[i];
    }
    return sumOfFile;
}
Last edited on
i figured out how to delete the breakpoints so now it works :)
-For my functions in my OP 4 & 5 my teacher didnt really explain those to well .
- a function that takes as input 2 arrays and the size ( the two arrays are the same size) and returns a sum of products
- A function that takes as input an array and the size of the array and returns the sum of the square of the array

-- It doesnt say that i have to return anything just write the functions. I am just confused on which arrays i am suppose to use to calculate. or do i just make up random variables and. Can someone maybe guess at what he means ? Thanks
What exactly don't you understand about the item 4?

It doesnt say that i have to return anything just write the functions.

Yes it does. It says you need to return the sum of the products.

What exactly don't you understand about item 5?

This one requires you to return the sum of the squares of the array.

I am just confused on which arrays i am suppose to use to calculate.

Which arrays you use probably doesn't matter. For #4 pick two of your arrays and pass them along with the "cnt" of these arrays and return the proper value using the return statement.

For number 4, pass anyone of your arrays, along with the "cnt" and compute the sum of the squares.
i am supposed to calculate the slope and y intercept using :
Deaths vs Income & Deaths vs Doctors using linear regression so maybe i should use those?
I mean all i was told to return was the mean max sum of the 5 categories and then the slope and y intercept. this is what it should look like but i already got the mean max and sum.


mean max sum of DEATH =       9.31      12.80     493.20
mean max sum of DOC   =     116.09     238.00    6153.00
mean max sum of HOSP  =     589.79    1792.00   31259.00
mean max sum of INCOM =       9.44      13.00     500.10
mean max sum of POPUL =     110.64     292.00    5864.00

INC vs Death y=-0.27x + 11.81
Doc vs Death y=0.01x + 8.72


- here is my program. so i should write (4) & (5) below my sum function correct ?

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <iomanip>

using namespace std;

const int SIZE = 100; // Maximum size of array
double CalculateMean(const double x[], int cnt); // Mean of each category
double CalculateMax(const double x[], int cnt); // Maximum of each category
double CalculateSum(const double x[], int cnt); // Sum of each category

int main ()
{   double Deaths[SIZE];
    double Doctors[SIZE];
    double Hospitals[SIZE];
    double Income[SIZE];
    double Population[SIZE];
    ifstream health("health.txt");  // Open health.txt
    string firstline;
    int cnt = 0;
    double MeanDeaths, MeanDoctors, MeanHospitals, MeanIncome, MeanPopulation;
    double MaxDeaths, MaxDoctors, MaxHospitals, MaxIncome,MaxPopulation;
    double SumDeaths, SumDoctors, SumHospitals, SumIncome, SumPopulation;
    
    //  Check if open failed
    if (! health.is_open())
    {   cout << "Failed to open input file" << endl;
        return 1;
    }
 
    getline(health, firstline);
    
    //  Read input
    while (health >> Deaths[cnt] >> Doctors[cnt] >> Hospitals[cnt] >> Income[cnt] >> Population[cnt])
    {
        cnt++;
    }
    
    MeanDeaths = CalculateMean (Deaths, cnt);
    MeanDoctors = CalculateMean (Doctors, cnt);
    MeanHospitals = CalculateMean (Hospitals, cnt);
    MeanIncome = CalculateMean (Income, cnt);
    MeanPopulation = CalculateMean (Population, cnt);

    SumDeaths = CalculateSum(Deaths, cnt);
    SumDoctors = CalculateSum(Doctors, cnt);
    SumHospitals = CalculateSum(Hospitals, cnt);
    SumIncome = CalculateSum(Income, cnt);
    SumPopulation = CalculateSum(Population, cnt);

    MaxDeaths = CalculateMax (Deaths, cnt);
    MaxDoctors = CalculateMax (Doctors, cnt);
    MaxHospitals = CalculateMax (Hospitals, cnt);
    MaxIncome = CalculateMax (Income, cnt);
    MaxPopulation = CalculateMax (Population, cnt);
    
    cout << "Mean, Max, Sum Deaths = " << setprecision(2) << fixed << MeanDeaths << setw(8) << MaxDeaths << setw(8) << SumDeaths << endl;
    cout << "Mean, Max, Sum Doctors = " << MeanDoctors << setw(8) << MaxDoctors << setw(10) << SumDoctors << endl;
    cout << "Mean, Max, Sum Hospitals = " << MeanHospitals << setw(10) << MaxHospitals << setw(10) << SumHospitals << endl;
    cout << "Mean, Max, Sum Income = " << MeanIncome << setw(7) << MaxIncome << setw(8) << SumIncome << endl;
    cout << "Mean, Max, Sum Population = " << MeanPopulation << setw(8) << MaxPopulation << setw(9) << SumPopulation << endl;
    
    
 //  Perform other calculations and outputs here
  
    return 0;
}

double CalculateMean(const double x[], int cnt)
{
    double sum(0);
    
    for (int i=0; i<cnt; i++)
    {
        sum += x[i];
    }
    return sum/cnt;}


double CalculateMax (const double x[], int cnt)
{
    // Determine local objects
    double Max;
    
    // Determine maximum value in the array
    Max = x[0];
    for (int i=0; i<cnt; i++)
    {
        if (x[i] > Max)
            Max = x[i];
    }
       return Max;
}

double CalculateSum(const double x[], int cnt)
{
    double sumOfFile = 0.0;
    
    for (int i=0; i<cnt; i++)
    {
        sumOfFile += x[i];
    }
    return sumOfFile;
}
Last edited on
closed account (48T7M4Gy)
I'm sure Kemort will be along sometime to give you the solutions you want.

I didn't know you had yet another dreaded assignment until a few minutes ago. Family responsibilities took over the last few days. It looks like you're in good hands with jlb Phil. I won't interfere. How's your dog? Well-walked I hope. :)
Last edited on
Hey kemort :) how are you buddy ! unfortunately my buddy michelangelo (my puppy) is on his last days :( we try to walk him but he gets scared and tired very easily. We are just givin him lots of lovin. I will see him in 1 month :) This assignment is due tomorrow by5pm so i dont think i will finish it it time :/ but i will get some points at least.
- I just need to write 2 more functions 4 & 5 on my OP and then calculate the slope and y intercept using linear regression . i have an exam tomorrow so i cant stay up to late on this assignment. I'm trying though !
i feel as if for (4) sum of products using 2 arrays that i am supposed to use columns . like for Income vs Death would be columns 4 & 1. Ugh. Ill try doing it how jib explained it first right now
i wrote this but i dont think im doing it right ..

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <iomanip>

using namespace std;

const int SIZE = 100; // Maximum size of array
double CalculateMean(const double x[], int cnt); // Mean of each category
double CalculateMax(const double x[], int cnt); // Maximum of each category
double CalculateSum(const double x[], int cnt); // Sum of each category
double SumOfProducts(const double x[], int cnt, const double x[]);

int main ()
{   double Deaths[SIZE];
    double Doctors[SIZE];
    double Hospitals[SIZE];
    double Income[SIZE];
    double Population[SIZE];
    ifstream health("health.txt");  // Open health.txt
    string firstline;
    int cnt = 0;
    double MeanDeaths, MeanDoctors, MeanHospitals, MeanIncome, MeanPopulation;
    double MaxDeaths, MaxDoctors, MaxHospitals, MaxIncome,MaxPopulation;
    double SumDeaths, SumDoctors, SumHospitals, SumIncome, SumPopulation;
    
    
    //  Check if open failed
    if (! health.is_open())
    {   cout << "Failed to open input file" << endl;
        return 1;
    }
 
    getline(health, firstline);
    
    //  Read input
    while (health >> Deaths[cnt] >> Doctors[cnt] >> Hospitals[cnt] >> Income[cnt] >> Population[cnt])
    {
        cnt++;
    }
    
    MeanDeaths = CalculateMean (Deaths, cnt);
    MeanDoctors = CalculateMean (Doctors, cnt);
    MeanHospitals = CalculateMean (Hospitals, cnt);
    MeanIncome = CalculateMean (Income, cnt);
    MeanPopulation = CalculateMean (Population, cnt);

    SumDeaths = CalculateSum(Deaths, cnt);
    SumDoctors = CalculateSum(Doctors, cnt);
    SumHospitals = CalculateSum(Hospitals, cnt);
    SumIncome = CalculateSum(Income, cnt);
    SumPopulation = CalculateSum(Population, cnt);

    MaxDeaths = CalculateMax (Deaths, cnt);
    MaxDoctors = CalculateMax (Doctors, cnt);
    MaxHospitals = CalculateMax (Hospitals, cnt);
    MaxIncome = CalculateMax (Income, cnt);
    MaxPopulation = CalculateMax (Population, cnt);
    
    cout << "Mean, Max, Sum of Death = " << setprecision(2) << fixed << MeanDeaths << setw(8) << MaxDeaths << setw(8) << SumDeaths << endl;
    cout << "Mean, Max, Sum of Doctors = " << MeanDoctors << setw(8) << MaxDoctors << setw(10) << SumDoctors << endl;
    cout << "Mean, Max, Sum of Hospitals = " << MeanHospitals << setw(10) << MaxHospitals << setw(10) << SumHospitals << endl;
    cout << "Mean, Max, Sum of Income = " << MeanIncome << setw(7) << MaxIncome << setw(8) << SumIncome << endl;
    cout << "Mean, Max, Sum of Population = " << MeanPopulation << setw(8) << MaxPopulation << setw(9) << SumPopulation << endl;
    
    
 //  Perform other calculations and outputs here
  
    return 0;
}

double CalculateMean(const double x[], int cnt)
{
    double sum(0);
    
    for (int i=0; i<cnt; i++)
    {
        sum += x[i];
    }
    return sum/cnt;
}


double CalculateMax (const double x[], int cnt)
{
    // Determine local objects
    double Max;
    
    // Determine maximum value in the array
    Max = x[0];
    for (int i=0; i<cnt; i++)
    {
        if (x[i] > Max)
            Max = x[i];
    }
       return Max;
}

double CalculateSum(const double x[], int cnt)
{
    double sumOfFile = 0.0;
    
    for (int i=0; i<cnt; i++)
    {
        sumOfFile += x[i];
    }
    return sumOfFile;
}

double SumOfProducts(const double x[], int cnt, const double x[])
{
    double SumOfProducts = 0.0;
    
    for (int i=0; i<cnt; i++)
    {
        SumOfProducts *= x[i];
    }
    return SumOfProducts;
}
double SumOfSquares(const double x[], int cnt)
{
    double SumOfSquares = 0.0;
    
    for (int i=0; i<cnt; i++)
    {
        SumOfSquares += x[i]*x[i];
    }
    return SumOfSquares;
}
Last edited on
closed account (48T7M4Gy)
I will interfere if it's OK

double SumOfProducts(const double x[], int cnt, const double x[])
{
double SumOfProducts = 0.0;

for (int i=0; i<cnt; i++)
{
SumOfProducts *= x[i];
}
return SumOfProducts;
}

This is all wrong.
What you have to do is get the sum of the products of two array. So if you have array x and array y and they are the same size then you calculate sum += x[i]*y[i]

You pass arrays x[] y[] and their, common to both arrays, int size.

I didn't look overly closely at the question to determine which two arrays they require but it could be Deaths and Doctors, whatever. (there is a correlation and getting the sum of products is part of the process of quantifying it statistically)

Sorry to hear about your dog :(

so my teacher emailed me back essentially with what you just said. he told me to use my double Deaths[SIZE] & double Income[SIZE] for my arrayX[] and arrayY[] but will my program associate arrayX and arrayY with Deaths and Income ? or is he generally saying to put arrayX as Deaths[] and Income in my sumofProducts() function.
- this is what my fucntion looks like
1
2
3
4
5
6
7
8
9
double SumOfProducts(double Deaths[], double Income[], int cnt)
{
    double SumOP = 0.0;
    
    for (int i=0; i<cnt; i++)
    {
        SumOP = SumOP + Deaths[i] * Income[i];
    }
    return SumOP;

-above my int main()
double SumOfProducts(double Deaths[], double Income[], int cnt);
Last edited on
i think i did it ! :) here is my code and output :) does it look right ?
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <iomanip>

using namespace std;

const int SIZE = 100; // Maximum size of array
double CalculateMean(const double x[], int cnt); // Mean of each category
double CalculateMax(const double x[], int cnt); // Maximum of each category
double CalculateSum(const double x[], int cnt); // Sum of each category
double SumOfProducts(double Deaths[], double Income[], int cnt);
double SumOfSquares(double Deaths[], int cnt);

int main ()
{   double Deaths[SIZE];
    double Doctors[SIZE];
    double Hospitals[SIZE];
    double Income[SIZE];
    double Population[SIZE];
    ifstream health("health.txt");  // Open health.txt
    string firstline;
    int cnt = 0;
    double MeanDeaths, MeanDoctors, MeanHospitals, MeanIncome, MeanPopulation;
    double MaxDeaths, MaxDoctors, MaxHospitals, MaxIncome,MaxPopulation;
    double SumDeaths, SumDoctors, SumHospitals, SumIncome, SumPopulation;
    double SumOfProd, SumOfSq;
    
    
    //  Check if open failed
    if (! health.is_open())
    {   cout << "Failed to open input file" << endl;
        return 1;
    }
 
    getline(health, firstline);
    
    //  Read input
    while (health >> Deaths[cnt] >> Doctors[cnt] >> Hospitals[cnt] >> Income[cnt] >> Population[cnt])
    {
        cnt++;
    }
    
    MeanDeaths = CalculateMean (Deaths, cnt);
    MeanDoctors = CalculateMean (Doctors, cnt);
    MeanHospitals = CalculateMean (Hospitals, cnt);
    MeanIncome = CalculateMean (Income, cnt);
    MeanPopulation = CalculateMean (Population, cnt);

    SumDeaths = CalculateSum(Deaths, cnt);
    SumDoctors = CalculateSum(Doctors, cnt);
    SumHospitals = CalculateSum(Hospitals, cnt);
    SumIncome = CalculateSum(Income, cnt);
    SumPopulation = CalculateSum(Population, cnt);

    MaxDeaths = CalculateMax (Deaths, cnt);
    MaxDoctors = CalculateMax (Doctors, cnt);
    MaxHospitals = CalculateMax (Hospitals, cnt);
    MaxIncome = CalculateMax (Income, cnt);
    MaxPopulation = CalculateMax (Population, cnt);
    
    cout << "Mean, Max, Sum of Death = " << setprecision(2) << fixed << MeanDeaths << setw(8) << MaxDeaths << setw(8) << SumDeaths << endl;
    cout << "Mean, Max, Sum of Doctors = " << MeanDoctors << setw(8) << MaxDoctors << setw(10) << SumDoctors << endl;
    cout << "Mean, Max, Sum of Hospitals = " << MeanHospitals << setw(10) << MaxHospitals << setw(10) << SumHospitals << endl;
    cout << "Mean, Max, Sum of Income = " << MeanIncome << setw(7) << MaxIncome << setw(8) << SumIncome << endl;
    cout << "Mean, Max, Sum of Population = " << MeanPopulation << setw(8) << MaxPopulation << setw(9) << SumPopulation << endl;
    
    SumOfProd = SumOfProducts(Deaths, Income, cnt);
    cout << "Sum of Products = " << SumOfProd << endl;
    SumOfSq = SumOfSquares(Deaths, cnt);
    cout << "Sum of Squares = " << SumOfSq << endl;
    
    
 //  Perform other calculations and outputs here
  
    return 0;
}

double CalculateMean(const double x[], int cnt)
{
    double sum(0);
    
    for (int i=0; i<cnt; i++)
    {
        sum += x[i];
    }
    return sum/cnt;
}


double CalculateMax (const double x[], int cnt)
{
    // Determine local objects
    double Max;
    
    // Determine maximum value in the array
    Max = x[0];
    for (int i=0; i<cnt; i++)
    {
        if (x[i] > Max)
            Max = x[i];
    }
       return Max;
}

double CalculateSum(const double x[], int cnt)
{
    double sumOfFile = 0.0;
    
    for (int i=0; i<cnt; i++)
    {
        sumOfFile += x[i];
    }
    return sumOfFile;
}

double SumOfProducts(double Deaths[], double Income[], int cnt)
{
    double SumOP = 0.0;
    
    for (int i=0; i<cnt; i++)
    {
        SumOP = SumOP + Deaths[i] * Income[i];
    }
    return SumOP;
}
double SumOfSquares(double Deaths[], int cnt)
{
    double SumOS = 0.0;
    
    for (int i=0; i<cnt; i++)
    {
        SumOS += Deaths[i] * Deaths[i];
    }
    return SumOS;
}


Mean, Max, Sum of Death = 9.31   12.80  493.20
Mean, Max, Sum of Doctors = 116.09  238.00   6153.00
Mean, Max, Sum of Hospitals = 589.79   1792.00  31259.00
Mean, Max, Sum of Income = 9.44  13.00  500.10
Mean, Max, Sum of Population = 110.64  292.00  5864.00
Sum of Products = 4637.77
Sum of Squares = 4733.28
here is my finished program. i am not getting the correct line regression formula though.
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <iomanip>

using namespace std;

const int SIZE = 100; // Maximum size of array
double CalculateMean(const double x[], int cnt); // Mean of each category
double CalculateMax(const double x[], int cnt); // Maximum of each category
double CalculateSum(const double x[], int cnt); // Sum of each category
double SumOfProducts1(double Income[], double Deaths[], int cnt);
double SumOfProducts2(double Doctors[], double Deaths[], int cnt);
double SumOfSquares1(double Income[], int cnt);
double SumOfSquares2(double Doctors[], int cnt);

int main ()
{   double Deaths[SIZE];
    double Doctors[SIZE];
    double Hospitals[SIZE];
    double Income[SIZE];
    double Population[SIZE];
    ifstream health("health.txt");  // Open health.txt
    string firstline;
    int cnt = 0;
    double MeanDeaths, MeanDoctors, MeanHospitals, MeanIncome, MeanPopulation;
    double MaxDeaths, MaxDoctors, MaxHospitals, MaxIncome,MaxPopulation;
    double SumDeaths, SumDoctors, SumHospitals, SumIncome, SumPopulation;
    double SumOfProd1, SumOfSq1, slope1 = 0.0, yInt1 = 0.0;
    double SumOfProd2, SumOfSq2, slope2 = 0.0, yInt2 = 0.0;
    
    
    //  Check if open failed
    if (! health.is_open())
    {   cout << "Failed to open input file" << endl;
        return 1;
    }
 
    getline(health, firstline);
    
    //  Read input
    while (health >> Deaths[cnt] >> Doctors[cnt] >> Hospitals[cnt] >> Income[cnt] >> Population[cnt])
    {
        cnt++;
    }
    
    MeanDeaths = CalculateMean (Deaths, cnt);
    MeanDoctors = CalculateMean (Doctors, cnt);
    MeanHospitals = CalculateMean (Hospitals, cnt);
    MeanIncome = CalculateMean (Income, cnt);
    MeanPopulation = CalculateMean (Population, cnt);

    SumDeaths = CalculateSum(Deaths, cnt);
    SumDoctors = CalculateSum(Doctors, cnt);
    SumHospitals = CalculateSum(Hospitals, cnt);
    SumIncome = CalculateSum(Income, cnt);
    SumPopulation = CalculateSum(Population, cnt);

    MaxDeaths = CalculateMax (Deaths, cnt);
    MaxDoctors = CalculateMax (Doctors, cnt);
    MaxHospitals = CalculateMax (Hospitals, cnt);
    MaxIncome = CalculateMax (Income, cnt);
    MaxPopulation = CalculateMax (Population, cnt);
    
    cout << "Mean, Max, Sum of Death = " << setprecision(2) << fixed << MeanDeaths << setw(8) << MaxDeaths << setw(8) << SumDeaths << endl;
    cout << "Mean, Max, Sum of Doctors = " << MeanDoctors << setw(8) << MaxDoctors << setw(10) << SumDoctors << endl;
    cout << "Mean, Max, Sum of Hospitals = " << MeanHospitals << setw(10) << MaxHospitals << setw(10) << SumHospitals << endl;
    cout << "Mean, Max, Sum of Income = " << MeanIncome << setw(7) << MaxIncome << setw(8) << SumIncome << endl;
    cout << "Mean, Max, Sum of Population = " << MeanPopulation << setw(8) << MaxPopulation << setw(9) << SumPopulation << endl;
    
    SumOfProd1 = SumOfProducts1(Income, Deaths, cnt);
    SumOfSq1 = SumOfSquares1(Income, cnt);
    
    SumOfProd2 = SumOfProducts2(Doctors, Deaths, cnt);
    SumOfSq2 = SumOfSquares2(Doctors, cnt);
    

    slope1 = (SumIncome*SumDeaths - cnt*SumOfProd1) / (SumOfSq1 - cnt*SumOfSq1);
    yInt1 = (SumIncome*SumOfProd1 - SumOfSq1*SumDeaths) / (SumOfSq1 - cnt*SumOfSq1);
    cout << "Income vs Death" << setw(6) << "y= " << slope1 << "x + "; cout << yInt1 << endl;
    
    slope2 = (SumDoctors*SumDeaths - cnt*SumOfProd2) / (SumOfSq1 - cnt*SumOfSq1);
    yInt2 = (SumDoctors*SumOfProd2 - SumOfSq2*SumDeaths) / (SumOfSq2 - cnt*SumOfSq2);
     cout << "Doctors vs Death" << setw(6) << "y= " << slope2 << "x + "; cout << yInt2 << endl;
  
    return 0;
}

double CalculateMean(const double x[], int cnt)
{
    double sum = 0.0;
    
    for (int i=0; i<cnt; i++)
    {
        sum += x[i];
    }
    return sum/cnt;
}

double CalculateMax (const double x[], int cnt)
{
    double Max;
    
    Max = x[0];
    for (int i=0; i<cnt; i++)
    {
        if (x[i] > Max)
            Max = x[i];
    }
       return Max;
}

double CalculateSum(const double x[], int cnt)
{
    double sumOfFile = 0.0;
    
    for (int i=0; i<cnt; i++)
    {
        sumOfFile += x[i];
    }
    return sumOfFile;
}

double SumOfProducts1(double Income[], double Deaths[], int cnt)
{
    double SumOP1 = 0.0;
    
    for (int i=0; i<cnt-1; i++)
    {
        SumOP1 += Income[i] * Deaths[i];
    }
    return SumOP1;
}

double SumOfSquares1(double Income[], int cnt)
{
    double SumOS1 = 0.0;
    
    for (int i=0; i<cnt-1; i++)
    {
        SumOS1 += Income[i] * Income[i];
    }
    return SumOS1;
}

double SumOfProducts2(double Doctors[], double Deaths[], int cnt)
{
    double SumOP2 = 0.0;
    
    for (int i=0; i<cnt-1; i++)
    {
        SumOP2 += Doctors[i] * Deaths[i];
    }
    return SumOP2;
}

double SumOfSquares2(double Doctors[], int cnt)
{
    double SumOS2 = 0.0;
    
    for (int i=0; i<cnt-1; i++)
    {
        SumOS2 += Doctors[i] * Doctors[i];
    }
    return SumOS2;
}

- What my line regression formula looks like:
Mean, Max, Sum of Death = 9.31   12.80  493.20
Mean, Max, Sum of Doctors = 116.09  238.00   6153.00
Mean, Max, Sum of Hospitals = 589.79   1792.00  31259.00
Mean, Max, Sum of Income = 9.44  13.00  500.10
Mean, Max, Sum of Population = 110.64  292.00  5864.00
Income vs Deaths  y = -0.02x + 0.12
Doctors vs Deaths  y = -0.08x + 0.82


What the line regression formula SHOULD look like
mean max sum of DEATH =       9.31      12.80     493.20
mean max sum of DOC   =     116.09     238.00    6153.00
mean max sum of HOSP  =     589.79    1792.00   31259.00
mean max sum of INCOM =       9.44      13.00     500.10
mean max sum of POPUL =     110.64     292.00    5864.00

INC vs Death y=-0.27x + 11.81
Doc vs Death y=0.01x + 8.72
Last edited on
closed account (48T7M4Gy)
For a start you only need one of each the sumofproducts and sumofsquares functions. They are generalised for general arrays x and y. The prototypes ahead of main should only have parameter types, not names because even though they are ignored, it is misleading.

line 9 should be moved inside main. A global const is not needed and is bad.

Are you still using the same dataset that you show at the start of this thread?

https://www.easycalculation.com/statistics/learn-regression.php
Last edited on
yes here is the whole thing:
-what do you mean the prototypes ahead of main should only have parameter types. you mean my Death[SIZE] etc ? and i do know that i only need 1 of the sum of.. but i couldnt get it to work with just the first one and i wasnt sure if i would be able to finish it by 5 tonight so i just wanted at least all of it even though it should be condensed. I have an exam in 1 hour and then ill be working on this from 2-5.
-Even though i have 2 of the sumof.. i just want to make sure that i get the right formula for the first one then i will go back and make it 1 function each. Can
Deaths Doctors Hosp Income Population
8.00 78 284 9.10 109
9.30 68 433 8.70 144
7.50 70 739 7.20 113
8.90 96 1792 8.90 97
10.20 74 477 8.30 206
8.30 111 362 10.90 124
8.80 77 671 10.00 152
8.80 168 636 9.10 162
10.70 82 329 8.70 150
11.70 89 634 7.60 134
8.50 149 631 10.80 292
8.30 60 257 9.50 108
8.20 96 284 8.80 111
7.90 83 603 9.50 182
10.30 130 686 8.70 129
7.40 145 345 11.20 158
9.60 112 1357 9.70 186
9.30 131 544 9.60 177
10.60 80 205 9.10 127
9.70 130 1264 9.20 179
11.60 140 688 8.30 80
8.10 154 354 8.40 103
9.80 118 1632 9.40 101
7.40 94 348 9.80 117
9.40 119 370 10.40 88
11.20 153 648 9.90 78
9.10 116 366 9.20 102
10.50 97 540 10.30 95
11.90 176 680 8.90 80
8.40 75 345 9.60 92
5.00 134 525 10.30 126
9.80 161 870 10.40 108
9.80 111 669 9.70 77
10.80 114 452 9.60 60
10.10 142 430 10.70 71
10.90 238 822 10.30 86
9.20 78 190 10.70 93
8.30 196 867 9.60 106
7.30 125 969 10.50 162
9.40 82 499 7.70 95
9.40 125 925 10.20 91
9.80 129 353 9.90 52
3.60 84 288 8.40 110
8.40 183 718 10.40 69
10.80 119 540 9.20 57
10.10 180 668 13.00 106
9.00 82 347 8.80 40
10.00 71 345 9.20 50
11.30 118 463 7.80 35
11.30 121 728 8.20 86
12.80 68 383 7.40 57
10.00 112 316 10.40 57
6.70 109 388 8.90 94
closed account (48T7M4Gy)
BTW It's linear regression, not line regression
Last edited on
o ya sorry thats what i meant :p
closed account (48T7M4Gy)
Standby for a short while and I'll change the data.
closed account (48T7M4Gy)
OK I get the same answers.

I changed the names of your functions but this is what your prototypes should look like

1
2
3
4
5
double mean(const double[], const int); // Mean of each category
double max(const double[], const int); // Maximum of each category
double sum(const double[], const int); // Sum of each category
double sumProducts(const double[], const int, const double[]);
double sumSquares(const double[], const int);


I changed the names because 'Calculate' is redundant and just bloats the name.
Pages: 12345