0

I'm trying to calculate the size of the file . The process I've followed is to read the file and store it in an array and calculate its size. However,I really don't know ... I tried n number of ways..I've to pass this size as an attribute to the frequency function.along with the name of the array.

#include <stdio.h>
#include<conio.h>

void  frequency (int theArray [ ], int ??????, int x)
{
    int count = 0;
    int u;

    for (u = 0; u < ??????; u++)
    {
        if ( theArray[u]==x)
        {
            count = count + 1 ;
            /*printf("\n%d",theArray[u]);*/ 
        }      
        else
        {
            count = count ;
        } 
    }
    printf ("\nThe frequency of %d in your array is %d ",x,count);
} 

void main()
{
    FILE*file = fopen("num.txt","r");
    int integers[100];
    int i=0;
    int r = 0;
    int num;
    int theArray[100];
    int there[100];
    int n;
    int g;
    int x;
    while(fscanf(file,"%d",&num)>0)
    {
        integers[i]=num;
        printf("\n%d",(integers[i]));
        there[r] = integers[i];
        i++;
    }
    //printf("%d",there[r]);

    //printf("\n%d",file);

    //fclose(file);

    printf ("\n OK, Thanks! Now What Number Do You Want To Search For Frequency In Your Array? ");
    scanf("\n%d", &x);/*Stores Number To Search For Frequency*/
    frequency(integers,????????,x);

    getch();
    fclose(file);
}

?????? is the size of the integer array from where i read the file and stored it.

I could not find a way to calculate the size of the array into which i copied my file. My idea is to calculate the frequency of a number in that file and calculate the probability of it's occurrence and thereby calculating entropy..Suggestions please!

nem035
  • 34,790
  • 6
  • 87
  • 99
  • I don't know is there any function to calculate length of an array but you can initialize all element to -1 and then copy all data and at last check till -1. And also for good coding you need separate track of each and every character so that it must distinct. – Avinash Sep 21 '14 at 05:28
  • check this http://stackoverflow.com/questions/8236/how-do-you-determine-the-size-of-a-file-in-c – nem035 Sep 21 '14 at 05:31
  • 1
    call `frequency(integers, i, x);` – BLUEPIXY Sep 21 '14 at 05:40
  • Note that this is related to another question by [rishanth chavali](http://stackoverflow.com/users/3992956/rishanth-chavali), namely [Unable to convert `(int *)` to `int`](http://stackoverflow.com/questions/25955175/unable-to-convert-int-to-int). – Jonathan Leffler Sep 21 '14 at 06:10

3 Answers3

0

There are a lot of parts of this code that don't make sense, but I assume it is your debugging trying to figure out what is wrong. The answer to your specific question is:

For each value read from the file you set integers[i] to the value and then increment i. Thus i is the count of items in integers. You then pass integers to frequency(), so i should be passed to the second parameter as the count.

Note that if there are more than 100 values in the file, you will over index integers and cause unpredictable behavior.

Ron Struempf
  • 146
  • 4
0

I don't know why you are initializing so many variables and some of them with awkward names like ??????.

Your main problem is that the call to function should be

frequency(integers, i, x);

Your code with the awkward irrelevant parts removed will look like

#include <stdio.h>
#include<conio.h>

void  frequency (int theArray [ ], int number, int x)
{
    int count = 0;
    int u;

    for (u = 0; u < number; u++)
    {
        if ( theArray[u]==x)
            count++;
    }
    printf ("\nThe frequency of %d in your array is %d ",x,count);
} 

void main()
{
    FILE*file = fopen("num.txt","r");
    int integers[100];
    int i=0;
    int num;
    int x;
    while(fscanf(file,"%d",&num)>0)
    {
        integers[i]=num;
        printf("\n%d",integers[i]);
        i++;
    }

    printf ("\n OK, Thanks! Now What Number Do You Want To Search For Frequency In Your Array? ");
    scanf(" %d", &x);/*Stores Number To Search For Frequency*/
    frequency(integers,i,x);

    getch();
    fclose(file);
}
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
0

To calculate length of array:

int len= sizeof(arr)/sizeof(arr[0]);

It will give length of array without looping.

Pratyush Khare
  • 689
  • 5
  • 16