473,626 Members | 3,340 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to initialize a large float array efficiently

2 New Member
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 24310
sivadhas2006
142 New Member
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 New Member
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 New Member
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
14817
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
1944
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
2894
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
4850
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 singles that correspond to those in my structure detailed below. So when I load the file, all that I know for certain is that there will be some multiple of these eight singles represented in the binary data. My code below will read the data...
4
6069
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 one-dimensional array of float value . I m trying to do something like this.
15
26417
by: thinktwice | last post by:
char a = { 0 } is it ok?
3
3081
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 float array is {5.0f,10.0f,1.0f,15.0f}. Here's my code: import java.util.*; public class SortArrays { public static void main(String args) { Integer score = {5, 10, 0, 1, 15};
3
12613
by: syang8 | last post by:
If I have a struct S { int a; float b; }; how to initialize a static array of S?
2
2014
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 written in VB6). This data was used to create graphs, instead creating a record for each point the entire array was saved in one field. How do I move the same data from database field in to float array suing C# ? Thank You
0
8266
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8705
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8638
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
5574
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4092
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2626
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.