454,603 Members | 1,422 Online Need help? Post your question and get tips & solutions from a community of 454,603 IT Pros & Developers. It's quick & easy.

# How to initialize a large float array efficiently

 P: 2 Hi, I want to initialize some elements in an array at the beginning(say 300 elements) and at the ending(230 elements) to 0 and the elements between start and end to a value based on the calculation. Currently, I am doing in the following way: Expand|Select|Wrap|Line Numbers     float fArray;     int i;     for( i = 0; i < 300; i++)     {         fArray[i]  = 0.0;     }      for(i = 300; i < 1770; i++)     {         //Setting fArray[i]  based on some calculation     }      for(i = 1770; i < 2000; i++)   {        fArray[i]  = 0.0;     }   In the above code, the first and the last loops are doing the same except for the range. I want to optimize this code. If possible, I just want to avoid the loops to initialize the array with 0. I want to do that initialization in a better way. Is there any better solution for this? Nov 15 '06 #1
3 Replies

 100+ P: 142 Hi, I want to initialize some elements in an array at the beginning(say 300 elements) and at the ending(230 elements) to 0 and the elements between start and end to a value based on the calculation. Currently, I am doing in the following way: Expand|Select|Wrap|Line Numbers     float fArray;     int i;     for( i = 0; i < 300; i++)     {         fArray[i]  = 0.0;     }      for(i = 300; i < 1770; i++)     {         //Setting fArray[i]  based on some calculation     }      for(i = 1770; i < 2000; i++)   {        fArray[i]  = 0.0;     }   In the above code, the first and the last loops are doing the same except for the range. I want to optimize this code. If possible, I just want to avoid the loops to initialize the array with 0. I want to do that initialization in a better way. Is there any better solution for this? Hi, Try this ... Expand|Select|Wrap|Line Numbers    float        fArray = {0.0};    int        i = 0;      for(i = 300; i < 1770; i++)    {       //Setting fArray[i]  based on some calculation    }   Regards, M.Sivadhas. Nov 15 '06 #2

 P: 2 Hi, Thanks for the reply. It is effective. But if I want to initialize the array at a later stage, assume that I fideled with the array after its declaration and now I want to initalize to 0 at the top and 1 at the bottom, is there any way? Nov 15 '06 #3

 100+ P: 142 Hi, If it is char array means we can initialize without using for loop. But I don't know to initialize the float array without using for loop. Regards, M.Sivadhas. Nov 16 '06 #4 