Ask your own question, for FREE!
Computer Science 10 Online
OpenStudy (lgbasallote):

what's wrong with this code

OpenStudy (lgbasallote):

``` #include <stdio.h> #include <conio.h> main() { int numData, cntr; int data[10]; printf("How many numbers? "); scanf("%d", &numData); for (cntr = 0; cntr < numData; cntr++) { printf("Data %d> ", cntr + 1); scanf("%d", &data[cntr]); } for (cntr = 0; cntr < numData; cntr++) { for (cntr = 0; cntr < numData; cntr++) { if (data[cntr] > data[cntr + 1]) { data[cntr] = data[cntr + 1]; } else break; } } for (cntr = 0; cntr < numData; cntr++) { printf("%d ", data[cntr]); } getch(); } ```

OpenStudy (lgbasallote):

it's purpose is supposed to arrange a given data by increasing order

OpenStudy (lgbasallote):

@rambo2210 think you can solve this?

OpenStudy (anonymous):

yeap sure. wait for a while.

OpenStudy (lgbasallote):

sure

OpenStudy (anonymous):

yeap got the solution .but this time you should help youself.

OpenStudy (anonymous):

first of all you have to use 2 different variable in for loops.

OpenStudy (lgbasallote):

what do you mean? cntr and counter? like that?

OpenStudy (anonymous):

yeap. because if you use the same then outer loop will be execute only once. check properly.

OpenStudy (lgbasallote):

hmm...so that's why they do that in matrices

OpenStudy (anonymous):

yeap. not only that you are not done yet. check out.

OpenStudy (lgbasallote):

yes i see...it just displayed exact order i input

OpenStudy (lgbasallote):

let me try thinking how to make it arrange

OpenStudy (anonymous):

yeap. take your time. and still dont get. than contact me. I have whole code written

OpenStudy (lgbasallote):

hmm if data [cntr] < data[cntr + 1] then i should compare data[cntr + 1] to the others right?

OpenStudy (anonymous):

yeap.

OpenStudy (lgbasallote):

hmm i can't seem to see how to do that

OpenStudy (anonymous):

you code will work perfect if you provide the numbers in decreasing order. try this,

OpenStudy (lgbasallote):

provide the numbers in decreasing order?

OpenStudy (anonymous):

sry sry ..still it wont work.

OpenStudy (anonymous):

so what do you want to know?

OpenStudy (lgbasallote):

wait... i think i have an idea

OpenStudy (anonymous):

thats gr8.

OpenStudy (lgbasallote):

this is very complicated

OpenStudy (anonymous):

you have to take 1 temporary variable.

OpenStudy (anonymous):

post here your modified code.

OpenStudy (lgbasallote):

i haven't done anything

OpenStudy (anonymous):

should post my code or you want to try.?

OpenStudy (anonymous):

Hello?????? got the answer.?

OpenStudy (lgbasallote):

still thinking

OpenStudy (anonymous):

OK.

OpenStudy (lgbasallote):

i give up

OpenStudy (anonymous):

oh..sad.//

OpenStudy (anonymous):

for (cntr1 = 0; cntr1 < numData-1; cntr1++) { for (cntr = 0; cntr < numData; cntr++) { if (data[cntr] > data[cntr + 1]) { temp=data[cntr]; data[cntr] = data[cntr + 1]; data[cntr +1] =temp; } } }

OpenStudy (lgbasallote):

so what did you do here?

OpenStudy (anonymous):

first I will compare:- if(data[cntr] > data[cntr + 1])) if hte condition is true:- I will swap that 2 number. thats it.

OpenStudy (lgbasallote):

why not put temp = data[cntr] outside the if?

OpenStudy (anonymous):

No you should copy to temp only if the condition of if satisfied.

OpenStudy (lgbasallote):

hmm i'll try

OpenStudy (lgbasallote):

it doesn't seem to work

OpenStudy (anonymous):

post you code.

OpenStudy (lgbasallote):

ah found how to fix it

OpenStudy (anonymous):

still I have somthing to explain you once you are done. please post you code here.

OpenStudy (lgbasallote):

#include <stdio.h> #include <conio.h> main() { int numData, cntr, counter, temp; int data[10]; printf("How many numbers? "); scanf("%d", &numData); for (cntr = 0; cntr < numData; cntr++) { printf("Data %d> ", cntr + 1); scanf("%d", &data[cntr]); } for (cntr = 0; cntr < numData; cntr++) for (counter = 0; counter < numData; counter++) if (data[counter] > data[counter + 1]) { temp = data[counter]; data[counter] = data[counter + 1]; data[counter + 1] = temp; } for (cntr = 0; cntr < numData; cntr++) { printf("%d ", data[cntr]); } getch(); }

OpenStudy (lgbasallote):

i really don;t get why put temp = data[counter] in the if statement though

OpenStudy (anonymous):

So where do you want to put it?

OpenStudy (lgbasallote):

outside the for loop

OpenStudy (anonymous):

as a good developer you should consider the execution time also. outer for loop need to run only as follow: for (cntr = 0; cntr < numData-1; cntr++)

OpenStudy (lgbasallote):

ah yes...putting it before the if statement works

OpenStudy (lgbasallote):

i meant if statement..not the for loop

OpenStudy (anonymous):

It will work but it will increase the execution time,

OpenStudy (lgbasallote):

why so? it's still just executed once...

OpenStudy (lgbasallote):

by the way...is it really necessary to always assign variables?

OpenStudy (anonymous):

it will copy data[counter] to temp everytime which is not required. you have to use temp only to swap the two number.

OpenStudy (lgbasallote):

hmm..that makes sense

OpenStudy (lgbasallote):

so it's necessary to assign variables always?

OpenStudy (anonymous):

what? I did not get you. example please.

OpenStudy (lgbasallote):

when you assigned data[cntr] to temp

OpenStudy (lgbasallote):

because simply doing data[cntr] = data[cntr + 1] doesn't work

OpenStudy (anonymous):

if you do data[cntr] = data[cntr + 1] you will loose the value of data[cntr] which you need.

OpenStudy (anonymous):

|dw:1350122922981:dw|

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!