472,958 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

take first 10 elements in a char array....

hey, I have a method that takes a char array of 10.

I have a char array of 30. how do I make it send the first 10, then the
next 10, then the final 10 ?

I need help with my looping skills. thanks.

I have this, but I get the index was out of bounds....

char[] char1 =
{'1','2','3','4','5','6,'7','8','9','0','1','2','3 ','4','5','6','7','8','9','0','1','2','3','4', '5','6','7','8','9','0','1'};
char[] char2 = new char[10];
for (int i = 0; i < myChar.Length; i = i + 9)
{
for (int j = 0; j <= 8; j++)
{
char2.SetValue([char1 + char2], j);
method(char2);
}
}
Jan 9 '07 #1
4 6946


"roger_27" <ro*****@discussions.microsoft.comwrote in message
news:9B**********************************@microsof t.com...
hey, I have a method that takes a char array of 10.

I have a char array of 30. how do I make it send the first 10, then the
next 10, then the final 10 ?

I need help with my looping skills. thanks.

I have this, but I get the index was out of bounds....

char[] char1 =
{'1','2','3','4','5','6,'7','8','9','0','1','2','3 ','4','5','6','7','8','9','0','1','2','3','4',
'5','6','7','8','9','0','1'};
char[] char2 = new char[10];
for (int i = 0; i < myChar.Length; i = i + 9)
{
for (int j = 0; j <= 8; j++)
{
char2.SetValue([char1 + char2], j);
method(char2);
}
}
One way:

char[] char1 = ... yer array w/30 elements ..;
char[] first10 = new char[10];
char[] mid10 = new char[10];
char[] last10 = new char[10];

Array.Copy(char1, 0, first10, 0, 10);
Array.Copy(char1, 10, mid10, 0, 10);
Array.Copy(char1, 20, last10, 0, 10);
first10 now contains the first 10 elements of the char1 character array.
mid10 now contains the middle 10 elements of the char1 character array.
last10 now contains the last 10 elements of the char1 character array.

:) HTH,
Mythran
Jan 9 '07 #2

"roger_27" <ro*****@discussions.microsoft.comwrote in message
news:9B**********************************@microsof t.com...
hey, I have a method that takes a char array of 10.

I have a char array of 30. how do I make it send the first 10, then the
next 10, then the final 10 ?

I need help with my looping skills. thanks.

I have this, but I get the index was out of bounds....

char[] char1 =
{'1','2','3','4','5','6,'7','8','9','0','1','2','3 ','4','5','6','7','8','9','0','1','2','3','4',
'5','6','7','8','9','0','1'};
char[] char2 = new char[10];
for (int i = 0; i < myChar.Length; i = i + 9)
{
for (int j = 0; j <= 8; j++)
{
char2.SetValue([char1 + char2], j);
method(char2);
}
}
Second way:

char[] char1 =
{'1','2','3','4','5','6,'7','8','9','0','1','2','3 ','4','5','6','7','8','9','0','1','2','3','4',
'5','6','7','8','9','0','1'};

char[] char2 = new char[10];
for (int i = 0; i < char1.Length; i += 10)
{
Array.Copy(char1, i, char2, 0, Math.Min(10, char1.Length - i) ); // Min
function protects from exception when there is less than 10 elements to
copy.
method(char2);
Array.Clear(char2, 0, 10);
}

Note that your char1 aray has 31 elements , so last "pack" of 10 elements
will have only 1st element set.
Jan 9 '07 #3
The answer depends among other things what "method" is going to do with it.

In your example you've defined the 30 element array so make it two
dimensional and pass each set of 10 individually. Or you can pass along a
starting element to "method" so it knows which element to start with in the
30 element array. Or as somebody has responded you can break it up but why
break it up if you've created it to begin with?

If you're intent on doing it the way you've posted may I recommend that you
test the "looping" part without trying to reference the array? You just
need to see the element values you get and trying to incorporate the array
is causing the out of bounds error. Simply output the value so you can see
the numbers. You will see the number is out of range but you won't get an
error... adjust the routine and repeat.

