Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ArrayWithLength combine(ArrayWithLength arr1, ArrayWithLength arr2) {
- ArrayWithLength ret;
- ret.arr = (uint16_t *) malloc(sizeof(uint16_t) * 0);
- ret.len = 0;
- int len1 = arr1.len;
- int len2 = arr2.len;
- /*if(len1 == 0){ //das erste array leer
- if(len2 != 0) ret = base_case(arr2); //beide arrays leer
- return ret; //in beiden faellen (unabhaebgig von der erfuelung der if bedingung) ist ret schon fertig geschrieben und muss zurueckgegeben werden
- }
- else if(len2 == 0){ //das zweite array leer
- if(len1 != 0) ret = base_case(arr1);
- return ret;
- }
- */
- ret.arr = (uint16_t *) realloc(ret.arr,sizeof(uint16_t) * (len1+len2));
- ret.len = len1+len2;
- //printf("array1[0] = %u\n", arr1.arr[0]);
- //indizen fuer jeweiliges array
- size_t arr1_count = 0;
- size_t arr2_count = 0;
- size_t i=0; //anstatt eine for schleife zu machen, den index fuer rueckgabe array in der while schleife erhoehen
- while(arr1_count < len1 && arr2_count < len2){
- if(arr1.arr[arr1_count]>arr2.arr[arr2_count]){ //groessere zahl einfuegen
- ret.arr[i] = arr1.arr[arr1_count];
- arr1_count++;
- }
- else{
- ret.arr[i] = arr2.arr[arr2_count];
- arr2_count++;
- }
- i++;
- }
- while(arr1_count < len1){
- ret.arr[i] = arr1.arr[arr1_count];
- i++;
- arr1_count++;
- }
- while(arr2_count < len2){
- ret.arr[i] = arr2.arr[arr2_count];
- i++;
- arr2_count++;
- }
- return ret;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement