1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

need a float type array of 100000 elements!!!

Discussion in 'C' started by Ironmaniac, Sep 4, 2009.

  1. Ironmaniac

    Ironmaniac New Member

    Joined:
    Sep 4, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    How can one declare a float or double type array of 100000 elements (say)??
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    If your stack is big enough:
    Code:
    double x[100000];
    
    If not you'll have to do it on the heap instead:
    Code:
    double *x=new double[100000];
    //...
    delete[] x;
    
     
  3. Ironmaniac

    Ironmaniac New Member

    Joined:
    Sep 4, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Thanks!! But this looks like C++....I need it in C.
     
  4. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    If you plan to use Dynamic Memory Allocation,

    (*) in C :
    Code:
    float *x;
    x = (float *)malloc(100000 * sizeof(float) );
    ...
    free(x);
    (*) in C++ :
    Code:
    float *x;
    x = new float[100000];
    //...
    delete [] x;

    But, if you have an ANSI/ISO C++ specific compiler, you can declare such an array, without using Dynamic Memory Allocation :
    Code:
    #include <vector>
    std::vector<float> x(100000);
     
  5. Ironmaniac

    Ironmaniac New Member

    Joined:
    Sep 4, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0


    I want to use this way of doing things, Saswath....now how do I couple it with my code?
    The code being.....
    Code:
     #include<stdio.h>
    #include<conio.h>
    
    
    void main()
    
    {
        clrscr();
        int j=101,y=j-1,z,p;
            int k,m,l,i,g,n,q,a;
            int r,t;
            float s,sd1,avg;
            float mavg[1000],sds[1000],sds1[1000],sd[1000],A[]={126332363.4610    ,
    126332295.1810    ,
    126332226.8900    ,
    126332158.5990    ,
    126332090.3270    ,
    126332022.0410    ,
    126331953.7590    ,
    126331885.4740    ,
    126331817.1800    ,
    126331748.8930    ,
    126331680.6070    ,
    126331612.3320    ,
    126331544.0540    ,
    126331475.7560    ,
    126331407.4670    ,
    126331339.1870    ,
    126331270.9070    ,
    126331202.6230    ,
    126331134.3280    ,
    126331066.0420    ,
    126330997.7580    ,
    126330929.4790    ,
    126330861.1910    ,
    126330792.9020    ,126330724.6240    ,
    126330656.3540    ,
    126330588.0730    ,
    126330519.7710    ,
    126330451.4860    ,
    126330383.2070    ,126330314.9290    ,
    126330246.6370    ,
    126330178.3430    ,
    126330110.0730    ,
    126330041.7950    ,
    126329973.5100    ,
    126329905.2190    ,
    126329836.9300    ,
    126329768.6510    ,
    126329700.3780    ,
    126329632.0950    ,
    126329563.8050    ,
    126329495.5270    ,
    126329427.2490    ,
    126329358.9680    ,
    126329290.6790    ,
    126329222.3980    ,
    126329154.1150    ,
    126329085.8320    ,
    126329017.5450    ,
    126328949.2550    ,
    126328880.9730    ,
    126328812.6820    ,
    126328744.3870    ,
    126328676.1090    ,
    126328607.8390    ,
    126328539.5530    ,
    126328471.2570    ,
    126328402.9720    ,
    126328334.6880    ,126328266.4060    ,
    126328198.1180    ,
    126328129.8240    ,
    126328061.5460    ,
    126327993.2820    ,
    126327924.9980    ,
    126327856.7020    ,
    126327788.4220    ,
    126327720.1300    ,
    126327651.8550    ,
    126327583.5620    ,
    126327515.2700    ,
    126327446.9770    ,
    126327378.7020    ,
    126327310.4260    ,
    126327242.1450    ,
    126327173.8530    ,
    126327105.5730    ,
    126327037.2810    ,
    126326968.9940    ,
    126326900.7160    ,
    126326832.4360    ,
    126326764.1450    ,
    126326695.8530    ,
    126326627.5690    ,
    126326559.2960    ,
    126326491.0040    ,
    126326422.7150    ,
    126326354.4370    ,
    126326286.1590    ,126326217.8590    ,
    126326149.5700    ,
    126326081.2920    ,
    126326013.0010    ,
    126325944.7200    ,
    126325876.4330    ,
    126325808.1460    ,
    126325739.8540    ,
    126325671.5710    ,
    126325603.2760,126325534.9840    },dev[100],dev1[100],ma2[100];
             printf("Enter number of the starting sample:   ");
             scanf("%d",&r);
             printf("Enter number of the ending sample:      ");
             scanf("%d",&t);
    
    
             for(a=0;a<t-r;a++,r++)
             {
                sd[a]=A[r];
                }
        printf("Enter number of items to calculate the moving avg on the successive diff array:");
        scanf("%u",&z);
        printf("Enter number of items to calculate the moving avg on the deviation array:");
        scanf("%u",&p);
        printf("\n");
    
         for(l=k=0;k<y,l<y;l++,k++)
         {
          sd1=sd[k+1]-sd[k];
          sds[k]=sds1[l]=sd1;
         // printf("\n")  ;
         // printf("\n sdiff%d  is %f",k,sds[k]);
          }
         //printf("\n");
    
         for(m=0;m<(y-(z-1));m++)
           {
              s=0;
              for(i=0;i<z;i++)
               {
                //printf("\nsds[%d]=%f  ",i+m,sds1[i+m]);
    
                s=s+sds1[i+m];
                //printf("\n%f",s);
                }
                avg=s/z;
                //printf("\navg for m=%d is %f",m,avg);
                mavg[m]=avg;
                printf("mavg[%d]is%f\n",m,mavg[m]);
               // printf("\n");
            }
               printf("\n");
    
         for(g=0,l=(((z+1)/2)-1);g<(y-z),l<(y+1-((z+1)/2));g++,l++)
        {
            dev[g]=dev1[g]= sds1[l]-mavg[g];
            printf("dev[%d]=%f\n",g,dev[g]);
        }
    
    
           for(q=0;q<(g-p+1);q++)
           {
              s=0;
              for(n=0;n<p;n++)
               {
                s=s+dev[n+q];
               // printf("\n%f",s);
                }
                avg=s/p; //printf("\navg for m=%d is %f",m,avg);
                ma2[q]=avg;
                printf("\n");
                printf("ma2[%d]is%f",q,ma2[q]);
              //  printf("\n");
            }
             getch();
    }
    
    


    Thanks!!
     
  6. Ironmaniac

    Ironmaniac New Member

    Joined:
    Sep 4, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    The array of interest is mavg[].....I need this to be very large......
    Code:
     
    
    float mavg[1000]
    ,sds[1000],sds1[1000],sd[1000],A[]={126332363.4610 ,
    126332295.1810 ,
    126332226.8900 ,
    126332158.5990 ,
    126332090.3270 ,
    126332022.0410 ,
    126331953.7590 ,
    126331885.4740 ,
    126331817.1800 ,
    126331748.8930 ,
    126331680.6070 ,
    126331612.3320 ,
    126331544.0540 ,
    126331475.7560 ,
    126331407.4670 ,
    126331339.1870 ,
    126331270.9070 ,
    126331202.6230 ,
    126331134.3280 ,
    126331066.0420 ,
    126330997.7580 ,
    126330929.4790 ,
    126330861.1910 ,
    126330792.9020 ,126330724.6240 ,
    126330656.3540 ,
    126330588.0730 ,
    126330519.7710 ,
    126330451.4860 ,
    126330383.2070 ,126330314.9290 ,
    126330246.6370 ,
    126330178.3430 ,
    126330110.0730 ,
    126330041.7950 ,
    126329973.5100 ,
    126329905.2190 ,
    126329836.9300 ,
    126329768.6510 ,
    126329700.3780 ,
    126329632.0950 ,
    126329563.8050 ,
    126329495.5270 ,
    126329427.2490 ,
    126329358.9680 ,
    126329290.6790 ,
    126329222.3980 ,
    126329154.1150 ,
    126329085.8320 ,
    126329017.5450 ,
    126328949.2550 ,
    126328880.9730 ,
    126328812.6820 ,
    126328744.3870 ,
    126328676.1090 ,
    126328607.8390 ,
    126328539.5530 ,
    126328471.2570 ,
    126328402.9720 ,
    126328334.6880 ,126328266.4060 ,
    126328198.1180 ,
    126328129.8240 ,
    126328061.5460 ,
    126327993.2820 ,
    126327924.9980 ,
    126327856.7020 ,
    126327788.4220 ,
    126327720.1300 ,
    126327651.8550 ,
    126327583.5620 ,
    126327515.2700 ,
    126327446.9770 ,
    126327378.7020 ,
    126327310.4260 ,
    126327242.1450 ,
    126327173.8530 ,
    126327105.5730 ,
    126327037.2810 ,
    126326968.9940 ,
    126326900.7160 ,
    126326832.4360 ,
    126326764.1450 ,
    126326695.8530 ,
    126326627.5690 ,
    126326559.2960 ,
    126326491.0040 ,
    126326422.7150 ,
    126326354.4370 ,
    126326286.1590 ,126326217.8590 ,
    126326149.5700 ,
    126326081.2920 ,
    126326013.0010 ,
    126325944.7200 ,
    126325876.4330 ,
    126325808.1460 ,
    126325739.8540 ,
    126325671.5710 ,
    126325603.2760,126325534.9840 }
     
  7. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Just use the code Saswat gave you, so instead of
    Code:
    float mavg[1000], sds[1000] /* etc */ ;
    
    use this:
    Code:
    float *mavg, sds[1000] /* etc */ ;
    mavg=(float *)malloc(100000 * sizeof(float) );
    // the rest of your code
    free(mavg); // don't forget this!
    
     
  8. Ironmaniac

    Ironmaniac New Member

    Joined:
    Sep 4, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Ok..thanks..I am starting to get a hold of things here......once I allocate the memory...using DMA...How do I store values into the memory locations...
    e.g. I free memory using the following code......how do I store values into the locations pointed by pointer A.

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
     
    void main()
    {
     clrscr();
    {
      double *A;
      A=(double *)malloc(100000000 * sizeof(double) );
        if(A==NULL)
         {
          printf("unsuccessful");
          // exit(0);
         }
        else
         {
          printf("you are in luck kid!!");
         }
       free(A);}
       getch();
    }
    
     
  9. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Same way you do it with normal arrays, e.g. A[5]=27;

    I would strongly advise you start reading a C book; it will tell you all this.
     
  10. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    You are allocating space for 100000000 doubles, which is equivalent to 100000000 * 8 bytes = 781248 KBs = 762 MBs !! :surprised :crazy: :surprised
    If you don't have enough Virtual Memory (Page File Size), your system will crash !

    See what happens in my task manager (running Windows XP) ::

    Initially :: [​IMG]

    Finally :: [​IMG]

    As expected :D
     

    Attached Files:

    shabbir likes this.

Share This Page