So I need to read in a file of integers between 0 and 9 and
So I need to read in a file of integers between 0 and 9 and use that data to count how many integers were read in and how often each number occurs. I don\'t quite understand how to read in the file that we prompt the user to enter, then be able to count how many occurances there are of each number. (program in c)
Solution
#include<stdio.h>
int main()
{
//file handle to read data from file containing numbers
FILE *in;
//array to store number of occurances of numbers 0-9
int arr_ocuurance[10]={0},i=0,num,j=0;
in = fopen(\"num.txt\",\"r\");
if( in == NULL )
{
printf(\"unableto open file for reading\ \");
return -1;
}
while(!feof(in))
{
fscanf(in,\"%d\",&num);
++arr_ocuurance[num%10] ;
i++;
}
for(j = 0 ; j < 10 ; j++)
{
printf(\"number %d has occured %d times in the file\ \",j,arr_ocuurance[j]);
}
}
