# Thread: Homework help (again)... So simple, its hard!

1. ## Homework help (again)... So simple, its hard!

Hey all again... Ready for this one??
I need to read from an external file called data3.dat, which I inputted....

Code:
```0  3
0  6
1  10
1  8
1  11
0  9
1  30
1  7
1  8```
If there is a 0 (column 1), ignore the corresponding number next to it (colunm 2). If there is a 1 in column 1, find the average of all the numbers in column 2 that correspond to 1's in column one. So basically, take the average of all the numbers that have a 1 in front of them. Then find the max. I am okay up to right here... here is my code;

Code:
```/*  Homework 14 */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void)

{
int flag, x, count=0, j=1, flag1=0, max;
float sum=0.0, ave;
FILE *data;

data = fopen("data3.dat", "r");

while(fscanf (exam, "%i %i", &flag, &x )==2)
{
if (flag==1)
{
sum = sum + x;
++count;
if (flag == 0)
{
max=x;
flag1 = 1;
}
if (x>max)
{
max = x;
}
}
}
ave = sum/count;

printf("Average = %5.2f \n", ave);
printf("Max = %2i \n", max);
return 0;
}```
Now I have to print out all the locations of the max value in column 2. If there is more than 5 max values, print "More than five maxima. \n"

Help me please, I am not sure where to start!!

2. Now I have to print out all the locations of the max value in column 2. If there is more than 5 max values, print "More than five maxima. \n"
What do you mean by locations? line number or what? ur address?

Code:
```      if (flag==1)
{
sum = sum + x;
++count;
if (flag == 0)         // can this happen?
{
max=x;
flag1 = 1;
}
if (x>max)
{
max = x;
}
}```

3. Oh sorry! Yes, location == line number.

For the,
Code:
`  if (flag == 0)`
it works. The code I posted above compiles and executes correctly. I know it is really weird code, my prof is super old school.
The problem I am working on, printing out the line number of up to 5 max locations, it is just adding onto this existing program.

4. It executes correctly, but that's because you have it correct later on. The flag==0 part can never execute, no matter what. Fortunately, you reduplicate the x>max test again, because otherwise you'd never get it set.

EDIT: And to answer the question, that's where you need to keep track of your line number, right there where you set the max. Unfortunately you haven't been keeping track of the line number (just the number of times flag==1), so you're going to have to start.

5. tabstop, I'm not quite sure what you mean about keeping track of the line number.

6. Originally Posted by Aslan14
tabstop, I'm not quite sure what you mean about keeping track of the line number.
I don't know what to tell you; it's not a euphemism or anything. You need to keep track of the line number. For instance, when you find the average, you need to divide by the number of numbers you added together. How do you know what it is, since it could change from one run of the program to the next. You kept track of it, is how, in the variable "count".

7. Ok, I understand now. Do you think that I would want to call each location of the max a different name? For instance;
Code:
`int locmax1, locmax2, locmax3;`
up to 5 locations?

8. It depends on how you write it.

9. You have some braces messed up. You have a check for (flag == 1) inside a conditional block where (flag == 0). Re-indent your code and check your bracing. I haven't looked too closely, but you probably just have a structural problem.

If you're having trouble with the indentation concept, let the IDE do it for you (or use an indentation tool) and go from there.

The indentation is important to humans, even if not to the compiler. The braces even more so.

10. This is what I have so far.. not coming out correct, but am I at least headed in the right direction?
Code:
```/*  Homework 14 */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void)

{
int flag, x, count=0, j=1,k=1, m=1, n=1, i=1, flag1=0, max, locmax, locmax2, locmax3, locmax4, locmax5;
float sum=0.0, ave;
FILE *exam;

exam = fopen("exam3.dat", "r");

while(fscanf (exam, "%i %i", &flag, &x )==2)
{
if (flag==1)
{
sum = sum + x;
++count;

if (flag1 == 0)
{
max=x;
locmax=j;
locmax2=k;
locmax3=m;
locmax4=n;
locmax5=i;
flag1 = 1;
}
if ( x > max )
{
max = x;
locmax = j;
}

if (x == max )
{
locmax = j;}
if (x == locmax){
locmax2 = k;}
if (x ==locmax2){
locmax3 = m;}
if (x == locmax3){
locmax4 = n;}
if ( x == locmax4){
locmax5 = i;
}
}
++j;
++k;
++m;
++n;
++i;

}
ave = sum/count;

printf("Average = %7.2f \n", ave);
printf("Max = %2i \n", max);
printf("Location of max %2i \n", locmax);
printf("Location of max %2i \n", locmax2);
printf("Location of max %2i \n", locmax3);
printf("Location of max %2i \n", locmax4);
printf("Location of max %2i \n", locmax5);
return 0;
}```

11. I think that should do the trick.
Code:
```   int i = 0;
int line_no = 0;
int max_loc[5];
while(fscanf (exam, "%i %i", &flag, &x )==2) {
line_no++;
.....
if (x>max)
{
max = x;
i = 0;
max_loc[i++] = line_no;
} else if(x == max) {
if(i < 5)
max_loc[i++] = line_no;
}```

12. Sorry brewbuck, my prof doesn't indent at all, I will fix it. As far as this part;
Code:
``` if (flag==1)
{
sum = sum + x;
++count;

if (flag1 == 0)```
I need to add an open bracket in between the two if statements?

13. Let's indent properly.
Code:
```if (flag == 1)
{
sum = sum + x;
++count;

if (flag1 == 0)
{
/* and so on */
}
/* .... */
}```
Tell me, following the code, does it still work? Probably not, at least, not because of the second if. The first if must be true for the second if to be evaluated.

14. It's a bit like learning a new language (well, okay, it's exactly like learning a new language) but if you're going to get very far you'll need to learn how to "read" C. Look at this bit o' code:
Code:
```if (flag == 0)
{
max=x;
flag1 = 1;
}
if (x>max)
{
max = x;
}```
You should recognize the structures -- if (this) then do the bit in curly braces. You may also notice that the two curly brace parts are nearly the same (except for the bit about flag1, which doesn't ever appear before or after so it can probably be ignored). So these two if-statements do the same thing. Now look at the conditions themselves: the first is flag==0. BUT, this whole bit of code is embedded in a "if flag==1" thing; so this never happens. The second condition, x > max, can possibly happen; and if you think about it, is (almost) exactly the condition you want: if the current x is bigger than the max x, well then we've got a new max in town. The additional part you'll need to do here is keep track of where we are in the file, which is what you needed a line_number for. Once you've got a line_number working, then we can worry about doing locations.

15. Whiteflags, I see what you are saying. I'm going to work on it for a bit then repost.