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

In summary: This can lead to silly errors if the values are not correctly cast.In summary, the program will open a file and read in grades from it. It will calculate the number of nonzero integers and the average grade.
  • #1
vladittude0583
40
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
  • #2
fscanf will also read any blank lines and return 0 (non mached)
 
  • #3
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!
 
  • #4
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.
 
  • #5
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.
 

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

What is C+ Programming?

C+ Programming is a high-level, general-purpose programming language commonly used for developing operating systems, video games, and other complex applications. It was created by Bjarne Stroustrup in 1983 as an extension of the C programming language.

Why do I need help with my C+ Programming homework?

C+ Programming can be a complex and challenging subject, especially for beginners. Seeking help with your homework allows you to better understand the concepts and techniques, leading to better grades and a deeper understanding of the language.

What should I look for in a good C+ Programming homework helper?

A good C+ Programming homework helper should have a strong understanding of the language and its concepts. They should also be experienced in writing efficient and error-free code, have good communication skills, and be able to provide clear explanations and feedback.

How can I ensure the quality of the C+ Programming homework help I receive?

To ensure the quality of your C+ Programming homework help, you can ask for samples or reviews from previous clients. You can also check the qualifications and experience of the helper and communicate your expectations and requirements clearly.

Is it ethical to seek help with my C+ Programming homework?

Yes, seeking help with your C+ Programming homework is ethical as long as you use the help to improve your understanding and skills in the language. It is important to not plagiarize or submit someone else's work as your own.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
Back
Top