Hey guys, so i have this assignment that i cant figure out. I need to take two sorted arrays of type double and put them into another array of type *double. when i run the code it displays a bunch of -0.00's. Any help would be appreciated.
when you run the code you don't get any errors / segfaults?
you only get arr3 full of -0.00's?
sounds like you're never saving the values from arr1 or arr2 into the array. or, arr1 and arr2 are full of 0's in the beginning (doubtful but worth checking)
>>54478
well i know that arr1 and arr2 arent empty.
There are no errors or segfaults during compiling.
not really sure why it doesn't save the values.
Doesn't look like you ever initialize k.
>>54489
when i initialize j and k it compiles and then crashes.
>>54487
one thing i see is when you malloc for the array you should say
arr3 = mal...
instead of
*arr3 = mal....
but also, i haven't written anything in C / C++ for a year...
>>54494
Ive tried setting j and k to 0 in the line that i defined them as well as setting them after that in a seperate line. just crashes every time
>>54491
tried this and it also crashes after compiling
>>54496
try this verbatim:
int i = 0;
int j = 0;
int k = 0;
>>54502
nope. crashes. thanks tho
How is arr3 being declared before your call to merge?
>>54505
Here is the code for the starting part:
#include <stdio.h>
#include <stdlib.h>
void merge(double arr1[], double arr2[], double *arr3[], int sz1, int sz2, int *sz3);
void print_arr(double arr[], int sz);
int main(){
double v0[] = {};
double v1[] = {22.0, 30.1, 35.2, 49.2, 56.4, 65.1, 76.4, 79.6, 86.2, 88.9};
double v2[] = {4.6, 9.0, 13.0, 47.8, 66.9, 68.7, 71.0};
double v3[] = {80.1, 92.7, 97.4, 102.3, 105.0, 112.3, 121, 136.7, 163.4, 177.9};
int sz1 = 10, sz2 = 7, sz3 = 10, szres;
double *res;
printf("\nTEST 1\n======\n");
merge(v1, v2, &res, sz1, sz2, &szres);
print_arr(res,szres);
free(res);
>>54504
which line does it crash on?
if you can't do int i = 0; something way bigger is fucked
>>54507
You have res declared as a double*, but it's being passed to merge as a double**. And in merge it's being treated as if it were a double**.
Try changing res to a double**.
>>54509
it crashes at the line *arr3[k] = arr1[i];
>>54518
I got this tip from someone else the other day but the assignment says i cant change anything in main or the print function.
>>54524
Oh OK. I was wrong anyway, since you're passing &res and not just res, so that's OK. But if it's crashing on the first assignment to arr3 in merge, there must be something wrong about arr3.
>>54520
ok, go ahead and keep in the code where you initialize i j and k to 0 because if you don't you can't guarantee repeat performance. the code isn't crashing b/c you initialize i j and k to 0.
i also agree with >>54518
and to go back to my first post. the values from arr1 / arr2 are never getting into arr3...which could easily be explained by pointer mishandling
I'm thinking it's maybe this syntax in merge:
*arr3[k] = ...
I think this is first getting arr3[k], and then dereferencing whatever that is. But you want to dereference first. Try changing that to:
(*arr3)[k] = ...
in all four places. I don't use the square brackets syntax for C arrays, personally I think it's confusing.
O.K. so i switched the while loops to for loops and tired what >>54547 suggested. It now runs as if i still am using the while loops and did not have i,j,k initiallized.
Well the assignment is due in 10 minutes. Thanks to everyone for trying. Really appreciate it. Even learned some stuff that will definitely help in the future.
>>54552
since you've made a few changes at this, can you post your current code so we can reference that?