- #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!
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!