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

Click For Summary

Discussion Overview

The discussion revolves around a homework assignment involving a C program designed to read integer grades from a file and count the number of non-zero integers while calculating the average. Participants are examining issues related to the implementation of the program, particularly focusing on the counting of non-zero integers and potential errors in the code.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares their code and expresses difficulty in correctly counting non-zero integers, suspecting that the program is counting zeros incorrectly.
  • Another participant notes that the use of fscanf may read blank lines, which could affect the count of non-zero integers.
  • A suggestion is made to print the value read during the counting process to help debug the issue, along with a warning about the potential pitfalls of modifying a variable within a printf statement.
  • Concerns are raised about the variable type mismatch, where a double type is used for reading integers, which could lead to unexpected behavior.
  • A participant suggests verifying the contents of the data file to ensure that all entries are indeed integers and to check for any non-numeric data that could cause issues.

Areas of Agreement / Disagreement

Participants express various concerns and suggestions, but there is no consensus on the exact cause of the issue or a definitive solution. Multiple competing views on debugging strategies and potential code corrections are presented.

Contextual Notes

There are unresolved questions regarding the format of the data file and how it may affect the program's behavior. The discussion highlights potential issues with variable types and input handling that remain to be clarified.

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 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
Replies
1
Views
2K
  • · Replies 21 ·
Replies
21
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
9
Views
2K