"roger_27" <ro*****@discussions.microsoft.comwrote in message
news:9B**********************************@microsof t.com...
hey, I have a method that takes a char array of 10.

I have a char array of 30. how do I make it send the first 10, then the
next 10, then the final 10 ?

I need help with my looping skills. thanks.

I have this, but I get the index was out of bounds....

char[] char1 =
{'1','2','3','4','5','6,'7','8','9','0','1','2','3 ','4','5','6','7','8','9','0','1','2','3','4',
'5','6','7','8','9','0','1'};
char[] char2 = new char[10];
for (int i = 0; i < myChar.Length; i = i + 9)
{
for (int j = 0; j <= 8; j++)
{
char2.SetValue([char1 + char2], j);
method(char2);
}
}

Jan 9 '07 #4


"roger_27" <ro*****@discussions.microsoft.comwrote in message
news:9B**********************************@microsof t.com...
hey, I have a method that takes a char array of 10.

I have a char array of 30. how do I make it send the first 10, then the
next 10, then the final 10 ?

I need help with my looping skills. thanks.

I have this, but I get the index was out of bounds....

char[] char1 =
{'1','2','3','4','5','6,'7','8','9','0','1','2','3 ','4','5','6','7','8','9','0','1','2','3','4',
'5','6','7','8','9','0','1'};
char[] char2 = new char[10];
for (int i = 0; i < myChar.Length; i = i + 9)
{
for (int j = 0; j <= 8; j++)
{
char2.SetValue([char1 + char2], j);
method(char2);
}
}
And yet another way that's diff than my first way :) This is pretty easy
but probably not very elegant...note is uses the variables from my previous
post:

first10 = (new string(char1)).ToCharArray(0, 10);

and to make it easier to read:

private char[] GetCharsInArray(
Array arr,
int startIdx,
int length
)
{
// either this line
return (new string(arr)).ToCharArray(startIdx, length);

// or these lines, which are 'more correct' and probably
// process faster. They DO process faster on larger
// character arrays (I assume) as they do a direct copy
// rather than creating a new string and then convert
// that string to an array as above.
char[] buff = new char[length];
Array.Copy(arr, startIdx, buff, 0, length);
return buff;
}

and in your code:

first10 = GetCharsInArray(char1, 0, 10);
mid10 = GetCharsInArray(char1, 10, 10);
last10 = GetCharsInArray(char1, 20, 10);

HTH,
Mythran
Jan 9 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Pontus F | last post by:
Hi I am learning C++ and I'm still trying to get a grip of pointers and other C/C++ concepts. I would appreciate if somebody could explain what's wrong with this code: ---begin code block--- ...
3
by: Mark Miller | last post by:
I have a char array and when I write it to a file using BinaryWriter the position of the pointer is the size of the array + 1. For example: writing char leaves the pointer at position 26 after...
7
by: Luiz Antonio Gomes Picanço | last post by:
I have this structure: struct some { int version; char signature; } some2 void main() {
2
by: Amrit Kohli | last post by:
Hello. I have the following code, to do a simple operation by copying the elements of a vector of strings into an array of char pointers. However, when I run this code, the first element in the...
9
by: rob.kirkpatrick | last post by:
Hello I need to populate an array of char arrays at run-time. A very simplifed version of the code is below. char ** list should contain cnt char arrays. The values of char ** list are set by...
15
by: thinktwice | last post by:
char a = { 0 } is it ok?
8
by: OziRus | last post by:
Hi, I've char* array that I defined like char *str. I want to read from a file, that contains names in each row, and assign them to my str char * array. m is a char array, f is a file...
9
by: Angus | last post by:
Hello I want to write a function which returns a char array. But if I write this: char myfunction() { return "123"; }
13
by: Superman859 | last post by:
Hello everyone. Heads up - c++ syntax is killing me. I do quite well in creating a Java program with very few syntax errors, but I get them all over the place in c++. The smallest little things...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.