Hi, I have written a c code that finds euclidean distance between corresponding elements of two 2D arrays. i have taken two text files like this..

9.0444 0.083404 -0.30599 -0.21367 -0.032527 -0.048789 -0.17683 -0.10523 0.050547 -0.028377 -0.06061 -0.0011242 -0.031838
9.164 0.18631 -0.3277 -0.1631 -0.014165 -0.041347 -0.063973 -0.059217 0.1422 -0.076865 -0.22507 -0.24214 -0.069154
9.1688 0.27382 -0.21322 -0.21558 -0.20094 -0.2459 -0.21811 -0.22654 -0.093443 -0.14375 -0.17468 -0.17559 -0.079262
8.9909 0.13161 -0.23827 -0.15121 -0.062505 -0.1702 -0.12139 -0.17619 0.035838 -0.089756 -0.17788 -0.089365 0.076777
8.9626 0.006085 -0.20092 -0.21606 -0.010768 -0.089176 -0.12807 -0.073935 0.081079 0.050701 -0.13336 -0.12325 -0.11199

Number of columns are fixed i.e 13 but rows can vary in millions. code is working well with small text files. But it is not working with large text files due to array size. i need to define array dynamically. but i don't know how to do it. This is what i have done so far..

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
    int i=0,j,k,x,count=0,n=0,m=13,p=0,q=13;
    float dis=0.0,s=0.0;
    float avg[100],a[100][100],b[100][100];
    //double dis=0,s=0,avg[100];
    char buf[BUFSIZ];
    FILE *fp1,*fp2,*fp3;
    //clrscr();
    fp1=fopen("a1.txt","r");
    fp2=fopen("a2.txt","r");
    //fp3=fopen("ouput.txt","w");
    while (fgets(buf,sizeof(buf),fp1) != NULL)
    n++; // counting rows of a1.txt

    while (fgets(buf,sizeof(buf),fp2) != NULL)
    p++;    // counting rows of a2.txt
    rewind(fp1);

    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            fscanf(fp1,"%f",&a[i][j]);
            printf("%f ",a[i][j]);
        }
        printf("\n");
    }
    printf("\n");
    rewind(fp2);
    for(i=0;i<p;i++)
    {
        for(j=0;j<q;j++)
        {
            fscanf(fp2,"%f",&b[i][j]);
            printf("%f ",b[i][j]);
        }
        printf("\n");
    }
          printf("\n");


    //calculation part

     for(x=0;x<=(n-1);x++)   
     {
     if(count!=(n-p)+1){
        i=x;k=0;
        while(i<(p+x))
        {
            for(j=0;j<q;j++)
            {
                s=s+pow((a[i][j]-b[k][j]),2.0);
                dis=sqrt(s);
            }
            i++;
            k++;

        }
        count++;
        avg[i]=dis;
        printf("\n\n%f\n",avg[i]);

        s=0;
    }
     else
     break;
    fprintf(fp3,"%f\n",avg[i]);

 }
    //getch();
    fclose(fp1);
    fclose(fp2);
    fclose(fp3);
}

Any help is appriciated...Thank you.

Dynamic Array can be allocated with the help of malloc().

Example :
int *p;
p = (int *) malloc ( sizeof(int) * 10 ) ; // array of int of size 10

can you please elaborate this answer in detail..

what malloc does is basically frees you from having to think too hard about what the size of your array should be during its definition , as it lets you modify memory space on the fly.this and this should get you started.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.