0

Please have a look at the following code

#include <iostream>

using namespace std;

int main()
{
    int array1[10] = {1,1,1,1,1,1,1,1,1,1};
    int array2[10] = {2,2,2,2,2,2,2,2,2,2};

    int array3[20];

    for(int i=0;i<=9;i++)
    {
        array3[i] = array1[i];
        array3[i+1] = array2[i];
    }

    for(int i=0;i<20;i++)
    {
        cout << array3[i] << endl;
    }
}

Here what I am trying to do is, assigning all the values in array1 and array2 into array3. These should be assigned in an order, which means,

array3[0] = array1[0]

array3[1] = array2[0]

array3[2] = array1[1]

array3[3] = array2[1]

but what I have tried is not working peroperly. Please help.

4 Answers 4

5

This would only assign up to element 10. I assume you would want something like the following:

for(int i=0;i<=9;i++)
{
    array3[i*2] = array1[i];
    array3[i*2+1] = array2[i];
}
Sign up to request clarification or add additional context in comments.

4 Comments

Or i += 2 instead of i++
@ColeJohnson - See my comments to other questions as to why this won't work. You'd have to have a separate variable to hold the indices for array3 or you'd overstep array1/2 or not fill array3. This most accurately describes what is meant, and is efficient (multiplies by small constants are very fast).
Oh, right. Forgot that I is used as indicie on source ones also
I like this way because it clearly shows a relationship between the i in each array1/2 and the i in array3. Personal preference I guess...
4

you need array3[i*2]= and array3[i*2+1]= in your logic because you add 2 values at once

but in addition there is algorithm for it

#include <algorithm>

int array1[10] = {1,1,1,1,1,1,1,1,1,1};
int array2[10] = {2,2,2,2,2,2,2,2,2,2};
std::copy(array2, array2 + sizeof10 array1);

Comments

1

Your assignment loop should be,

int k = 0;
for(int i=0;i<=9;i++)
    {
        array3[k++] = array1[i];
        array3[k++] = array2[i];
    }

As you have ensured the sizes of the array are right during declaration. It is ok to use an independent variable k to for indexing into the array3.

4 Comments

Slgihtly neater, but less efficient...and you should be able to cope with a '*2' in your head :)
Neat way! Thanks!! +1 from me :)
@Karthick its another variable that the processor has to deal with.
I would NEVER use post increment on array indicies. It's undefined.
0

try this..

for(int i=0;i<=9;i++)
{
array3[i*2] = array1[i];
array3[i*2+1] = array2[i];
}

4 Comments

He was asking for storing all the elements from array1 and array2. So, i<20 is necessary.
nope, its <10. else ur accessing index 19 from array1.. u are also writing into index 39 of array3
but array1 and array2 has just 10 elements. array1[11] till array1[19] would be invalid. If there is no padding between variables on stack array2[11] might indeed be array1[0]
@Roger Zhu you must not edit answer in this way, only spelling correction, formatting etc but not changing the answer in way you want. (Sorry for posting it in comments but I don't know where to post it) If you have different opinion about answer say in comments or add own answer, however there is already correct answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.