Why does my array print differently just by simply adding a useless printf?
By having that printf statement, my code prints out the correct sorted array. without it, it acts weird. I read something about it not being synchronized, but how do i fix that?
@wio
any ideas wio? i've been stuck on this for so loong. :( also i need help writing an assembly method that gets the min value from an array. but this problem is my main focus rn.
I'm not sure. I don't see how the print statement would change anything.
should i take a screen shot of the different messages?
somewhere on stackoverflow it said that the printf has a synchonized(this) in its actual code. so, it lets the thread know it's the same.
and without it, the variables stay separate and get cached or something.
http://meta.stackoverflow.com/questions/269174/questions-about-threadloop-not-working-without-print-statement << this was the post. howeve,r i can't fix it evne reading this.
You're not using Java though, right? Also, are you doing a multi-threaded program?
i'm not sure. what is meant by a multithreaded program. i'm doing c.
I'm calling the insert functions many times, yes.
If it is a single threaded program, you don't need to worry about syncronization.
how do i know if it is multi-threaded prog?
It will create a thread. Something like pthread in C
well it seems like i am running a multi-threaded program then. i don't know where it says pthread, but it's not wokring without the print statement.
wait, would you happen to know how ot write basic assembly code?
If you haven't learned about threads yet, then you shouldn't be having a synchronization issue in your class.
we learned about threads in previous classes, but nothing was mentioned about it in this one.
but um, i'll try to figure this out. However, i really need help with writing an assembly code that gets the min value from an array. i know how to do arithmetic operations. however i do not know how to do get teh value. Any help?
I can help you come up with a better insert function.
ok!
``` void resize(list *ls) { ls->maxSize = ls->maxSize * 2; ls->sortedList = (int *) realloc(ls->sortedList); } int insert(list *ls, int val) { int position; // insert position int i; // index used for shifting array if (ls->size == ls->maxSize) { resize(ls); } // find the insertion position for (position= 0; position < ls->size; position++) { if (ls->sortedList[position] > val) { break; } } // shift the elements after insertion points over for (i = ls->size; i > position; i--) { ls->sortedList[i + 1] = ls->sortedList[i]; } // set array value ls->sortedList[position] = val; ls->size++; return position; } ``` You shouldn't check for a pointer to be `null`. Ultimately, you should never pass a null pointer into a function which requires a non-null value.
Hmmm, I think one line I put was wrong. ``` ls->sortedList[i + 1] = ls->sortedList[i]; ``` Should be ``` ls->sortedList[i] = ls->sortedList[i - 1]; ```
You should create a new question for your assembly thing.
ok!
Join our real-time social learning platform and learn together with your friends!