Multidimensional Array From Data File

It compiles just fine, but when I run it, the output shows "The tallest female is 0x22febc" and "The lightest female is 0x22feb8".

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
  #include <iostream>
#include <fstream>
#define FILE_IN "heightweight.dat"
using namespace std;

void TallestandLightest (int[][2], int*, int*);

int main ()
{
    char exit;
    cout << "This program finds the tallset female and lightest female from a"; 
    cout << endl;
    cout << "given data set.";
    cout << endl;
    cout << endl;
    int countera, counterb, hwarray[18][2], height, weight;
    char gender[18];
    ifstream input;
    input.open (FILE_IN, ios::in);
    input.getline (gender, 18);
    countera = 0;
    counterb = 0;
    for (countera = 0; countera < 18; ++countera)
    {
        input >> hwarray[countera][counterb];
    }
    countera = 0;
    counterb = 1;
    for (countera = 0; countera < 18; ++countera)
    {
        input >> hwarray[countera][counterb];
    }
    input.close();
    TallestandLightest (hwarray, &height, &weight);
    cout << "The tallest female is " << &height <<" inches tall.";
    cout << endl;
    cout << "The lightest female is " << &weight << " pounds.";
    cout << endl;
    cout << endl;
    cout << "Press any key to exit the program. ";
    cin >> exit;
}

void TallestandLightest(int hwarray[][2], int *h_ptr, int *w_ptr)
{
     int counta, countb;
     float tallest = hwarray[0][0]; 
     float lightest = hwarray [0][1];
     counta = 0;
     countb = 0;
     for (counta = 0; counta < 18; ++counta)
     {
         if (hwarray[counta][countb] > tallest) *h_ptr=hwarray[counta][countb];
     }
     counta = 0;
     countb = 1; 
     for (counta = 0; counta < 18; ++counta)
     {
         if (hwarray[counta][countb]<lightest) *w_ptr=hwarray[counta][countb]; 
     }
}
Last edited on
Remove the "&" before height and weight lines 35 and 37.
OK, that part worked, but now the array is only grabbing values from the first column of the data file, and not even the ones I'm looking for. Here is the code, revised with toum's help:
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
#include <iostream>
#include <fstream>
#define FILE_IN "heightweight.dat"
using namespace std;

void TallestandLightest (int[][2], int*, int*);

int main ()
{
    char exit;
    cout << "This program finds the tallset female and lightest female from a"; 
    cout << endl;
    cout << "given data set.";
    cout << endl;
    cout << endl;
    int countera, counterb, hwarray[18][2], height, weight;
    char gender[18];
    ifstream input;
    input.open (FILE_IN, ios::in);
    input.getline (gender, 18);
    countera = 0;
    counterb = 0;
    for (countera = 0; countera < 18; ++countera)
    {
        input >> hwarray[countera][counterb];
    }
    countera = 0;
    counterb = 1;
    for (countera = 0; countera < 18; ++countera)
    {
        input >> hwarray[countera][counterb];
    }
    input.close();
    TallestandLightest (hwarray, &height, &weight);
    cout << "The tallest female is " << &height <<" inches tall.";
    cout << endl;
    cout << "The lightest female is " << &weight << " pounds.";
    cout << endl;
    cout << endl;
    cout << "Press any key to exit the program. ";
    cin >> exit;
}

void TallestandLightest(int hwarray[][2], int *h_ptr, int *w_ptr)
{
     int counta, countb;
     float tallest = hwarray[0][0]; 
     float lightest = hwarray [0][1];
     counta = 0;
     countb = 0;
     for (counta = 0; counta < 18; ++counta)
     {
         if (hwarray[counta][countb] > tallest) *h_ptr=hwarray[counta][countb];
     }
     counta = 0;
     countb = 1; 
     for (counta = 0; counta < 18; ++counta)
     {
         if (hwarray[counta][countb]<lightest) *w_ptr=hwarray[counta][countb]; 
     }
}

Oh, and just in case, these are the values of the .dat file I'm taking these from:

