This program has stopped working..

Hey guys! I hit a slight problem. While I don't get any error on compiling this code, when running the program, I get the "This program has stopped working" error. I have tried putting everything in a try/catch to see if there is any problem, but I got nothing.
Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
void generar_array(int arr1[], int tam);
void mostrar_array(int arr1[], int tam);
void organizar_array(int arr1[], int tam);
int main()
{
 int arr1[100], tam=100;
 generar_array(arr1,tam);
 mostrar_array(arr1,tam);
 organizar_array(arr1,tam);
 mostrar_array(arr1,tam);

return 0;

}
void generar_array(int arr1[], int tam)
{
 int i;
 srand(time(NULL));
 for(i=0;i<tam;i++)
 {
     arr1[i]=1+rand()%(1000-1);
 }

}
void mostrar_array(int arr1[], int tam)
{
    int i;
    cout<<"el array formado es"<<endl;
    for(i=0;i<tam;i++)
    {
        cout<<arr1[i]<<" ";
    }
    cout<<endl<<endl;

}
void organizar_array(int arr1[], int tam)
{
int j=0, k=0,i;
int arr2[j], arr3[k];
for(i=0;i<tam;i++)
{
    if(arr1[i]%2==0)
    {
        arr2[j]=arr1[i];
        j++;
    }

    else
    {
        arr3[k]=arr1[i];
        k++;
    }
}
for(i=0;i<j;i++)
{
    arr1[i]=arr2[i];
}
for(i=j;i<(j+k);i++)
{
    arr1[i]=arr2[i];
}
cout<<"el array ordenado es "<<endl;


}


and the output is
1
2
3
4
5
6
7
8
9
10
el array formado es
277 545 304 143 267 43 861 962 586 953 503 386 654 556 970 60 330 305 232 274 13
7 422 810 488 994 641 453 233 836 26 567 874 971 196 459 421 804 466 589 50 731
858 670 905 451 667 43 890 505 143 938 160 872 595 66 664 258 449 149 652 76 577
 819 865 374 235 126 540 512 915 641 158 228 273 235 252 503 400 900 12 269 424
687 189 766 726 149 652 192 301 738 707 48 412 773 294 625 230 597 212


Process returned -1073741819 (0xC0000005)   execution time : 6.779 s
Press any key to continue.


It is not the first time it happens, it happened quite a lot. I am sure it isn't due to my RAM, as on other PCs it throws the same error.
Any help is greatly appreciated!
Thank you in advance!
Best regards,
BrreaKerr.
In your oranise_array function you have multiple errors. Line 43 isn't even legal C++, you have to define arrays with constexpr values, or dynamically allocate on the heap. Also, you do realise that the arrays would be initialized with size 0 anyway, which means that you instantly get a segfault? You should dynamically your arrays with size tam (worst case scenario), and then remember to deallocate at the end of the function. Alternately, you could change to a better algorithm, which doesn't require 2 temporary arrays, such as swapping the elements within the array itself.
I managed to find the problem, I was declaring an array as arr2[0] and arr3[0]. meaning that in the for that follows the program was trying to place values in an array that does not have any dimensions. I explained it here, just in case someone else runs into this problem.
Edit: I forgot to refresh before posting, thank you NT3!
Last edited on
Topic archived. No new replies allowed.