+ 1

I want a iterative and without function c program for verifying if a vector is sorted increasily or not,is my code correct?

int main(){ int t[]={1,0,7,6}; int i; int n=0; for(i=0;i<4;i++){ if(t[i]<t[i+1]) n=n+1; } if(n==0) printf("not sorted"); else printf("sorted"); return 0; }

13th Feb 2021, 1:58 PM
Coderalgeria
Coderalgeria - avatar
9 Respuestas
+ 2
Coderalgeria ok, quite good. First you forget one = by if(croiss=0). After this change works partially good, if this can be so said. The big mistake is by the index. If your vector will be {0,1,6,7} then your index x+1 will be out of declared array and the result can be not true, that means not as expected. Your while loop has to be at 1 shorter then length of your array. You have to execute this code a few times then you will see what I mean and what happend https://code.sololearn.com/cJOHI7GeNSSJ/?ref=app
14th Feb 2021, 8:32 AM
JaScript
JaScript - avatar
+ 2
Yes, here it has to be i<3 You can write generally and first determine the length of an array and then prepare the loop with i<length-1.
14th Feb 2021, 12:40 PM
JaScript
JaScript - avatar
+ 1
As you can see the code result „sorted“ for your vector‘s example is not correct.
13th Feb 2021, 2:36 PM
JaScript
JaScript - avatar
+ 1
First you need to outline what algorithm can determine this. You can do it on paper with small vectors of length 3 or 4. After that it can be implemented in code. Otherwise it would not be your work and you will not learn anything.
13th Feb 2021, 2:57 PM
JaScript
JaScript - avatar
+ 1
Ok how about this code: I want a iterative and without function c program for verifying if a vector is sorted increasily or not,is my code correct? int main(){ int t[]={1,0,7,6}; int croiss=1; int i=0; while((i<4)&&(croiss==1)){ if(t[i]>t[i+1]) croiss=0; else i++; } if(croiss=0) printf("not increased"); else printf("increased"); return 0; }
13th Feb 2021, 7:46 PM
Coderalgeria
Coderalgeria - avatar
+ 1
I see,thank you JaScript .
14th Feb 2021, 12:48 PM
Coderalgeria
Coderalgeria - avatar
+ 1
You are welcome Coderalgeria and happy coding.
14th Feb 2021, 12:53 PM
JaScript
JaScript - avatar
0
Yeah can anyone correct me
13th Feb 2021, 2:49 PM
Coderalgeria
Coderalgeria - avatar
0
JaScript do u mean : int main(){ int t[]={1,0,7,6}; int croiss=1; int i=0; while((i<3)&&(croiss==1)){ if(t[i]>t[i+1]) croiss=0; else i++; } if(croiss==0) printf("not increased"); else printf("increased"); return 0; }
14th Feb 2021, 12:08 PM
Coderalgeria
Coderalgeria - avatar