-4

I want to create an array that has 2 elements of array A and 1 element of array B.

test case:

A: 10,15,7,19,6,24,12,2,18

B: 13,21,5,3,14,9,17

result:

10,15,13,7,19,21,6,24,5,12,2,3,18,14,9,17

What should i do ?

#include <iostream>

using namespace std;

int main()
{

    int n,m,s;
    cout<<"please enter M :";
    cin>>m;
    cout<<"please enter N :";
    cin>>n;
    int a[m];
    for(int i=0; i<m; i++)
    {
        cout<<"enter "<<i<<" number of an array";
        cin>>a[i];
    }
    int b[n];
    for(int i=0; i<m; i++)
    {
        cout<<"enter "<<i<<" number of an array";
        cin>>b[i];
    }
    s = m + n;
    int c[s];

    return 0;
}
  • 1
    on what basis you are going to push elements to new array? – mayank bisht Jan 05 '18 at 14:44
  • 6
    Variable length arrays are not standard c++, though some compilers support it as an extension. In conformant c++, the size of a local array must be a compile time constant. – François Andrieux Jan 05 '18 at 14:46
  • 1
    what do you mean by _"2 element of array A ,and 1 element of array B"_? do you mean that you want your `c` array to be something like `A[0],A[1],...,A[n-1],A[0],A[1],...,A[n-1],B[0],B[1],...,B[m-1]` or something like `A[0],A[1],B[0]`? – apalomer Jan 05 '18 at 14:47
  • 1
    If you had used `std::vector` instead, you'd live easier with one of the solutions to [this](https://stackoverflow.com/questions/201718/concatenating-two-stdvectors) question... – Aconcagua Jan 05 '18 at 14:56
  • @apalomer I want select 2 element from first array(A) , and 1 element from second(B). – saeed kazemi Jan 05 '18 at 15:02
  • @Aconcagua I have to solve this with array. – saeed kazemi Jan 05 '18 at 15:03
  • 2
    @saeedkazemi -- *I have to solve this with array* -- This is not valid C++ syntax: `int a[m];...int c[s];` So how are you going to solve this, given that your code is not legal C++? Declaring arrays using a variable as the number of entries is not an array -- it is something outside of C++ that your compiler just happens to support as an extension. – PaulMcKenzie Jan 05 '18 at 15:09
  • It looks like the majority of your code is just getting the A and B arrays in. You might want to add comments stating what you're doing there and change the prompts to something like "Please enter size of array A", `"Please enter the value for A[" << i << "]: "` to make that clearer. – dwilliss Jan 05 '18 at 15:09
  • I wish that g++ or whatever compiler would turn **off** the VLA's by default. Instead we get posts every single day from new programmers who believe they're writing real C++ when they do things like `int m;...int a[m];`. Turning off the VLA's would thus force the newbie programmer to come up with valid code (by probably using `std::vector`) instead of falling into the VLA trap. – PaulMcKenzie Jan 05 '18 at 15:14
  • 1
    @PaulMcKenzie just a non-related question but aligned with the comment of almost everybody. Would `int m(10);int* a= new int[m]` be real c++? Obviously then you should deallocate using `delete[] a`. – apalomer Jan 05 '18 at 15:24
  • Yes, that would be valid C++. But it puts emphasis on using `new[] / delete[]` when not necessary. In any respect, the OP would have been forced to use something other than what they're using now if VLA's were turned off by default. – PaulMcKenzie Jan 05 '18 at 22:58

4 Answers4

0

The second loop should be till i less than n, unless you are making both arrays of size m. I assume you want to have random numbers from the arrays. You can generate the random index, using which you can select 2 elements from the first one and 1 from the second.

0

This works for me. I hope it does for you as well (but be aware of the comments that people do about the variable size of the array, you should not be doing that):

#include <iostream>

using namespace std;

