Program Help (arrays and functions)

I need some help resolving this program I'm trying to write. Keep in mind that this is for a beginning course in C++, so anything more advanced that using 'struct' is beyond my comprehension. There are many errors and I don't completely understand how to use the arrays as function parameters. Anyway, this is what I have so far. Basically, what I'm trying to do is this:

Use data from .csv (excel file) and put into two arrays (this was done by the instructor in main already) and output to screen the following objectives. All of the functions I did myself trying to do the following:

1. Average age for all runners

2. Average age for female runners only

3. Count of runners from Alabama

4. Count of female runners from Alabama

5. Count of runners over 40 years-old

6. Count of females over 40 years-old

7. Count of runners in the 50k event

8. Count of females in the 50k event

9. Find jim barnett (his name is in lower case in the data) and print out his age (read one row of the array at a time until lastname is == “Barnett”

10. Calculate the median age of all runners. (the exact middle of the data)
--------------------------------------------------------------------------------

I would appreciate it if anyone has time to modify the program to completion, as giving me pointers is not going to help me if there are a lot of errors. Its imperative that I get this completed and I'm tearing out my hair.

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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Header files
#include "stdafx.h"
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>

// Namespace declarations
using namespace std;
using namespace System;

string sarray[300][8]; // hold all the data
int age[300]; // hold the age

// Constant declarations
const int BUFFSIZE = 180; 
const int NUMBER_OF_ROWS = 278;
const int NUMBER_OF_COLUMNS = 8;

char buff[BUFFSIZE]; // a buffer to temporarily park the data 

// Function prototypes
void ageCounter(int age[278]);
void femaleAgeCounter(string sarray[300][8], int NUMBER_OF_COLUMNS);
void runnersCounterForAlabama(string sarray[300][8], int NUMBER_OF_COLUMNS);
void runnersCounterFemalesAlabama(string sarray[300][8], int NUMBER_OF_COLUMNS);
void runnersCounterOverForty(int age[278]);
void runnersCounterFemalesOverForty(string sarray[300][8], int NUMBER_OF_COLUMNS);
void runnersCounterInEvent(int age[278]);
void runnersCounterFemalesInEvent(string sarray[300][8], int NUMBER_OF_COLUMNS);
void barnettAge(string sarray[300][8], int NUMBER_OF_COLUMNS);
void medianAge(int age[278]);

int main(int)
{//top of main
	ifstream infile;
	
	stringstream ss; 

	int r = 0;
	int c = 0;

	infile.open("C:\\runners.csv");

	if (!infile)
	{
		cout << "file did not open and program will terminate" << endl;
		
		system("PAUSE");
		return(1);
	}

   else
	{
		cout << "file opened ok" << endl;

		system("PAUSE");
	}
	
	cout << "start of processing" << endl;
	
		for(r = 0; r <= 278; r++)
		{//top of for r
	
			infile.getline( buff,  BUFFSIZE );

			ss << buff;
	
				for(c = 0; c <= 7; c++)
				{//top of c
		
					ss.getline( buff, 40, ',' );

					sarray[r][c] = buff;
				}//bottom of c

			age[r] = atoi(buff);	
	
			// This copies an empty string into ss, erasing the 
			//  previous contents. 
			ss << "";  

			// This clears the 'eof' flag.  Otherwise, even after  
			//  writing new data to ss we wouldn't be able to 
			//  read from it. 
			ss.clear();
		}//end of for r

			for(r = 0; r <= 278; r++)
			{
				cout << endl;

					for(c = 0; c <= 7; c++)
					{
						cout << sarray[r][c] << " ";
					}

				cout << age[r];
				cout << endl;
			}

	cout << endl;
	cout << "end of processing" << endl;

	// Function calls
	ageCounter(age);
	femaleAgeCounter(sarray, NUMBER_OF_COLUMNS);
	runnersCounterForAlabama(sarray, NUMBER_OF_COLUMNS);
	runnersCounterFemalesAlabama(sarray, NUMBER_OF_COLUMNS);
	runnersCounterOverForty(age);
	runnersCounterFemalesOverForty(sarray, NUMBER_OF_COLUMNS);
	runnersCounterInEvent(age);
	runnersCounterFemalesInEvent(sarray, NUMBER_OF_COLUMNS);
	barnettAge(sarray, NUMBER_OF_COLUMNS);
	medianAge(age);

    system("PAUSE");
    return(0);
}//end of main

// Average age for all runners
void ageCounter(int age[278])
{
	int r;
	int sum = 0;
	int average = 0;

		// Loop to cycle through array and accumulate the ages
		for(r = 0; r < 278; r++)
		{
			sum += age[r];
		}

	average = sum / 278;

	cout << fixed << showpoint << setprecision(2);
	cout << "Average age of all runners is: " << average << endl << endl;
}

// Average age for female runners
void femaleAgeCounter(string sarray[300][8], int NUMBER_OF_COLUMNS)
{
	int r;
	int c = 4;
	int i;

	// Loop to cycle through array for female runners
	for(r = 0; r < 278; r++)
	{
		for(c = 0; c < NUMBER_OF_COLUMNS; c++)
		{
			if (sarray[r][c] == 'f' || sarray[r][c] == 'F')
			{
				i = i + 1;
			}
		}
	}
			
	cout << "The total number of female runners is: " << i << endl << endl;				
}

// Count for runners from Alabama
void runnersCounterForAlabama(string sarray[300][8], int NUMBER_OF_COLUMNS)
{
	int r;
	int c = 3;
	int i;

	// Loop to cycle through array looking for Alabama
	for(r = 0; r < 278; r++)
	{
		if (sarray[r][c] == 'al' ||
			sarray[r][c] == 'AL')
		{
			i = i + 1; // If Alabama is found, accumulate total
		}
	}

	cout << "The total number of runners from Alabama is: "
		 << i << endl << endl;
}

// Count for female runners from Alabama
void runnersCounterFemalesAlabama(string sarray[300][8], int NUMBER_OF_COLUMNS)
{
	int r;
	int i;

	// Loop to cycle through array for females from Alabama
	for(r = 0; r < 278; r++)
	{
		while (sarray[r][3] == 'al' ||
			   sarray[r][3] == 'AL')
		{
			if (sarray[r][4] == 'f' || 
				sarray[r][4] == 'F')
			{
				i = i + 1; // If found, accumulate total
			}
		}

	cout << "The total number of female runners from Alabama is: "
		 << i << endl << endl;
}

// Count for runners over 40-years-old
void runnersCounterOverForty(int age[278])
{
	int r;
	int i;
	int sum = 0;

	// Loop to cycle through array and find runners over 40
	for(r = 0; r < 278; r++)
	{
		while (age[r] > 40)
		{
			i = i + 1; // If over 40, accumulate total
		}
	}

	cout << "The total number of runners over 40 are: " << i << endl << endl;
}

// Count for female runners over 40-years-old
void runnersCounterFemalesOverForty(string sarray[300][8], int NUMBER_OF_COLUMNS)
{
	int r;
	int c;
	int i;

	// Loop to cycle through array and find female runners over 40
	for(r = 0; r < 278; r++)
	{
		for(c = 0; c < NUMBER_OF_COLUMNS; c++)
		{
			if (sarray[r][NUMBER_OF_COLUMNS] == 'f' || 
				sarray[r][NUMBER_OF_COLUMNS] == 'F')
			{
				while (sarray[r][7] > 40)
				{
					i = i + 1; // If found, accumulate total
				}
			}
		}
	}

	cout << "The number of females over 40-years-old is: "
		 << i << endl << endl;
}

// Count for runners in the event
void runnersCounterInEvent(int age[278])
{
	int r;
	int i;
	int sum = 0;

	// Loop to cycle through array and accumulate runners in event
	for(r = 0; r < 278; r++)
	{
		i = i + 1; // accumulate runners after each loop
	}

	cout << "The total number of runners in the event is: " << i << endl << endl;
}

// Count for female runners in the event
void runnersCounterFemalesInEvent(string sarray[300][8], int NUMBER_OF_COLUMNS)
{
	int r;
	int c;
	int i;

	// Loop to cycle through the array and accumulate female runners
	for(r = 0; r < 278; r++)
	{
		for(c = 0; c < NUMBER_OF_COLUMNS; c++)
		{
			while (sarray[r][NUMBER_OF_COLUMNS] == 'f' ||
					sarray[r][NUMBER_OF_COLUMNS] == 'F')
			{
				i = i + 1; // If found, accumulate female runners
			}
		}
	}

	cout << "The number of female runners in the event is: "
		 << i << endl << endl;
}

// Find Jim Barnett and print his age
void barnettAge(string sarray[300][8], int NUMBER_OF_COLUMNS)
{
	int r;
	int c;
	int age;

	// Loop to cycle through array and find the lastname, 'barnett'
	// Once found, retrieve age
	for(r = 0; r < 278; r++)
	{
		for(c = 0; c < NUMBER_OF_COLUMNS; c++)
		{
			if ("barnett")
			{
				age = sarray[r][c + 7]; // Once last name is found,
			}							// retrieve value at the age column
		}
	}

	cout << "Jim Barnett's age is: " << age << endl << endl;

}

// Calculate the median age of all runners
void medianAge(int age[278])
{
	int median = 0;

	median = (age[278/2] + (age[(278 / 2) - 1]) / 2; // Calculation to find the median
													 // within the array
	cout << fixed << showpoint << setprecision(2);
	cout << "The median age is: " << median << endl << endl;
}
Last edited on
Here is an excerpt from the data file to give you an idea of what's happening.

(There are 278 rows, and 8 columns)


aaron,sam,hanceville,AL,M,Large,25k,48
aderhold,mark,cullman,al,m,Medium,25k,51
Adkison,Todd,Vinemont,AL,M,Medium,25k,43
Aikin,John,Florence,AL,M,Large,50k,58
Alexander,Anthony,Killen,AL,M,Medium,25k,56
Allbritton,Bob,Brentwood,TN,M,Medium,25k,51
Amrhein,Randy,dickson,TN,M,Extra Large,50k,67
Apple,Rob,Murfreesboro,TN,M,Extra Large,50k,50
armstrong,brenda,madison,al,f,Medium,25k,46
armstrong,thomas,madison,al,m,extra large,25k,51
Armstrong,Kathy,Grenada,MS,F,Small,25k,49
Armstrong,Rafe,Grenada,MS,M,Medium,25k,49
armstrong,nelson,castalin springs,tn,m,Medium,50k,38
Well, I've not tried to compile this. But glancing at the code, I see
const int NUMBER_OF_ROWS = 278;
and then rather than using this constant, I see the number 278 cropping up repeatedly throughout the code. So, I'd recommend getting rid of the numbers, and use this constant instead.

However - I think it not a good design to assume that the input file will contain precisely 278 lines. I would assign a slightly larger array, say 300 or maybe 500 elements. Then count the number of lines as the data is read from the file.
Something like this.
1
2
3
4
5
6
7
8
9
10
11
12
    string line;
    int rowcount = 0;
    while (getline(infile, line))
    {
        stringstream ss(line);
        
        // here deal with the details from the line 
        // which was just read from the file.
        // store in relevant array etc.
        
        rowcount++;
    }


After that has completed, you will know there are rowcount rows in each array, and that value should be passed as a parameter to any function which needs it.

If all this seems a bit too much to handle, I'd recommend working on one function at a time. You could comment out (with // ) the calls to the other functions. That way, you can focus your attention on one issue at a time, rather than being overwhelmed by the whole thing.
Topic archived. No new replies allowed.