Help Passing input file into arrays

Pages: 123... 5
Hey guys so I am having a lot of trouble passing arrays into functions from my input.txt file. I've studied and studied and am having difficulty actually implementing it into code. Here is a sample of my txt file:
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

I have to implement the following functions:
1)a function that takes as input an array and array size and returns the mean value.
2)a function that takes as input an array and the size of the array and returns the maximum value of an array
3)a function that takes as input an array and the size of the array and returns the sum of the array.
4)a function that takes as input 2 arrays and the size (assume the two
arrays are the same size)and returns a sum of products:
5.)A function that takes as input an array and the size of the array and returns the sum of the square of the array

- This is my first time ever using arrays let alone writing passing it from a function from an input file.
- Here is my code so far. Any help and i would be eternally grateful. Thank you friends.

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

using namespace std;

const int SIZE = 100; // Maximum size of array
int Deaths[SIZE];
int Doctors[SIZE];
int Hospitals[SIZE];
int Income[SIZE];
int Population[SIZE];
void inputFromFile(int[], int, int, int, int);
double CalculateMean(int[], int, int, int, int); // Mean of each category
double CalculateMax(int[], int, int, int, int); // Maximum of each category
double  CalculateSum(int[], int, int, int, int); // Sum of each category

int main()
{
    int Mean;
    int Max;
    int Sum;
    inputFromFile(Deaths, Doctors, Hosp, Income, Population, SIZE);
    
    
    ifstream health("health.txt");  // Open health.txt
    
string firstline;
    
// Read health.txt
    double Deaths, Doctors, Hosp, Income, Population;
    
    
    
    
    if (health.is_open())
    {
        getline(health, firstLine);
        
        cout << "The mean, maximum & sum of Deaths: " <<  << endl;
        cout << "The mean, maximum & sum of Doctors: " <<  << endl;
        cout << "The mean, maximum & sum of Hospitals: " <<  << endl;
        cout << "The mean, maximum & sum of Income: " <<  << endl;
        cout << "The mean, maximum & sum of Population: " <<  << endl;
        
        while (!health.eof())
        {
        // Organize health.txt Data
            health >> Deaths >> Doctors >> Hosp >> Income >> Population;
        }

    
}

    return 0;
    
    double CalculateMean(const double x[])
    {
        double sum(0);
        
        for (int i=0; i<100; i++)
        {
            sum =+ x[i];
        }
        return sum/n;
    }
    
    double CalculateMax (const double x[])
    {
        double Max = x[0];
        for (int i=1; i<100; i++)
        {
            if (x[i] > Max)
                Max = x[i];
        }
        return Max;
    }

Last edited on
if anyone could please help i would seriously appreciate it.. my grade in this class depends on this assignment and i am stressing out over how difficult this is for me . please.
First you have several arrays that you declared as global variables (a bad practice by the way) that you never use.

Second you try to call a function inputFromFile() that you have failed to implement. Also note all the parameters of this function should probably be arrays, and you probably should return the number of records actually read from the file using the return statement.

Third you have code that is outside any function.

Fourth the two function implementations you provided are not correct, look at your instructions. How many parameters should each of these functions contain?

Fifth line 33 hides your bad global variables since the name of the variables are the same.

this is what my program should output at the end but i have no idea how to write 6 functions using arrays . i have been studying the past 3 days and implenting different things but im so far from being correct. I'm hoping my buddy Kemort will come on soon. He usually walks me through my program and i learn a lot from him without him actually giving me the answers . Very patient person. Thank you jib though i dont know what you mean so much but i will try to figure it out. thanks :)
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
Well good luck, I'm sure Kemort will be along sometime to give you the solutions you want. But you really should start by fixing any and all compiler warnings and errors that your code produces, you should have several errors and several warnings.

ok thank you i will do that now !
any help would be appreciated . ive spent all day trying to figure this out
What have you tried? Post your current code (the code after you fixed all your compile errors and warnings).

here is my current code. This is just my most recent one. i have tried more than 7. I know it's really bad :/

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

using namespace std;

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

int main(void)
{
    int Deaths[SIZE];
    int Doctors[SIZE];
    int Hospitals[SIZE];
    int Income[SIZE];
    int Population[SIZE];
    inputFromFile(Deaths, Doctors, Hospitals, Income, Population);
    
    
    ifstream health("health.txt");  // Open health.txt
    
string firstline;
    
// Read health.txt
    double Death, Docs, Hosp, Inc, Pop;
    
    
    
    
    if (health.is_open())
    {
        getline(health, firstline);
        
        cout << "The mean, maximum & sum of Deaths: " <<  << endl;
        cout << "The mean, maximum & sum of Doctors: " <<  << endl;
        cout << "The mean, maximum & sum of Hospitals: " <<  << endl;
        cout << "The mean, maximum & sum of Income: " <<  << endl;
        cout << "The mean, maximum & sum of Population: " <<  << endl;
        
        while (!health.eof())
        {
        // Organize health.txt Data
            health >> Death >> Docs >> Hosp >> Inc >> Pop;
        }

    
}

    return 0;
    
    double CalculateMean(const double x[], int[], int[], int[], int[], int[])
    {
        double sum(0);
        
        for (int i=0; k<SIZE; i++)
        {
            sum =+ x[i];
        }
        return sum/n;
    }
    
    double CalculateMax (const double x[], int[], int[], int[], int[], int[])
    {
        double Max = x[0];
        for (int i=1; i<SIZE; i++)
        {
            if (x[i] > Max)
                Max = x[i];
        }
        return Max;
    }
}
Last edited on
lines 39-43 i know are wrong i just set it up so that i can put the variable i need in between the << << at the end. But i can delete that anytime until my code is working
So why isn't your code working?

Does it compile without errors and warnings?

If not what are your compile error and warning messges?

What have you done to try to fix the problems?
it says lines 57 & 68 'Function definition not allowed here' but this was how i was taught to write functions after the return 0; so i dont know why it says its wrong. believe me ive played with it but if i change one thing, 4 different errors will pop up. but i am still working on it as we speak
it says lines 57 & 68 'Function definition not allowed here' but this was how i was taught to write functions after the return 0; so i dont know why it says its wrong.

Check the position of your closing braces. the closing brace at line 77 looks out of place. It probably should be right after the return statement.
ya the reason i added the one at the end was because xcode told me to. ive been playing with the braces but if i add/delete one then i get more errors. hopefully it will work eventually..
Lines 39-43:
<< <<
You need a value between the << and the <<, or remove one of the << operators.

line 45:
while (!health.eof())
Doesn't work the way you're expecting. eof() is only true AFTER you attempt a read.

Line 55: You need a }

