By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,603 Members | 1,422 Online
Bytes IT Community
+ Ask a Question
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
  1.     float fArray[2000];
  2.     int i;
  3.     for( i = 0; i < 300; i++)
  4.     {
  5.         fArray[i]  = 0.0;
  6.     }
  7.  
  8.    for(i = 300; i < 1770; i++)
  9.     {
  10.         //Setting fArray[i]  based on some calculation
  11.     }
  12.  
  13.    for(i = 1770; i < 2000; i++)
  14.   {
  15.        fArray[i]  = 0.0;
  16.  
  17.   }
  18.  
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
Share this Question
Share on Google+
3 Replies


sivadhas2006
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
  1.     float fArray[2000];
  2.     int i;
  3.     for( i = 0; i < 300; i++)
  4.     {
  5.         fArray[i]  = 0.0;
  6.     }
  7.  
  8.    for(i = 300; i < 1770; i++)
  9.     {
  10.         //Setting fArray[i]  based on some calculation
  11.     }
  12.  
  13.    for(i = 1770; i < 2000; i++)
  14.   {
  15.        fArray[i]  = 0.0;
  16.  
  17.   }
  18.  
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
  1.    float 
  2.       fArray[2000] = {0.0};
  3.    int 
  4.       i = 0;
  5.  
  6.    for(i = 300; i < 1770; i++)
  7.    {
  8.       //Setting fArray[i]  based on some calculation
  9.    }
  10.  
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

sivadhas2006
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

Post your reply

Sign in to post your reply or Sign up for a free account.