int main()
{

    int n,m,s;
    cout<<"please enter M :";
    cin>>m;
    cout<<"please enter N :";
    cin>>n;
    int a[m];
    for(int i=0; i<m; i++)
    {
        cout<<"enter "<<i<<" number of array A: ";
        cin>>a[i];
    }
    int b[n];
    for(int i=0; i<n; i++)
    {
        cout<<"enter "<<i<<" number of array B: ";
        cin>>b[i];
    }
    s = m + n;
    int c[s];

    // Copy proportions
    int a_index(0);
    int b_index(0);
    int c_index;
    for (c_index = 0; a_index < m && b_index < n; c_index++)
    {
        if (c_index%3 == 2)
        {
            c[c_index] = b[b_index];            
            b_index++;
        }
        else
        {
            c[c_index] = a[a_index];
            a_index++;
        }
    }

    // Copy the rest of the arrays, starting by a
    for (int i = a_index; i < m; i++){
        c[c_index++] = a[a_index++];
    }
    for (int i = b_index; i < n; i++){
        c[c_index++] = b[b_index++];
    }

    // Display result
    std::cout<<"A:";
    for (int i = 0;i<m;i++){
        std::cout<<" "<<a[i];
    }
    std::cout<<std::endl;
    std::cout<<"B:";
    for (int i = 0;i<n;i++){
        std::cout<<" "<<b[i];
    }
    std::cout<<std::endl;
    std::cout<<"C:";
    for (int i = 0;i<m+n;i++){
        std::cout<<" "<<c[i];
    }
    std::cout<<std::endl;

    return 0;
}
apalomer
  • 1,895
  • 14
  • 36
0

this can be done using simple iteration as given below as it would give the required output , just add this code below yours.

{
 //adding elements in array
 for(int i=1;i<s+1;i++)
   {
     if(i%3 == 0)    //for every third number will be filled from b[]
       c[i-1]=b[i-1];//index resolution
     else            //while all the other numbers will be from a[]
       c[i-1]=a[i-1];//index resolution
   }
 //printing result
 for(int i=0;i<s;i++)
   cout<<c[i]<<" ";
}
0

Try this code ! . Its working fine & verifed your test cases . I am attached the screenshort of the output

#include <iostream>
using namespace std;

int main()
{

    int n, m, count=0,countA=0,countB=0;

    // n respresnts the size of Array A
    // m represents the size of Array B
    // count variable is used to track that exactly two elements of array A in inserted in array C
    // countA used to point the array A index 
    // countB used to point the array B index

    int a[10], b[10], c[20];

    // enter size of first array A
    cout << "please enter M :";
    cin >> m;

    //enter size of second array B
    cout << "please enter N :";
    cin >> n;

    //taking input in first array A
    for (int i = 0; i<m; i++)
    {
        cout << "enter " << i << " number of an array";
        cin >> a[i];
    }

    //taking input in second array B
    for (int i = 0; i<n; i++)
    {
        cout << "enter " << i << " number of an array";
        cin >> b[i];
    }

    //creating the resultant Array C
    for (int i = 0; i<m+n; i++)
    {
        if (count < 2 && countA < m)
        {
            c[i] = a[countA];
            countA++;
            count++;
        }
        else
        {
            c[i] = b[countB];
            countB++;
            count=0;

        }

    }

    //displaying the Array C
    for (int i = 0; i < m + n; i++)
    {
        cout << c[i] << " \t";
    }


    return 0;
}

Output of the code

Usman
  • 1,983
  • 15
  • 28
  • thank you XOXO. can you tell me the algorithm that you use to solve this Iam new to programming. – saeed kazemi Jan 06 '18 at 14:42
  • I know the problem of the size of array but here I am only giving the solution related to test case above. The logic of the code is correct so you can change the size of the array and limit according to your requirement. – Usman Jan 06 '18 at 19:10
  • @MuhammadUsman Depends on what one considers "correct" - missing range checks of user input are such a common source of failures... The original attempt using VLA -- if it *did* work -- would not have needed these, your fixed size arrays now suddenly *do*, so you should at least mention this in a comment... – Aconcagua Jan 08 '18 at 10:42