C语言的功能。 +阵列[关闭]

问题描述 投票:-5回答:1

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

我必须写一个函数,如果数组是向下序列,则返回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;
 }
c
1个回答
1
投票

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

请研究这个答案,而不是仅仅提交它是正确的。你只会通过学习获得洞察力。

#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);
}
© www.soinside.com 2019 - 2024. All rights reserved.