用C ++中的2个数组填充数组

时间:2018-01-05 14:42:30

标签: c++ arrays

我想创建一个包含数组A的2个元素和数组B的1个元素的数组。

测试用例:

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

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

结果:

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

我该怎么办?

#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;
}

4 个答案:

答案 0 :(得分:0)

第二个循环应该是小于n,除非你制作两个大小为m的数组。我假设你想从数组中得到随机数。您可以生成随机索引,使用该索引可以从第一个元素中选择2个元素,从第二个元素中选择1个元素。

答案 1 :(得分:0)

这对我有用。我希望它也适合你(但要注意人们对数组的可变大小做的评论,你不应该这样做):

#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;
}

答案 2 :(得分:0)

这可以使用下面给出的简单迭代来完成,因为它会提供所需的输出,只需在您的下面添加此代码即可。

{
 //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]<<" ";
}

答案 3 :(得分:0)

试试这个代码! 。它的工作正常&amp;验证您的测试用例。 我附加了输出的屏幕截图

#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