im so confused

below is a function i'm trying to write but i'm confused. i'm supposed to make a point in here called a pivot that divides this array of random size. it's suppose to tell me the point where i split it at, then shoot out two arrays with the values array 1 is all values before pivot and array 2 is all values behind the pivot.

void split_up_array(int array[], int size, int *& array1, int *& array2, int & size1, int & size2)
{
// Split up array
array1 = new int[arraySize / 2];
array2 = new int[arraySize / 2];

for (int count = 0; count < arraySize; count++)
{
array[count];

}
closed account (48T7M4Gy)
Maybe this is a start to getting the answer:

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
#include <iostream>

int main()
{
    int array[] = {1,2,3,4,5,6,7,8,9};
    
    int size = sizeof(array)/sizeof(int);
    
    int size_1 = 5;
    int size_2 = size - size_1;
    
    int *array_1 = new int[size_1];
    int *array_2 = new int[size_2];
    
    for(int i = 0; i < size_1; ++i)
        array_1[i] = array[i];
    
    for(int i = 0; i < size_2; ++i)
    array_2[i] = array[i + size_1];
    
    
    std::cout << "Split occurs at " << size_1 << '\n';
    
    for(int i = 0; i < size_1; ++i)
        std:: cout << array_1[i] << ' ';
    std::cout << '\n';
    
    for(int i = 0; i < size_2; ++i)
        std:: cout << array_2[i] << ' ';
    std::cout << '\n';
    
    return 0;
}


Split occurs at 5
1 2 3 4 5 
6 7 8 9 
Program ended with exit code: 0
thank you for reviving the engine kind sir.
So obviously this didn't work...lol......so the problem here is I get my pivot point with t. So i figured okay so the size of 1 and 2 should be found before t for one and after t for 2
so If this shot in 7 numbers and t came out as the 3 number. The first two numbers should fill array 1 and the 4 last ones should feel array 2.......now the way I have it now size_1 seems to work, and them size_2 does some funky stuff....and shoots out a big blank

void split_up_array(int array[], int arraySize, int *& array1, int *& array2, int & size_1, int & size_2)
{

arraySize=sizeof(array);
int t= array[rand() % arraySize];
size_1=arraySize<t;
size_2=arraySize>t;


cout<<"The array is divided at this number "<<t<<endl;
for (int i = 0; i < size_1; i++)
{
array1[i] = array[i];
}



for(int i = 0; i <size_2; i++)
{
array2[i] = array[i];
}


}
partitioning the original array based on a random number(#include <random>), declaring 2 new arrays on the heap and using std::copy:
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
#include <iostream>
#include <algorithm>
#include <random>
#include <chrono>
#include <memory>

const int myArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};


int main()
{
     auto seed = std::chrono::system_clock::now().time_since_epoch().count();//seed
     std::default_random_engine dre(seed);//engine
     auto myArraySize = sizeof(myArray)/sizeof(myArray[0]);
     std::uniform_int_distribution<size_t> di(0,myArraySize);//distribution


    auto partitionPoint = di(dre);//array partitioned by a random number b/w 0 and it's size
    auto backEnd = myArraySize - partitionPoint;//back-end of the array
    std::cout << "Partition at element " <<  partitionPoint << "\n";;

    int* frontArray = new int[partitionPoint];//declaring the 2 new arrays on the heap
    int* backArray = new int[backEnd];//suggestion - replace with std::unique_ptr

    std::copy(myArray, myArray + partitionPoint, frontArray);//filling the new arrays with the numbers
    std::copy(myArray + partitionPoint, myArray + myArraySize, backArray);

    for (size_t i = 0; i < partitionPoint; ++i)std::cout << frontArray[i] << " ";
    std::cout << "\n";
    for (size_t i = 0; i < backEnd; ++i)std::cout << backArray[i] << " ";
    std::cout << "\n";

    delete [] frontArray;
    delete [] backArray;
}
closed account (48T7M4Gy)
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
#include <iostream>

void print_array( const int[], const int);
void split_array(const int[], const int, int, int[], int[]);

int main()
{
    int array[] = {1,2,3,4,5,6,7,8,9};
    int size = sizeof(array)/sizeof(int);
    print_array(array, size);
    
    int pivot = 5;
    int size_1 = pivot;
    int size_2 = size - pivot;
    
    int *array_1 = new int[size_1];
    int *array_2 = new int[size_2];

    
    split_array(array, size, pivot, array_1, array_2);
        
    
    print_array(array_1, size_1);
    print_array(array_2, size_2);
    
    return 0;
}

void print_array(const int anArray[], const int array_size){
    for(int i = 0; i < array_size; ++i)
        std::cout << anArray[i] << ' ';
    std::cout << '\n';
    
}


void split_array(const int aArray[], const int aSize, const int aPivot, int first_array[], int second_array[]){
    
    std::cout << "Split occurs at " << aPivot << '\n';
    
    for(int i = 0; i < aPivot; ++i)
        first_array[i] = aArray[i];
    
    for(int i = 0; i < aSize - aPivot; ++i)
        second_array[i] = aArray[i + aPivot];
}
1 2 3 4 5 6 7 8 9 
Split occurs at 5
1 2 3 4 5 
6 7 8 9 
Program ended with exit code: 0
I suggest to go through your code a few times and search for those kind of errors as they can soend a lot of time finding them, Especially with c++. If you find it troubling you can also try using a program to help you do that. I tend to use checkmarx for code security.
Anyway, good luck!
Ben.
Topic archived. No new replies allowed.