DaniV
DaniV

Reputation: 13

How to remake one array into two smaller via C++?

I need to make from one 10-element array two 5-element arrays. I made code like this

#include <iostream>
using namespace std;

int main() {
    const int size1 = 10, size2 = 5;

    int array[size1]{ 1,3,6,2,82,10,6,34,2,5 }, arb[size2]{}, arc[size2]{};

    for (size_t i = 0; i < 5; i++)
    {
        arb[i] = array[i];
    }
    for (size_t i = 5; i < 9; i++) {
        
        for (size_t e = 0; e < 4; e++)
        {
            arc[e] = array[i];
        }
        
    }

    for (size_t i = 0; i < 5; i++)
    {
        cout << arb[i] << "\t";
    }
    cout << endl;

    for (size_t i = 0; i < 5; i++)
    {
        cout << arc[i] << "\t";
    }
}

But the second array gives "2 2 2 2 0". I haven`t any ideas how to fix this.

Upvotes: 0

Views: 128

Answers (6)

Nadin Hasan
Nadin Hasan

Reputation: 29

The problem is in the nested for loop

for (size_t i = 5; i < 10; i++)
{ 
    //essentially what you're doing in this loop is that for each element 5, 6, 7, 8, 9 
    // for the first loop you are assigning for instance arc[0] till arc[3] to be all equal to array[5]
    // so whats happening is n=by the time you reach arc
    // take care you are assigning only 4 elements

    for (size_t e = 0; e < 4; e++)          // size here 
    {
        arc[e] = array[i];
    }

}

An alternative is to do this:

for (size_t i = 0; i < size2; i++)
{
    arc[i] = array[i+5];
}

Upvotes: 0

Surt
Surt

Reputation: 16099

Lets try the dirty variant, O(1)

Warning this will explode in your face if array goes out of scope before arb or arc

int main() {
    const int size1 = 10, size2 = 5;

    int array[size1]{ 1,3,6,2,82,10,6,34,2,5 };
    int *arb = array, *arc = &array[size2];

    for (size_t i = 0; i < size2; i++) {
        cout << arb[i] << "\t";
    }
    cout << endl;

    for (size_t i = 0; i < size2; i++) {
        cout << arc[i] << "\t";
    }
}

Upvotes: 0

Jerry Coffin
Jerry Coffin

Reputation: 490108

Unless you have a specific reason that these must be arrays (which is extremely rare, at least in my experience), I'd use std::vectors instead. This makes it trivial to initialize the smaller "arrays" with the correct pieces of the larger one:

std::vector<int> array { 1,3,6,2,82,10,6,34,2,5 };
std::vector<int> arb { array.begin(), array.begin() + size2};
std::vector<int> arc { array.begin() + size2, array.end() };

This also makes it easy to (for one example) split the main array in half, regardless of its exact size:

std::vector<int> array { 1,3,6,2,82,10,6,34,2,5, 12, 17, 92, 101, -3 };
auto half = array.size() / 2;
std::vector<int> arb { array.begin(), array.begin() + half};
std::vector<int> arc { array.begin() + half, array.end() };

[As it stands right now, this will make the second half the larger one if the main array has an odd number of elements, but if that's not what you want, you can change the computation of half to suit your needs.]

Upvotes: 4

Vlad from Moscow
Vlad from Moscow

Reputation: 310950

These nested loops

for (size_t i = 5; i < 9; i++) {
    
    for (size_t e = 0; e < 4; e++)
    {
        arc[e] = array[i];
    }
    
}

do not make a sense. Moreover there are used unknow magic number 9 and 4. In fact all elements of the array arc except the last element are set to the value of the element array[8] that is to the value of the element of the array array used in the last iteration of the outer loop.

The program can look the following way

#include <iostream>

int main() 
{
    const size_t size1 = 10, size2 = 5;

    int array[size1]{ 1,3,6,2,82,10,6,34,2,5 }, arb[size2]{}, arc[size2]{};

    for ( size_t i = 0; i < size2; i++ )
    {
        arb[i] = array[i];
    }
    
    for ( size_t i = 0; i < size2; i++ )
    {
        arc[i] = array[i + size2];
    }
    
    for ( const auto &item : arb )
    {
        std::cout << item << ' ';
    }
    
    std::cout << '\n';
    
    for ( const auto &item : arc )
    {
        std::cout << item << ' ';
    }
    
    std::cout << '\n';
    
    return 0;
}

The program output is

1 3 6 2 82 
10 6 34 2 5 

Or instead of the loops you could use the standard algorithm std::copy and std::copy_n. For example

#include <iostream>
#include <algorithm>
#include <iterator>
#include <algorithm>

int main() 
{
    const size_t size1 = 10, size2 = 5;

    int array[size1]{ 1,3,6,2,82,10,6,34,2,5 }, arb[size2]{}, arc[size2]{};

    std::copy_n( std::begin( array ), size2, std::begin( arb ) );
    std::copy_n( std::next( std::begin( array ), size2 ), size2, std::begin( arc ) );
    
    for ( const auto &item : arb )
    {
        std::cout << item << ' ';
    }
    
    std::cout << '\n';
    
    for ( const auto &item : arc )
    {
        std::cout << item << ' ';
    }
    
    std::cout << '\n';
    
    return 0;
}

The program output is the same as shown above.

1 3 6 2 82 
10 6 34 2 5 

Upvotes: 1

selbie
selbie

Reputation: 104514

Use memcpy to copy an array of plain old value types.

memcpy(arb, array, size2);
memcpy(arc, array+size2, size2);

Both of those statements replace your two for-loops.

memcpy function is available by declaring #include <string.h> at the top of your source file.

Upvotes: -1

Denis Kotov
Denis Kotov

Reputation: 882

You did mistake in second loop by iterating second time, in such way after this loop you set only last element in new array ...

Here is the proper version of the code:

#include <iostream>

int main() {
  const int size1 = 10, size2 = 5;

  int array[size1]{ 1, 3, 6, 2, 82, 10, 6, 34, 2, 5 };
  int arb[size2]{};
  int arc[size2]{};

  for (size_t i = 0; i < 5; i++) {
    arb[i] = array[i];
  }
  for (size_t i = 5; i < 10; i++) {
    arc[i-5] = array[i];
  }

  for (size_t i = 0; i < 5; i++) {
    std::cout << arb[i] << "\t";
  }
  std::cout << std::endl;

  for (size_t i = 0; i < 5; i++) {
    std::cout << arc[i] << "\t";
  }
}

Also I would suggest simpler version of the code with std::copy:

#include <iostream>

int main() {
  const int size1 = 10, size2 = 5;

  int array[size1]{ 1, 3, 6, 2, 82, 10, 6, 34, 2, 5 };
  int arb[size2]{};
  int arc[size2]{};

  std::copy(&array[0], &array[5], arb);
  std::copy(&array[5], &array[10], arc);

  for (size_t i = 0; i < 5; i++) {
    std::cout << arb[i] << "\t";
  }
  std::cout << std::endl;

  for (size_t i = 0; i < 5; i++) {
    std::cout << arc[i] << "\t";
  }
}

Upvotes: 1

Related Questions