0

i'm trying to concatenate 2 int arrays and after that i try to sort the resulted array. The reading works just fine,but i think there are some problems at the concatenate part.If someone can enlight me i'll be happy and excuse me for my rubbish english. NOTE: The code should be keept at a low lvl programming(like C++ rookie-all code in main) Code above:

int N, M,vect1[500],vect2[500];

cin>>N;
for(int i=0; i<N; i++)
    cin>>vect1[i];

cin>>M;
for(int i=0; i<M; i++)
    cin>>vect2[i];

int rez1 = sizeof(vect1) / sizeof(vect1[0]);
int rez2 = sizeof(vect2) / sizeof(vect2[0]);
int rez3=rez1+rez2;

vect1[N+rez3];
int j=0;
for(int i = rez1; i < rez3 ; i++`
{
        vect1[i]=vect2[j]; 
        j++;
}

int sortat = 0, aux;
while (sortat == 0)
 {
sortat = 1;
for (int i = 1; i < rez3; ++i)
  if (vect1[i] > vect1[i + 1])
    {
     sortat = 0;
     // interschimbam pe v[i] cu v[i + 1]
      aux = vect1[i];
      vect1[i] = vect1[i + 1];
      vect1[i + 1] = aux;
  }}                 for(int i=0; i <rez3; i++)
    cout<<vect1[i];       
return 0; 
  • 2
    It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [Debugging Guide](http://idownvotedbecau.se/nodebugging/) – NathanOliver Dec 14 '18 at 13:54
  • Possible duplicate of [C++ concatenate two int arrays into one larger array](https://stackoverflow.com/questions/12791266/c-concatenate-two-int-arrays-into-one-larger-array) – PradyumanDixit Dec 14 '18 at 13:54
  • Hint: check the result of `sizeof(vect1) / sizeof(vect1[0])`, you'll be surprised. – NathanOliver Dec 14 '18 at 13:57
  • What is the intention of `vect1[N+rez3];`? I think you need a third array here to hold your concatenated output. – Paul Sanders Dec 14 '18 at 14:03
  • 1
    Isn't like result of `sizeof(vect1) / sizeof(vect1[0])` is `500`? Is this what you really wanted? – Amadeusz Dec 14 '18 at 14:08
  • i wanted to increase the size of the vect1 with that Paul Sanders. – Parge Beniamin Dec 14 '18 at 14:14
  • actually with that sizeof i wanted to count the elements but i guess i should do a strlen or something like this. – Parge Beniamin Dec 14 '18 at 14:15
  • No, the way you did it is fine. (`strlen` is useless here.) I can't think what @NathanOliver had in mind. – TonyK Dec 14 '18 at 14:58
  • Still no answer -.- i know where is the problem but dont know why it does not work:-? – Parge Beniamin Dec 14 '18 at 14:59
  • The first obvious problem (although your program will still work if `N+rez3 <= 500`) is that the statement `vect1[N+rez3];` does nothing at all. I'm surprised the compiler didn't issue a warning. (Or did it?) You should really use a third array for this. And you should check that `M` and `N` don't exceed 500! (Unlikely, I know, but it's a good habit to get into.) – TonyK Dec 14 '18 at 15:01
  • "it should be solved all in main,without pointers or functions, the basic C++ for begginers" is *exceedingly* bad for learning C++. "basic C++ for beginners" involves learning that `std::vector` is your friend. – Caleth Dec 14 '18 at 15:06
  • @Caleth ikr... lot of things are wa y much easier with std and others but no one teaches you that from the beggining, there is no tutorial to start teaching you with std and others. – Parge Beniamin Dec 14 '18 at 15:11

2 Answers2

2
template <class Type, size_t sizeA, size_t sizeB>
void concat_arrays(const Type(&a)[sizeA],
                  const Type(&b)[sizeB],
                  Type(&result)[sizeA + sizeB])
{
    Type(&midpoint)[sizeB] = (Type(&)[sizeB])(result[sizeA]);
    std::copy(a, a + sizeA, result);
    std::copy(b, b + sizeB, midpoint);
}

You can use as follows:

int main()
{
    int a[3] = { 1, 2, 3 };
    int b[3] = { 10, 20, 30 };
    int c[6];

    concat_arrays(a, b, c); // c = a . b

    size_t cSize = sizeof(c) / sizeof(*c);

    for (int i = 0; i < cSize; i++)
        std::cout << c[i] << std::endl;

    return 0;
}
not an alien
  • 651
  • 4
  • 13
  • 1
    `Type(&result)[sizeA + sizeB]` is a little restrictive. What if I want to combine them into a larger array that I will add more data to later? – NathanOliver Dec 14 '18 at 14:11
  • 1
    The cast is pedantically UB, BTW. Just use `Type* midPoint = result + sizeA;`. – Jarod42 Dec 14 '18 at 14:16
  • It should be keept at a low low level(programming lvl) it should be solved all in main,without pointers or functions, the basic C++ for begginers – Parge Beniamin Dec 14 '18 at 14:19
  • 1
    For @NathanOliver's point, add another `size_t` to the template argument list and then `static_assert` that it's `>= sizeA + sizeB` – AndyG Dec 14 '18 at 14:47
  • @PargeBeniamin: those seem like arbitrary restrictions. Additionally, they were not mentioned in your question – AndyG Dec 14 '18 at 14:48
  • @AndyG you are right,that was my fault, i have edited the description. I try to add the second array to the first one, from the position of the last element from 1st array, but it doesn't work... – Parge Beniamin Dec 14 '18 at 14:52
  • You don't need to compute the midpoint. The first `std::copy` returns an iterator that can be used as the destination for the second `std::copy` call. – Blastfurnace Dec 14 '18 at 16:44
0

std::vector is used like a C style array, but doesn't have half as many annoying quirks.

#include <iostream> // for std::cin, std::cout
#include <vector> // for std::vector
#include <algorithm> // for std::sort

int main()
{
    int N, M;
    std::cin >> N;

    std::vector<int> vect1(N); // Allocates N ints
    for(int i = 0; i != N; ++i)
        std::cin >> vect1[i]; // Indexed like an array

    std::vector<int> vect2(M); // Allocates M ints
    for(int i = 0; i != M; ++i)
        std::cin >> vect2[i];

    vect1.reserve(N + M); // Reallocate to fit more
    vect1.insert(vect1.end(), vect2.begin(), vect2.end()); // add elements at the back

    std::sort(vect1.begin(), vect1.end()); // All the algorithms operate on pairs of iterators

    for(int i = 0; i != M + N; ++i)
        std::cout << vect1[i];
}
Caleth
  • 52,200
  • 2
  • 44
  • 75