473,568 Members | 2,850 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sorting array data

1 New Member
I'm trying to figure out the best approach to grab data from an array. So heres the deal. I have byte array called curveData[256] filled will hex bytes that I've downloaded from a unit under test. Basically, I am trying to loop through this array of 256 bytes. I want to grab the 1st byte (of a 4 byte chunk), so [0], then [4] [8] and so on…so every 4th byte starting with the first and throwing them into the rpmArray. Then I want to grab the bytes in between, [1][2][3] …etc, and throw them into another array called multOffArray. Here is my code below

|0|1|2|3|4|5|6| 7|…………|252|253| 254|255|


int i;
byte[] rpmArray = new byte[64];
byte[] multOffArray = new byte[192];
for (i = 0; i < curveData.Lengt h; i++ )
{
if (i%4 == 0 || i%4 == 4)
Array.Copy(curv eData, curveData[i], rpmArray, curveData[i], 1);
else
Array.Copy(curv eData, curveData[i], multOffArray, curveData[i+1], 3);
}
DisplayData(Mes sageType.Outgoi ng, "\n rpmArray: " + rpmArray);
DisplayData(Mes sageType.Outgoi ng, "\n multOffArray: " + multOffArray);

I'm getting errors saying that the index is out of bounds. When I check the bytes in the arrays where I’m storing this info, its not in the order that I anticipated and I am missing data. I tried to set it up so that at every index which is a multiple of 4, would initiate copying of data from the source array to my destination array. Maybe somebody can help me get this loop right. It's been a while since I've written code and I know this isn't right. If someone can help it'd be much appreciated.
Oct 8 '08 #1
3 1237
balabaster
797 Recognized Expert Contributor
Would something like this be suitable?

Expand|Select|Wrap|Line Numbers
  1. System.Collections.ArrayList rpmArray = new  System.Collections.ArrayList();
  2. System.Collections.ArrayList multOffArray = new System.Collections.ArrayList();
  3.  
  4. for (int i = 0; i < curveData.Length; i++)
  5. {
  6.     if (i % 4 == 0)
  7.         rpmArray.Add(curveData[i]);
  8.     else
  9.         multOffArray.Add(curveData[i]);
  10. }
It bypasses the need for array management and if you want to treat the ArrayLists as arrays, just reference them by rpmArray.ToArra y() and multOffArray.To Array()
Oct 9 '08 #2
balabaster
797 Recognized Expert Contributor
Or you could use the snazzy new LINQ to query the data set rather than iterating through it:

Expand|Select|Wrap|Line Numbers
  1. int rowIndex = 0;
  2.  
  3. var rpmQry = 
  4.   from item in (
  5.     from value in curveData 
  6.     select new {index = rowIndex++, value}
  7.   )
  8.   where item.index % 4 == 0
  9.   select item.value;
  10.  
  11. var rpmData = rpmQry.ToArray();
I tried to figure out a more succinct way of getting at the row indexes of each item to query for mod 4, but it's late and I'm tired... it would be nicer as a simple query rather than nested query. I started off with two queries, but I didn't like that it was being visited twice, until I wanted to reference it a second time for the multOffArray:

Expand|Select|Wrap|Line Numbers
  1. int rowIndex = 0;
  2. var allDataQry = from item in curveData select new { index = rowIndex++, value = item };
  3. var rpmArray = (from item in allDataQry where item.index % 4 == 0 select item.value).ToArray();
  4. var multOffArray = (from item in allDataQry where item.index % 4 != 0 select item.value).ToArray();
  5.  
Oct 9 '08 #3
Plater
7,872 Recognized Expert Expert
As for why you get index of bounds:
Think about what happens on the last itteration for i, i would be the last index in your array. So what would curveData[i+1] (Note: i+1) be, out of bounds.
Oct 9 '08 #4

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

Similar topics

7
3247
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) CType(local4, Short) = CType(src, Short)
0
2651
by: Alex Vinokur | last post by:
=================================== ------------- Sorting ------------- Comparative performance measurement =================================== Testsuite : Comparing Function Objects to Function Pointers Source : Technical Report on C++ Performance Tool : The C/C++ Program Perfometer (Version 2.8.1-1.19-Beta) *...
4
2529
by: John Bullock | last post by:
Hello, I am at wit's end with an array sorting problem. I have a simple table-sorting function which must, at times, sort on columns that include entries with nothing but a space (@nbsp;). I want all of the spaces to be put in the first slots of the array. IE 6 does this. But Firefox 0.9.1 doesn't, and I don't know why. I have not been...
5
2510
by: DKC | last post by:
Hi, Using VB.NET. I have a datagrid having a strongly typed array of objects as its data source. The data from the array of objects is displayed by means of a table style, which is fine, but I cannot sort the data when I click on the column header. I have set the tablestype.allowsorting = true, but this has no effect.
0
3221
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of the list view you need to do sorting another way... here is my code on how I did it to help anyone starting out get an idea of how to use virtual mode in ..NET 2.0 Imports...
7
4806
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the expandable row, the hidden row is made visible with css. The problem is when i sort the rows, the hidden rows get sorted as well which i don't want and want...
5
6676
KevinADC
by: KevinADC | last post by:
Introduction This discussion of the sort function is targeted at beginners to perl coding. More experienced perl coders will find nothing new or useful. Sorting lists or arrays is a very common requirement of programs. If you don't know the difference between a list and array don't worry about it. A list is just an array without a name. They...
1
7175
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar methods or syntax to a less experienced perl coder. I will post links to online resources you can read if necessary. Experienced perl coders might find...
6
4061
by: =?Utf-8?B?RGFu?= | last post by:
I am reposting a question from about 3 weeks ago ("sorting capability"). I have an aspx page in which I get the data from a database dynamically, through C# code, by creating a dynamic table using TableHeaderCell and TableHeaderRow. The data is binded to the table by using a DataSet and Data Reader. The responses on the forum made me...
5
4916
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums there are, it crashes. There are currently 6 columns, and I only want 4. How do I remove the last two (discount and date)? Here is a link:...
0
7604
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7916
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. ...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6275
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5498
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
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...
0
3651
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...
1
2101
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
0
932
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...

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.