Ask your own question, for FREE!
Computer Science 25 Online
OpenStudy (curry):

Why does my array print differently just by simply adding a useless printf?

OpenStudy (curry):

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?

OpenStudy (curry):

@wio

OpenStudy (curry):

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.

OpenStudy (anonymous):

I'm not sure. I don't see how the print statement would change anything.

OpenStudy (curry):

should i take a screen shot of the different messages?

OpenStudy (curry):

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.

OpenStudy (curry):

and without it, the variables stay separate and get cached or something.

OpenStudy (curry):

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.

OpenStudy (anonymous):

You're not using Java though, right? Also, are you doing a multi-threaded program?

OpenStudy (curry):

i'm not sure. what is meant by a multithreaded program. i'm doing c.

OpenStudy (curry):

I'm calling the insert functions many times, yes.

OpenStudy (anonymous):

If it is a single threaded program, you don't need to worry about syncronization.

OpenStudy (curry):

how do i know if it is multi-threaded prog?

OpenStudy (anonymous):

It will create a thread. Something like pthread in C

OpenStudy (curry):

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.

OpenStudy (curry):

wait, would you happen to know how ot write basic assembly code?

OpenStudy (anonymous):

If you haven't learned about threads yet, then you shouldn't be having a synchronization issue in your class.

OpenStudy (curry):

we learned about threads in previous classes, but nothing was mentioned about it in this one.

OpenStudy (curry):

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?

OpenStudy (anonymous):

I can help you come up with a better insert function.

OpenStudy (curry):

ok!

OpenStudy (anonymous):

``` 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.

OpenStudy (anonymous):

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]; ```

OpenStudy (anonymous):

You should create a new question for your assembly thing.

OpenStudy (curry):

ok!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!