what's wrong with this code
``` #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(); } ```
it's purpose is supposed to arrange a given data by increasing order
@rambo2210 think you can solve this?
yeap sure. wait for a while.
sure
yeap got the solution .but this time you should help youself.
first of all you have to use 2 different variable in for loops.
what do you mean? cntr and counter? like that?
yeap. because if you use the same then outer loop will be execute only once. check properly.
hmm...so that's why they do that in matrices
yeap. not only that you are not done yet. check out.
yes i see...it just displayed exact order i input
let me try thinking how to make it arrange
yeap. take your time. and still dont get. than contact me. I have whole code written
hmm if data [cntr] < data[cntr + 1] then i should compare data[cntr + 1] to the others right?
yeap.
hmm i can't seem to see how to do that
you code will work perfect if you provide the numbers in decreasing order. try this,
provide the numbers in decreasing order?
sry sry ..still it wont work.
so what do you want to know?
wait... i think i have an idea
thats gr8.
this is very complicated
you have to take 1 temporary variable.
post here your modified code.
i haven't done anything
should post my code or you want to try.?
Hello?????? got the answer.?
still thinking
OK.
i give up
oh..sad.//
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; } } }
so what did you do here?
first I will compare:- if(data[cntr] > data[cntr + 1])) if hte condition is true:- I will swap that 2 number. thats it.
why not put temp = data[cntr] outside the if?
No you should copy to temp only if the condition of if satisfied.
hmm i'll try
it doesn't seem to work
post you code.
ah found how to fix it
still I have somthing to explain you once you are done. please post you code here.
#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(); }
i really don;t get why put temp = data[counter] in the if statement though
So where do you want to put it?
outside the for loop
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++)
ah yes...putting it before the if statement works
i meant if statement..not the for loop
It will work but it will increase the execution time,
why so? it's still just executed once...
by the way...is it really necessary to always assign variables?
it will copy data[counter] to temp everytime which is not required. you have to use temp only to swap the two number.
hmm..that makes sense
so it's necessary to assign variables always?
what? I did not get you. example please.
when you assigned data[cntr] to temp
because simply doing data[cntr] = data[cntr + 1] doesn't work
if you do data[cntr] = data[cntr + 1] you will loose the value of data[cntr] which you need.
|dw:1350122922981:dw|
Join our real-time social learning platform and learn together with your friends!