63 111
72 165
60 123
61 164
71 200
65 136
58 107
59 118
62 119
70 160
71 161
72 152
67 173
65 134
61 135
65 149
64 154
60 133
Last edited on
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>
#define FILE_IN "heightweight.dat"
using namespace std;

void TallestandLightest(int[][2], int*, int*);

int main()
{
	char exit;
	cout << "This program finds the tallset female and lightest female from a";
	cout << endl;
	cout << "given data set.";
	cout << endl;
	cout << endl;
	int countera, counterb, hwarray[18][2], height, weight;
	char gender[18];
	ifstream input;
	input.open(FILE_IN, ios::in);
	if (input.fail())
		cout << "File did not open";

	input.getline(gender, 18);
	countera = 0;
	while (input&&countera<18)
	{
		counterb = 0;
		input >> hwarray[countera][counterb];
		input >> hwarray[countera][counterb + 1];
		cout << hwarray[countera][counterb] << "      " << hwarray[countera][counterb + 1] << endl;
		countera++;
	}

	input.close();
	TallestandLightest(hwarray, &height, &weight);
	cout << "The tallest female is " << height << " inches tall.";
	cout << endl;
	cout << "The lightest female is " << weight << " pounds.";
	cout << endl;
	cout << endl;
	cout << "Press any key to exit the program. ";
	cin >> exit;
}

void TallestandLightest(int hwarray[][2], int *h_ptr, int *w_ptr)
{
	int counta, countb;
	int tallest = hwarray[0][0];
	int lightest = hwarray[0][1];
	counta = 0;
	countb = 0;
//LOOK HERE
	for (counta = 0; counta < 18; counta++)
	{
		if (hwarray[counta][countb] > tallest)
		{
			tallest = hwarray[counta][countb];
			*h_ptr = hwarray[counta][countb];

			
		}
	}
	counta = 0;
	countb = 1;

//LOOK HERE
	for (counta = 0; counta < 18; ++counta)
	{
		if (hwarray[counta][countb] < lightest)
		{
			lightest = hwarray[counta][countb];
			
			*w_ptr = hwarray[counta][countb];
			
		}
	}
}


You didn't set the new values for lightest and tallest during your linear search. Without doing that all this is doing is assigning a value higher than tallest to your *h_ptr variable instead of the tallest;the same goes with weight.
Last edited on
Everything works just great, but this is an error that is mystifying me, simply because it makes no sense, and I don't think anything in my code allows for it.
The value for lightest it constantly "18" even though 18 is not a value I am drawing from in the .dat file. It should be 107.

The 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
 int main ()
{   
    char exit;
    cout << "This program finds the tallset female and lightest female from a"; 
    cout << endl;
    cout << "given data set.";
    cout << endl;
    cout << endl;
    int countera, counterb, hwarray[18][2], height, weight;
    char gender[18];
    ifstream input;
    input.open (FILE_IN, ios::in);
    input.getline (gender, 18);
    for (countera = 0; countera < 18; ++countera)
    {
        for (counterb = 0; counterb < 2; ++counterb)
        {
            input >> hwarray[countera][counterb];
        }
    }
    input.close();
    TallestandLightest (hwarray, &height, &weight);
}


void TallestandLightest(int hwarray[][2], int *h_ptr, int *w_ptr)
{
     //code for finding height is above here
     counta = 0;
     countb = 0; 
     for (counta = 0; counta < 18; ++counta)
     {
         if (hwarray[counta][countb]<lightest) 
         {
            lightest = hwarray[counta][countb];
            *w_ptr = hwarray[counta][countb];
         } 
     }
}


And this is what the .dat file looks like exactly when opening the file in Notepad:

FEMALE
18
63 111
72 165
60 123
61 164
71 200
65 136
58 107
59 118
62 119
70 160
71 161
72 152
67 173
65 134
61 135
65 149
64 154
60 133
Last edited on
i see 18 in the .dat file you posted right after FEMALE.
True, but isn't that what
 
char gender[18];

is using to find the actual data?
"FEMALE" will be stored in gender, not 18. 18 appears to be the number of records stored in the file.
So should I just delete 18 from the data file?
Topic archived. No new replies allowed.