C语言中的函数。+ 数组

乔治

我遇到了一个我自己无法解决的问题。

如果数组是向下序列,我必须编写一个返回 0 的函数,否则返回违反模式的第一个数字的编号。在向用户询问整数 N 和 N 数字数组的应用程序中使用此函数。

但我不知道该怎么做!

有我的代码。

int *AnotherArray;
int i;
int Length;

system("cls");
printf("\t\t put Array volume: ");
scanf("%d", &Length);
printf("\n\n");

AnotherArray = (int*)malloc(Length * sizeof(int));

for (i = 0; i<Length; i++)
{
    printf("\t [%d] Елемент масиву = ", i);
    scanf("%d", &AnotherArray[i]);
}
printf("\n\n");

printf("\tYour Array: ");

for (i = 0; i<Length; i++)
{
    printf("%d ", AnotherArray[i]);
}
for(i=1; i<Length; i++)

    if(AnotherArray[i-1]>AnotherArray[i])
    {
        printf("\t(From up to down!)\n");
        break;
    }
for(i=1; i<Length; i++)
    if (AnotherArray[i-1]<AnotherArray[i])
    {

        printf("\t(From down to up!)\n");
        break;
    }
for(i=1; i<Length; i++)
    if (AnotherArray[i-1]==AnotherArray[i])
    {
        printf("\t(All numbers are equal!)\n");
        break;
    }

printf("\t\t\n downward sequence stop there: ");
printf("  %d", AnotherArray[0]);

for (i = 0; i<Length; i++)
{
    for (i = 0; AnotherArray[i]>AnotherArray[i+1]; i++)
    {
        printf("  %d", AnotherArray[i+1]);
    }

    printf("\t\t\n upward sequence stop there: ");
    printf("  %d", AnotherArray[0]);

    for (i = 0; i<Length; i++)
    {
        for (i = 0; AnotherArray[i]<AnotherArray[i+1]; i++)
        {
            printf("  %d", AnotherArray[i+1]);
        }
        free(AnotherArray);
        printf("\n\n\t Press 'ENTER' to back to the menu\n");
        getch();
        {
            main();
        }
        return 0;
 }
位移

我不想为你做功课。但是,我知道有时查看工作示例会有所帮助。这是一个非常基本的示例,但它应该可以帮助您理解此类问题背后的逻辑。

请研究这个答案,而不仅仅是将其提交为正确的。你只有通过学习才能获得洞察力。

#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);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章