Trimming the fat out of my code for an Array
Hi all,
The problem I have got is a file which has a set of date like this:
F 26 5
F 64 4
F 29 2
M 12 3
M 40 1
F 38 4
The first column represtents Male or Female the second Age and the third is a Selection. What I have to do is populate an array so all females under 25 who answered one are added together, all females under 25 who answered two are added together, so on, and so on.
Now I could do something like this (note, I just have it outputting to the screen until I have sorted my code out):
Code:
{
while (fscanf(rawdat, "%c %d %d &c", &sex, &age, &range, &newline) != EOF)
if (((sex == 'F') && (age < 25) && (range == 1)))
printf("%c %d %d\n", sex, age, range);
else if (((sex == 'F') && (age < 25) && (range == 2)))
printf("%c %d %d\n", sex, age, range);
etc......
etc......
}
Now this is wildly inefficient code, but it would work. What I am thinking of doing is using a switch statement for the range using a preprocessor directive to fill rows of the array then use the sex and age to fill the columns of the array. The problems I have with this is that I'm declaring the two co-ordinants of the array in separate places and I'm not sure how to do this. Am I barking up the wrong tree with this solution? Is there a better way?
I hope this makes sense as it's been bugging the hell out of me