TM_lol
TM_lol

Reputation: 27

is there other way to fill in array in C++ without vector

So I have array A and B, two of them contain random numbers and I need to write in the C array initially even numbers of A and B and then odd. I have made this wtih vector but I wonder if there is other way to do it like in Javascript there are methods like .unshift(), .push() etc

#include<iostream>
#include<vector>
using namespace std;

int main() {
    const int n = 4;
    int A[n];
    int B[n];
    vector<int>C;
    for (int i = 0; i < n; i++)
    {
        A[i] = rand() % 10;
        cout << A[i] << " ";
    }
    cout << endl;
    for (int i = 0; i < n; i++)
    {
        B[i] = rand() % 30;
        cout << B[i] << " ";
    }
    for (int i = 0; i < n; i += 1)
    {
        if (A[i] % 2 == 0)
        {
            C.push_back(A[i]);
        }
        if (B[i] % 2 == 0)
        {
            C.push_back(B[i]);
        }
    }
    for (int i = 0; i < n; i++)
    {
        if (A[i] % 2 != 0)
        {
            C.push_back(A[i]);
        }
        if (B[i] % 2 != 0)
        {
            C.push_back(B[i]);
        }
    }
    cout << endl;
    for (int i = 0; i < C.size(); i++)
        cout << C[i] << " ";
}

Upvotes: 2

Views: 211

Answers (4)

cryptor
cryptor

Reputation: 17

well you don't to change much.

first of declare C array as int C[n+n]; and declare a variable for incrementing through c array as int j=0;

and in if statements of loops do this C[j]=A[i]; j++; for first if and C[j]=B[i]; j++; for the second if statements

int main() {
    const int n = 4;
    int A[n];
    int B[n];
    int C[n+n];
    int j=0;
    for (int i = 0; i < n; i++)
    {
        A[i] = rand() % 10;
        cout << A[i] << " ";
    }
    cout << endl;
    for (int i = 0; i < n; i++)
    {
        B[i] = rand() % 30;
        cout << B[i] << " ";
    }
    for (int i = 0; i < n; i += 1)
    {
        if (A[i] % 2 == 0)
        {
            C[j]=A[i];
            j++;
        }
        if(B[i]%2==0){
            C[j]=B[i];
            j++;
        }
        
    }
    for (int i = 0; i < n; i++)
    {
        if (A[i] % 2 != 0)
        {
            C[j]=A[i];
            j++;
        }
        if (B[i] % 2 != 0)
        {
            C[j]=B[i];
            j++;
        }
    }
    j=0;
    cout << endl;
    for (int i = 0; i < C[].lenght(); i++)
        cout << C[i] << " ";
}

Upvotes: 0

cigien
cigien

Reputation: 60208

I would suggest interleaving A and B initially:

for (int i = 0; i < n; i += 1)
{
    C.push_back(A[i]);
    C.push_back(B[i]);
}

And then partitioning C into even and odd elements:

std::stable_partition(C.begin(), C.end(), [](int i) { return i % 2 == 0; });

Upvotes: 5

Kostas
Kostas

Reputation: 4176

You can use std::array, if you know the size you need in compile time. You can then add using an iterator.

#include<vector>
using namespace std;

int main() {
    const int n = 4;
    int A[n];
    int B[n];
    std::array<int, n+n>C;  // <-- here
    auto C_it = C.begin();  // <-- here

    for (int i = 0; i < n; i++)
    {
        A[i] = rand() % 10;
        cout << A[i] << " ";
    }
    cout << endl;
    for (int i = 0; i < n; i++)
    {
        B[i] = rand() % 30;
        cout << B[i] << " ";
    }
    for (int i = 0; i < n; i += 1)
    {
        if (A[i] % 2 == 0)
        {
            *C_it++ = A[i]; // <-- here
        }
        if (B[i] % 2 == 0)
        {
            *C_it++ = B[i];
        }
    }
    for (int i = 0; i < n; i++)
    {
        if (A[i] % 2 != 0)
        {
            *C_it++ = A[i];
        }
        if (B[i] % 2 != 0)
        {
            *C_it++ = B[i];
        }
    }
    cout << endl;
    for (int i = 0; i < C.size(); i++)
        cout << C[i] << " ";
}

Alternatively if you want to be more safe you can hold the next unwritten index and access elements with C.at(last++) = A[i], which checks for out-of-bounds and throws an exception instead of UB.

Upvotes: 1

JDługosz
JDługosz

Reputation: 5642

vector::push_back is the simplest way to have a collection that grows as you add things to the end.

Since you have fixed size for A and B, you could make them primitive arrays instead, which is what you have done. But for C you don't know how long it will be, so a collection that has a changeable size is appropriate.

Upvotes: 1

Related Questions