Help Passing input file into arrays

Pages: 12345
closed account (48T7M4Gy)
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

double mean(const double[], const int);
double max(const double[], const int);
double sum(const double[], const int);
double sumProducts(const double[], const int, const double[]);
double sumSquares(const double[], const int);
double slope(const double[], const int, const double[]);
double intercept(const double[], const int, const double[]);

int main()
{
	const int SIZE = 100;
	int count = 0;
	double Deaths[SIZE], Doctors[SIZE], Hospitals[SIZE], Income[SIZE], Population[SIZE];

	ifstream health("health.txt");
	string firstline;

	if (!health.is_open())
	{
		cout << "Failed to open input file" << endl;
		return 1;
	}
	else
	{
		getline(health, firstline);

		while (health >> Deaths[count] >> Doctors[count] >> Hospitals[count] >> Income[count] >> Population[count])
			count++;
	}

	cout << "     Death: "
		<< mean(Deaths, count) << '\t' << max(Deaths, count) << '\t'
		<< sum(Deaths, count) << endl;

	cout << "   Doctors: "
		<< mean(Doctors, count) << '\t' << max(Doctors, count) << '\t'
		<< sum(Doctors, count) << endl;

	cout << " Hospitals: "
		<< mean(Hospitals, count) << '\t' << max(Hospitals, count) << '\t'
		<< sum(Hospitals, count) << endl;

	cout << "    Income: "
		<< mean(Income, count) << '\t' << max(Income, count) << '\t'
		<< sum(Income, count) << endl;

	cout << "Population: "
		<< mean(Population, count) << '\t' << max(Population, count) << '\t'
		<< sum(Population, count) << endl;

	//  Perform other calculations and outputs here
	cout << "Income vs Deaths linear regression slope: " << slope(Income, count, Deaths) << endl;

	return 0;
}

double mean(const double x[], const int n)
{
	double sum = 0;

	for (int i = 0; i < n; i++)
	{
		sum += x[i];
	}
	return sum / n;
}

double max(const double x[], const int n)
{
	// Determine local objects
	double max;

	// Determine maximum value in the array
	max = x[0];
	for (int i = 0; i < n; i++)
	{
		if (x[i] > max)
			max = x[i];
	}
	return max;
}

double sum(const double x[], const int n)
{
	double total= 0.0;

	for (int i = 0; i < n; i++)
		total+= x[i];

	return total;
}

double sumProducts(const double x[], const int n, const double y[])
{
	double sum = 0.0;

	for (int i = 0; i < n; i++)
		sum += x[i] * y[i];

	return sum;
}

double sumSquares(const double x[], const int n)
{
	double sum = 0.0;

	for (int i = 0; i < n; i++)
		sum += x[i] * x[i];

	return sum;
}

double slope(const double x[], const int n, const double y[])
{
	//Slope(b) = (NΣXY - (ΣX)(ΣY)) / (NΣX2 - (ΣX)2)
	return (n * sumProducts(x, n, y) - sum(x, n) * sum(y, n)) / (n * sumSquares(x, n) - sum(x, n) * sum(x, n));
}

double intercept(const double x[], const int n, const double y[])
{
	//Intercept(a) = (ΣY - b(ΣX)) / N
	double a = 0;

	// blah blah blah

	return a;
}


     Death: 9.30566     12.8    493.2
   Doctors: 116.094     238     6153
 Hospitals: 589.792     1792    31259
    Income: 9.43585     13      500.1
Population: 110.642     292     5864
Income vs Deaths linear regression slope: -0.265884
Last edited on
wow ok thanks. I do not understand though your equation to solve for the slope. i see that it ouputs the correct answer but i dont understand how you created that to work. Since i need to write the y-intercept right now i want to understand how you did that. sumSquares(x, n) for example. Thank you kemort :)
nevermind buddy i figured it out :) this seems like a much easier way. my brain jsut doesnt process how my code could recognize variables like that . I am as usual going to study how this works. Now i understand Arrays !!!! Thank you sir. I will post the final 'product'
closed account (48T7M4Gy)
The formulae are at the reference I posted above. Don't ask me to derive them. I used to know the derivation but ...
here it is. im still going to fix the layout up a bit . ty ty ty sir

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
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <iomanip>

using namespace std;

double mean(const double[], const int);
double max(const double[], const int);
double sum(const double[], const int);
double sumProducts(const double[], const int, const double[]);
double sumSquares(const double[], const int);
double slope(const double[], const int, const double[]);
double intercept(const double[], const int, const double[]);

int main()
{
    const int SIZE = 100;
    int COUNT = 0;
    double Deaths[SIZE], Doctors[SIZE], Hospitals[SIZE], Income[SIZE], Population[SIZE];
    
    ifstream health("health.txt");
    string firstline;
    
    if (!health.is_open())
    {
        cout << "Failed to open input file" << endl;
        return 1;
    }
    else
    {
        getline(health, firstline);
        
        while (health >> Deaths[COUNT] >> Doctors[COUNT] >> Hospitals[COUNT] >> Income[COUNT] >> Population[COUNT])
            COUNT++;
    }
    
    cout << "Mean, Max, Sum of DEATH: " << setprecision(2) << fixed
    << mean(Deaths, COUNT) << '\t' << max(Deaths, COUNT) << '\t'
    << sum(Deaths, COUNT) << endl;
    
    cout << "Mean, Max, Sum of DOC: "
    << setw(8) << mean(Doctors, COUNT) << '\t' << max(Doctors, COUNT) << '\t'
    << sum(Doctors, COUNT) << endl;
    
    cout << "Mean, Max, Sum of HOSP: "
    << setw(7) << mean(Hospitals, COUNT) << '\t' << max(Hospitals, COUNT) << '\t'
    << sum(Hospitals, COUNT) << endl;
    
    cout << "Mean, Max, Sum of INC: "
    << setw(6) << mean(Income, COUNT) << '\t' << max(Income, COUNT) << '\t'
    << sum(Income, COUNT) << endl;
    
    cout << "Mean, Max, Sum of POPUL: "
    << mean(Population, COUNT) << '\t' << max(Population, COUNT) << '\t'
    << sum(Population, COUNT) << endl;
    
    cout << "" << endl;
    
    cout << "Income vs Deaths:" << setw(7) << "y = " << slope(Income, COUNT, Deaths) << "x + ";
    cout << intercept(Income, COUNT, Deaths) << endl;
    
    cout << "Doctors vs Deaths:" << setw(6) << "y = " << slope(Doctors, COUNT, Deaths) << "x + ";
    cout << intercept(Doctors, COUNT, Deaths) << endl;


    
    return 0;

}

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

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

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