Line 56,67: Why are you passing multiple arrays? Each function only uses one array.

line 60: k is undefined. Did you mean i<SIZE ?

line 64: Where is n defined?

Line 77: Remove the }
Last edited on
sorry my mistake. here is my code now. i have only added the mean and max function but i need 4 other arrays passed by functions that i cant figure out .. here is my code

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

using namespace std;

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

int main(void)
{
    int Deaths[SIZE];
    int Doctors[SIZE];
    int Hospitals[SIZE];
    int Income[SIZE];
    int Population[SIZE];
    inputFromFile(Deaths, Doctors, Hospitals, Income, Population);
    
    
    ifstream health("health.txt");  // Open health.txt
    
string firstline;
    
// Read health.txt
    double Death, Docs, Hosp, Inc, Pop;
    
    
    
    
    if (health.is_open())
    {
        getline(health, firstline);
        
        while (!health.eof())
        {
        // Organize health.txt Data
            health >> Death >> Docs >> Hosp >> Inc >> Pop;
        }

    
}

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


    double CalculateMax (const double x[], int[], int[], int[], int[], int[])
    {
        double Max = x[0];
      
        for (int i=1; i<SIZE; i++)
        {
            if (x[i] > Max)
                Max = x[i];
        }
        return Max;
    }
im passing multiple arrays because i need the mean/ max and sum of each category i wrote in my OP. I am probably using functions wrong again.. :/
CalculateMean() and CalculateMax() should operate on any one array.

What happens if you read less than exactly 100 records from the file?
Lines 54, 58, 65 assume you read exactly 100 records.

Consider the following function signature (see options #1 and #2 below):
 
double Calculate_xxx (const double x[], int sz)


line 17-21: you declare arrays of ints.
Line 30: You declare single variables of type double. Which is it?
According to the sample file in your first post, you have a mix of both.
You have a couple of choices:
1) Make all your arrays double.
2) Make the type of the arrays match the type in input file. In this case you will need two versions of your Calculate_xxx functions. One that takes an array of doubles and one that takes an array of ints.

Line 42: You never move the values you read into the various arrays.

Line 22: What is this? It appears to be a function prototype, but I see no corresponding function.

Last edited on
Let's take just one function for now, take CalculateMax().

Why are you trying to pass 6 arrays into this function?

Wouldn't it be easier to call that function multiple times passing different arrays each time? This function should have two parameters, the array and then number of elements in the array.



Last edited on
my professor said to assume the input file has less than 100 cities. So i made my max 100. But i counted them myself and there are exactly 53 but he said to assume less than 100. I tried passing 6 arrays so that i would get a mean,max and sum for all 5 of my categories but i know i probably did that wrong. the function signature that you wrote above. "sz" stands for my SIZE right ? ill try and fix what i have sir. thank you
Pages: 123... 5