Loading an array

Say I have array[11]={0};

And I have string line1("HelloHello")

How could I load/make equal array to the string line1?

I tried array={line1}

Do I need to use a loop to load every element individually?

Also, would anything be different to load a two dimensional char array?

Thank you!

First of all, if you're initializing an empty array, you can just do:

<variable_type> array[11];

Then you're asking how to load every element within the array equal to your string line1 variable?

You can think of arrays as a list of items of the same data type. So you may have something like this...

[item 1] [item 2] [item 3]....[item 11]

I'm guessing that you're getting compile errors with

array = {line1};

What you need to do...

for (int i = 0; i < 11; i++)
array[i] = line1;

If you want to do this to a two dimensional array, think of how you read a book. You read word by word, and then at the end of a line, you go to the next line and begin reading word by word until you finish reading. Accessing a multidimensional array is the same concept. If you want to fill a multidimensional object (or search), you need to traverse the entire span of the array, visiting each object until conditions are met.

How do you do this? Like this:

for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
array[i][j] = list1;

What is happening is that you visit each location in j, fill it with list1, and then at the end of the for loop when it breaks out of looping, i is increased by 1, and the j for loop begins again doing the same thing. This may help visualize the process...

[1][1] = 1
[1][2] = 1
[1][3] = 1
[2][1] = 1
[2][2] = 1
[2][3] = 1
[3][1] = 1
[3][2] = 1
[3][3] = 1

Hope that helps.
Topic archived. No new replies allowed.