# Thread: simple program, simple error? HELP!

1. ## simple program, simple error? HELP!

Hello all. I am a beginning programmer, taking my first programming class in college this semester. I have a program that

it's supposed to:
first: read the data from an outside file, data1.dat and find the average of all the entries in column 3 when the number in column1=1, or ignore the row if column1=0

or

find the maximum value of column 3 if column2=1
find the minimum value of column 3 if column2=0

it has some problem in the code, and when asked, my professor could not determine what it is. I'm kind of at a loss on this one.
the program I wrote is as follows:

Code:
```/* Homework 6 */

#include <stdio.h>
#include <math.h>
#define FILENAME "data1.dat"

int main()

{

/* declare variables */

int points=0;
int c1, c2;
double c3, ave, max, min, sum=0.0;
int flag0=0, flag1=0;
FILE *list;

/* open file */

list = fopen(FILENAME, "r");

while ((fscanf(list, "%i, %i, %lf", &c1, &c2, &c3)) == 3)

{

/* compute the average, minimum and maximum */

if (c1 == 1)
{
sum += c3;
points++;

if (c2 == 0)
{
if (flag0 == 0)

flag0 = 1;
min = c3;

if (c3 < min) min = c3;
}

if (c2 == 1)
{
if (flag1 == 0)

flag1 = 1;
max = c3;

if (c3 > max) max = c3;
}
}
}

/* compute ave */

ave=sum/points;

/* print a summary */

printf("average value: %7.2f \n", ave);

fclose(list);

return 0;

}```
any help is GREATLY appreciated.

Code:
```/* Homework 6 */

#include <stdio.h>
#include <math.h>
#define FILENAME "data1.dat"

int main()

{

/* declare variables */

int points=0;
int c1, c2;
double c3, ave, max, min, sum=0.0;
int flag0=0, flag1=0;
FILE *list;

/* open file */

list = fopen(FILENAME, "r");

while ((fscanf(list, "%i, %i, %lf", &c1, &c2, &c3)) == 3)

{

/* compute the average, minimum and maximum */

if (c1 == 1)
{
sum += c3;
points++;

if (c2 == 0)
{
if (flag0 == 0)

flag0 = 1;
min = c3;

if (c3 < min) min = c3;
}

if (c2 == 1)
{
if (flag1 == 0)

flag1 = 1;
max = c3;

if (c3 > max) max = c3;
}
}
}

/* compute ave */

ave=sum/points;

/* print a summary */

printf("average value: %7.2f \n", ave);

fclose(list);

return 0;

}```
do you see some problems now?

3. I have been working on this all evening, but still can't get the file to read in correctly. I know you guys probably hate beginners like myself taking up space on these bords, but I'm stumped.

Code:
```/* Homework 6 */

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define FILENAME "data1.dat"

int main()

{

/* declare variables */

int points=0;
int c1, c2;
double c3, ave, max, min, sum=0.0;
int flag0=0, flag1=0;
FILE *list;

/* open file */

list = fopen(FILENAME, "r");

if(list == NULL)
{
}
else
{

while(!feof(list))
{
fscanf(list, "%i, %i, %lf", &c1, &c2, &c3);

/* compute the average, minimum and maximum */

if (c1 == 1)
{
printf("%i, %i, %lf \n", c1, c1, c3);
sum += c3;
points++;
printf("sum: %lf \n", sum);
printf("points: %i \n", points);

if (c2 == 0)
{
if (flag0 == 0)
{
flag0 = 1;
min = c3;
}
if (c3 < min) min = c3;
}

if (c2 == 1)
{
if (flag1 == 0)
{
flag1 = 1;
max = c3;
}
if (c3 > max) max = c3;
}
}
}
}

/* compute ave */

if(points == 0)
{
printf("error, cannot divide by zero \n");
}
else
{
ave=sum/points;

/* print a summary */

printf("average value: %7.2f \n", ave);

}

fclose(list);

return 0;

}```
I also tried while((fscanf(list, "%i, %i, %lf", &c1, &c2, &c3)) == 3)
and while((fscanf(list, "%i, %i, %lf", &c1, &c2, &c3)) !=EOF)

with similar results. my conclusion is the file is not being read in correctly, I think It would return correct numbers if I could get it to read the file.

input file consists of:

data1.dat:
1 0 3.4
0 0 1.3
1 1 3.1
1 0 0.4
1 1 7.9
1 1 4.2
1 0 -2.1

4. 1. your indentation once gain suxxes - havn't you seen what problems it could make? (in VS Ctrl+A, Alt+F8)
2. your format does not match your data - there is no , between numbers
3. there is warning about using notinitialized max,min vars - to fix them initialize these vars
4. do not use feof to control loop - there is a FAQ which explin why

Code:
```/* Homework 6 */

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define FILENAME "data1.dat"

int main()

{

/* declare variables */

int points=0;
int c1, c2;
double c3, ave, max = -1, min = -1, sum=0.0;
int flag0=0, flag1=0;
FILE *list;

/* open file */

list = fopen(FILENAME, "r");

if(list == NULL)
{
}
else
{

while(fscanf(list, "%i %i %lf", &c1, &c2, &c3) == 3)
{

/* compute the average, minimum and maximum */

if (c1 == 1)
{
printf("%i, %i, %lf \n", c1, c1, c3);
sum += c3;
points++;
printf("sum: %lf \n", sum);
printf("points: %i \n", points);

if (c2 == 0)
{
if (flag0 == 0)
{
flag0 = 1;
min = c3;
}
if (c3 < min) min = c3;
}

if (c2 == 1)
{
if (flag1 == 0)
{
flag1 = 1;
max = c3;
}
if (c3 > max) max = c3;
}
}
}
}

/* compute ave */

if(points == 0)
{
printf("error, cannot divide by zero \n");
}
else
{
ave=sum/points;

/* print a summary */

printf("average value: %7.2f \n", ave);

}

fclose(list);

return 0;

}```

5. Thank you, vart. it was as simple as two misplaced commas.

Thanks also for a lesson on indenting. My professor doesn't indent at all, he pretty much just threw us to the wolves. It makes it way easier to read with the tab indentations.
My prof actually told us *not* to use tab, because it wouldn't work in the compiler. I guess I'll never trust him on stuff again, because obviously it worked.

Thanks again!