我不想为你做功课。但是,我知道看到一个有效的例子有时会有所帮助。这是一个非常超级的基本示例,但它应该可以帮助您理解这个问题背后的逻辑。
请研究这个答案,而不是仅仅提交它是正确的。你只会通过学习获得洞察力。
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> const int asc = 1; const int desc = 0; int array_asc[11] = {1,2,3,4,5,6,7,8,9,10}; int array_desc[11] = {10,9,8,7,6,5,4,3,2,1}; int array_rand[11] = {7,3,8,4,5,1,6,2,4,4}; int order_check[2] = {0, 0}; int check_order(int* array){ if(array[0] < (array[1])){ order_check[0] = asc; } else if (array[1] < (array[0])){ order_check[0] = desc; } else { return 1; } return 0; } int check_array(int* array, int* check){ switch(check[0]){ case 1: for(int i = array[0]; i < array[9]; i ++){ if(array[i] < (array[i+1])){ continue; } else { return i + 1; } }; break; case 0: for(int i = 0; i < 9; i++){ if(array[i] > (array[i+1])){ continue; } else { return i + 1; } }; break; } return 0; } int main(int argc, char** argv) { int ret; if(check_order(array_rand) == 0){ if(ret = check_array(array_rand, order_check)){ printf("Order breaks at index %d", ret); } else { printf("Array is in perfect order"); } } else { printf("Array is not in order.\n"); } return (EXIT_SUCCESS); }