+ 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; }
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
+ 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.
+ 1
As you can see the code result „sorted“ for your vector‘s example is not correct.
+ 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.
+ 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;
}
+ 1
I see,thank you JaScript .
+ 1
You are welcome Coderalgeria
and happy coding.
0
Yeah can anyone correct me
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;
}