How to Correctly Count Non-Zero Integers in a C Program?

Click For Summary
The discussion focuses on a C program designed to read integer grades from a file and count non-zero integers while calculating their average. The user encounters an issue where the program incorrectly counts 25 non-zero integers instead of the expected 22. Suggestions include verifying the data file for non-integer values, ensuring that the variable types match the format specifiers in `fscanf`, and using print statements to debug the values being read. It is emphasized that the program should handle potential blank lines and non-numeric data correctly. Correcting these issues will help achieve accurate results in counting non-zero integers.
vladittude0583
Messages
40
Reaction score
0
Here is the homework we have to do:

Write a program that will read a series of integer grades in the range 0-100 from a file, and display the number of nonzero grades read and the average grade. Here is a test data file (grades.dat) and a sample executable (grades1.exe). For the executable to work, the test data file must be saved to c:\temp.

Here is what I have coded so far:


#include <stdio.h>

int main()
{

char namefile[200]; /* name of file to open limited
to 200 characters */
FILE *access; // streamName

int nonZero = 0;
double total = 0.0;
double nextVal;
double value;

printf("Hello, this program is designed to open a file \n"
"at a specified directory and open it to calculate \n"
"the number of non-zero integers and also the average \n"
"of those integers! \n\n");

printf("What file would you like to open and calculate? ");
scanf("%s", namefile);

access = fopen(namefile, "r");

if ((access = fopen(namefile, "r")) == NULL)
{
printf("\nThat file does not exist! \n");
printf("The program will now be terminated. \n");


system("pause");
return 1;
}

while (fscanf(access, "%d", &value) != EOF)
{
if (value != 0)
{
nonZero++;
printf("you have %d non-zero integers", nonZero);
}

}
fclose(access);



system("pause");
return 0;
}

I cannot seem to get the "while" section to work and give me the correct number of non-zero integers. Our teacher gave us an executable file and I know that there is supposed to be 22 non-zero integers, but my program keeps coming up with 25 which I am assuming is the amount of integers in the file we are opening, therefore, there must be three zero integers. Could you please examine my code and tell me what is wrong with my calculating the "non-zero" integer portion? Please I am desperate here!
 
Physics news on Phys.org
fscanf will also read any blank lines and return 0 (non mached)
 
Well, to be a good programmer you need to learn the tricks.. one trick is to print the value that you read.. so..
nonZero++;
printf("you have %d non-zero integers", nonZero);

would be:
printf("Incrementing from %d to %d, the value read was %d\n", nonZero, ++nonZero, value);
system("pause"); // this can be getch() I think

So now you can go through and see if you are really adding up zeroes.. also you can do something like:

else {
printf("Looks like we got a 0!");
system("pause");
}

To see if your if is doing anything at all!
 
printf("Incrementing from %d to %d, the value read was %d\n", nonZero, ++nonZero, value);
That's generally a really bad idea. C++ doesn't define what happens if you do i and i++ in the same function call - the compiler is free to order the calls how ever it wants.
In practice a printf() call is unlikely to change the order - but in general function(i,i++) leads to some really interesting bugs, especially since the order may be differen tin realease and ebug builds.
 
First, I suggest you verify the content of the data file to see if they are integers or floating points (they are supposed to be integers).
You want to know if there are other non numeric data in the data that could cause problems.
If you want, you can post the data file or a portion of it as well, so we will know if the integers are on the same line, or on separate lines, whether there are blank lines in between, are there commas as serparators, etc.

One thing you may want to correct, although I am not sure if it will solve your problem:
the variable type must match the printing template, in your code:
> double value;
> ...
> while (fscanf(access, "%d", &value) != EOF)
Value was defined as double, but was read in as integer.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
Replies
1
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 21 ·
Replies
21
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K