英文:
there is an error in finding the array is sorted or not
问题
#include <stdio.h>;
int ret(int[], int);
int main() {
int n;
scanf("%d", &n);
int i;
int a[n];
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
ret(a, n);
return 0;
}
int ret(int a[], int n) {
int i, c1 = 0, c2 = 0;
if (a[n - 1] >= a[n - 2] && a[0] <= a[1]) {
for (i = 0; i < n - 1; i++) {
if (a[i] <= a[i + 1]) {
c1++;
}
}
if (c1 == n - 1) {
puts("已排序");
} else {
puts("未排序");
}
} else if (a[n - 1] <= a[n - 2] && a[0] >= a[1]) {
for (i = 0; i < n - 1; i++) {
if (a[i] >= a[i + 1]) {
c2++;
}
}
if (c2 == n - 1) {
puts("已排序");
} else {
puts("未排序");
}
}
return 0;
}
这是您提供的C代码的翻译,只包含代码部分,没有其他内容。此代码用于检查数组是否已排序。如果数组已排序,它将打印"已排序",否则将打印"未排序"。如果您在运行代码时遇到问题,请提供更多详细信息,以便我可以帮助您解决问题。
英文:
#include <stdio.h>
int ret(int[], int);
int main() {
int n;
scanf("%d", & n);
int i;
int a[n];
for (i = 0; i < n; i++) {
scanf("%d", & a[i]);
}
ret(a, n);
return 0;
}
int ret(int a[], int n) {
int i, c1 = 0, c2 = 0;
if (a[n - 1] >= a[n - 2] && a[0] <= a[1]) {
for (i = 0; i < n - 1; i++) {
if (a[i] <= a[i + 1]) {
c1++;
}
}
if (c1 == n - 1) {
puts("SORTED");
} else {
puts("NOTSORTED");
}
} else if (a[n - 1] <= a[n - 2] && a[0] >= a[1]) {
for (i = 0; i < n - 1; i++) {
if (a[i] >= a[i + 1]) {
c2++;
}
}
if (c2 == n - 1) {
puts("SORTED");
} else {
puts("NOT SORTED");
}
}
return 0;
}
I want to check whether the array is sorted or not, but I can't get the output. Sometimes it gives the correct output, but sometimes it doesn't give any output. Please check the code.
答案1
得分: 1
如果这个if语句不触发if(a[n-1]>=a[n-2]&&a[0]<=a[1])
,而且这个也不触发
else if(a[n-1]<=a[n-2] &&a[0]>=a[1]){
,那么没有任何行代码可以产生任何输出。
英文:
If this if statement doesn't fire if(a[n-1]>=a[n-2]&&a[0]<=a[1])
and this one doesn't either
else if(a[n-1]<=a[n-2] &&a[0]>=a[1]){
, then there is no line of code that can produce any output.
答案2
得分: 0
如@DavidSchwartz指出,对数组的两端进行采样不考虑整个数组的方向,甚至可能排除任何“分析”。
除此之外,int a[1] = { 42 };
是一个完全有效的数组,会导致您的代码进入“未定义行为”(访问不属于数组的内存)。
尝试以下操作:
#include <stdio.h>
void ret( int a[], int n ) {
int cUP = 0, cDN = 0; // 计算上升和下降变化的总数。
for( int i = 1; i < n; i++ ) { // 注意:从1开始并向后检查
cUP += ( a[ i-1 ] < a[ i ] ); // 上升
cDN += ( a[ i-1 ] > a[ i ] ); // 下降
}
// 如果上升和下降都存在,则数组未排序。
if( cUP && cDN )
puts( "未排序" );
else
puts( "已排序" );
}
int main() {
int a[] = { 1, 2, 3 };
ret( a, sizeof a/sizeof a[0] );
int b[] = { 3, 2, 1 };
ret( b, sizeof b/sizeof b[0] );
int c[] = { 3, 2, 5 };
ret( c, sizeof c/sizeof c[0] );
int d[] = { 3, 2, 2, 2, 1 };
ret( d, sizeof d/sizeof d[0] );
int e[] = { 2, 2, 2 };
ret( e, sizeof e/sizeof e[0] );
return 0;
}
实际上,循环可以提前退出,如下所示:
for( int i = 1; i < n && (!cUP || !cDN); i++ ) {
如果两个计数器都变为非零,则数组未排序,循环可以提前结束。
英文:
As pointed out by @DavidSchwartz, sampling both ends of the array does not account for the direction of the entire array and may even preclude any 'analysis'.
Apart from that, int a[1] = { 42 };
is a perfectly valid array that would lead your code into undefined behaviour (accessing memory that is not part of the array.)
Try the following:
#include <stdio.h>
void ret( int a[], int n ) {
int cUP = 0, cDN =0; // Tally up/down variations.
for( int i = 1; i < n; i++ ) { // NB: start at 1 and check backward
cUP += ( a[ i-1 ] < a[ i ] ); // going UP
cDN += ( a[ i-1 ] > a[ i ] ); // going DOWN
}
// If both up & down found, array is not sorted.
if( cUP && cDN )
puts( "NOT SORTED" );
else
puts( "SORTED" );
}
int main() {
int a[] = { 1, 2, 3 };
ret( a, sizeof a/sizeof a[0] );
int b[] = { 3, 2, 1 };
ret( b, sizeof b/sizeof b[0] );
int c[] = { 3, 2, 5 };
ret( c, sizeof c/sizeof c[0] );
int d[] = { 3, 2, 2, 2, 1 };
ret( d, sizeof d/sizeof d[0] );
int e[] = { 2, 2, 2 };
ret( e, sizeof e/sizeof e[0] );
return 0;
}
In fact, the loop could bail out early with the following:
for( int i = 1; i < n && (!cUP || !cDN); i++ ) {
If both counters become non-zero, the array is not sorted and the loop can finish early.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论