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

在C++中用2个数组填充数组,c++,arrays,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 :";

我想创建一个数组,其中包含数组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;
}

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

这对我很有用。我希望它也适用于您,但请注意人们对数组可变大小的评论,您不应该这样做:

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

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

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

试试这个代码。它运行良好,验证了您的测试用例。 我附上了输出的屏幕截图

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

在什么基础上,你要把元素推到新数组?可变长度数组不是标准C++,尽管有些编译器支持它作为扩展。在符合C++中,本地数组的大小必须是编译时间常数。数组A的2个元素和数组B的1个元素是什么意思?你的意思是你想让你的C数组像是[1,],[n],a(0),a(n-1),b [0 ],b[1 ],…,b[M-1 ]或类似[0,a [1 ],b[0 ] ]?如果你使用STD::向量,你会更容易用一个问题来解决…@ SaEdkAZeMi -我必须用数组来解决这个问题。这不是有效的C++语法:int a[m ];INTC[s];那么,如果代码不是合法的C++,你将如何解决这个问题?用变量作为数组的数量声明数组不是数组——它是C++以外的,编译器只是支持作为扩展。我想从第一个数组中分配两个元素,从第二个B中分配1个元素,我是新的C++,帮助我,谢谢。谢谢XXOO。你能告诉我你用来解决这个问题的算法吗?我是编程新手。我知道数组大小的问题,但这里我只给出与上述测试用例相关的解决方案。代码的逻辑是正确的,因此您可以根据需要更改数组的大小和限制。@MuhammadUsman取决于您认为正确的内容-用户输入的缺失范围检查是常见的故障源。。。原来的尝试使用VLA -如果它确实工作-将不需要这些,你的固定大小的数组现在突然做,所以你应该至少在评论中提到这…这是无效的C++ VLA;尽管您的编译器可能支持它。