double sumProducts(const double x[], const int n, const double y[])
{
    double sum = 0.0;
    
    for (int i = 0; i < n; i++)
        sum += x[i] * y[i];
    
    return sum;
}

double sumSquares(const double x[], const int n)
{
    double sum = 0.0;
    
    for (int i = 0; i < n; i++)
        sum += x[i] * x[i];
    
    return sum;
}

double slope(const double x[], const int n, const double y[])
{
  
    return (n * sumProducts(x, n, y) - sum(x, n) * sum(y, n)) / (n * sumSquares(x, n) - sum(x, n) * sum(x, n));
}

double intercept(const double x[], const int n, const double y[])
{
    double a = 0;
    
    a += (sum(x, n) * sumProducts(x, n, y) - sumSquares(x, n) * sum(y, n)) / (sum(x, n) * sum(x, n) - n * sumSquares(x, n));
                                                    
    return a;
}


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 INC:   9.44	13.00	500.10
Mean, Max, Sum of POPUL: 110.64	292.00	5864.00

Income vs Deaths:   y = -0.27x + 11.81
Doctors vs Deaths:  y = 0.01x + 8.72
Last edited on
ummm.. kemort .. i just recieved this in an email and i have no idea what he means

"Please note that you will be penalized heavily if you hardcode in the number of cities in the file i.e., using a for-loop with i < 53. It would behoove you to use .eof() when reading in your data from the file."

did i do what he said NOT to do because i have a SIZE and a COUNT? :/
Last edited on
closed account (48T7M4Gy)
It sounds a bit weird and not at all clear to me. ('behoove' is English but almost uniquely US-speak.)

1. You have only hardcoded the array space (100) but counted the elements as they come in which is OK to me, but I don't dish out the marks.

2. You will see it mentioned many, many times in advice to people here that eof() is not good form. The way you have incorporate the cin stream in the while statement is good/adequate/sound.

However, you might have to 'play the game' and explicitly read the file using eof() as the limit. Test and re-test to get it right with the data file. Make sure there are no blank lines at the end.

I'll test it myself and see if it works OK.


closed account (48T7M4Gy)
1
2
3
4
5
while (!health.eof())
 {
  health >> Deaths[count] >> Doctors[count] >> Hospitals[count] >> Income[count] >> Population[count];
  count++;
 }


Gives the same answer (as expected).

Another way of doing it is to count the elements first, dynamically allocate the arrays and then read the data in via a second run. How are you with pointers?
Last edited on
ok ya i always see people saying not to use .eof. wierd. but ok i will change it thank you :)
i havent learned about pointers yet. i dont know if i will this year
The assignment specifically stated that there were a maximum of 100 records in the file.
SO assuming a max size of your arrays of 100 is fine.

I see nothing in your code now that assumes 53 records.
We went through this many times.

did i do what he said NOT to do because i have a SIZE and a COUNT? :/

Not as far as I can tell. You have it right. There is NO mention of 53 in your program.


ok i finished the program . i am told to label my units for the slope intercept formula. this was given to me. what would be my units for the formulas ? or do you think he means what i already wrote for each formula :
- Income vs Deaths
&
- Doctors vs Deaths

Deaths = death rate per 1,000 residents
Doctors = doctors per 100,000 residents.
Hosp = hospitals per 100,000 residents
Income = annual income in 1,000's of dollars (per capita) Population = people per square mile
Last edited on
closed account (48T7M4Gy)
It depends on the units you are given and which axis, x or y each array is on.

So if income corresponds with x[] in your function and Deaths goes with y[] then slope is dy/dx so slope is Deaths/Income or no of deaths per $ income depending on the finer points of the given units.

The intercept is the y value for x = 0. So the intercept would be measured in no of deaths.

Same logic to with other combinations.

Last edited on
ya so x is income and doctors and the y-axis is deaths. and the info in my previos post is what is given. ok thanks
i just want to thank jib, AbstractionAnon and Kemort for the help and guidance. I definately learned a lot from this assignment. Thank you guys
closed account (48T7M4Gy)
:)
closed account (48T7M4Gy)
Oops!
a += (sum(x, n) * sumProducts(x, n, y) - sumSquares(x, n) * sum(y, n)) / (sum(x, n) * sum(x, n) - n * sumSquares(x, n));

+= NOT! You are very lucky that a = 0 initially and there was no iteration loop.
Last edited on
wait what ? i already turned in the assignment so its past 5 . did i mess something up ?
closed account (48T7M4Gy)
did i mess something up ?


Yeah, but it's not the end of the world.

should be a = ... , not a += ...

The answer will be correct.
o i see. if i just had double a; then i would be in trouble. gotcha. ok well thanks again buddy ! I have 1 more assignment to do this semester and its this exact same assignment but i have to do it in MATLAB which i heard is considerably easier and simpler so that is good. Have a good weekend kemort !
Last edited on
Pages: 12345