473,387 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to initialize a large float array efficiently

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
3 24276
sivadhas2006
142 100+
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
Rode
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
142 100+
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

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

Similar topics

13
by: Kyle | last post by:
Hi, Is it possible to initialize a constant memeber array in a class? I tried several syntax but all failed. class A { public: A(); ~A(); private:
0
by: Kueishiong Tu | last post by:
How do I initialize a char array with null characters? Is there an equivalent function to bzero() in VC++.net?
3
by: dbru | last post by:
I need to pass an address of a Managed float array to a DLL. The following doesn't seem to work extern static float GetXXX( StringBuilder HWND, long nWhat, ref float lparam );
2
by: twawsico | last post by:
I have a piece of code that needs to read the contents of a binary file (that I've created with another app) into an array of structures. The binary data in the file represents just a series of...
4
by: viks | last post by:
Hi guys I need little help here . I want to convert 'System::Object __gc * array ' to 'float array' Lets say I have object Reader with method Send ,it returns a variant that contains a...
15
by: thinktwice | last post by:
char a = { 0 } is it ok?
3
by: huiling25 | last post by:
My program works alright for integer array, but not float array. How can I change the program so that I can get the original position of each element in the float array after sorting? Let's say my...
3
by: syang8 | last post by:
If I have a struct S { int a; float b; }; how to initialize a static array of S?
2
by: Peter | last post by:
I have OLE Object field in Access Database. This field contains an array of floats. The array was moved from memory into a string and the string was saved as OLE Object in a database (That was...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.