打印最大尺寸为 20 的反向数组

Print Reverse array of Max size 20

#include <stdio.h>

void ArrayReverese(int a[], int Start, int End);
void printArray(int a[], int Size);
int main()
{
    int a[20], i, Size;
    int max_size = 20;

    printf("\nPlease Enter the size of an array: ");
    scanf_s("%d", &Size);
    while (Size <= 20)
    {
        //Inserting elements into the Declared Array
        for (i = 0; i < Size; i++)
        {
            scanf_s("%d", &a[i]);
        }

        ArrayReverese(a, 0, Size - 1); //Array Reverse
        printf("Result of an Reverse array is: \n");
        printArray(a, Size); //Printing Array
        return 0;
    }
    printf("Max size of array is 20");
}
/* Function to Reverse the Given Array */
void ArrayReverese(int a[], int Start, int End)
{
    int Temp;
    while (Start < End)
    {
        Temp = a[Start];
        a[Start] = a[End];
        a[End] = Temp;
        Start++;
        End--;
    }
}
/* Function to print the Array Output */
void printArray(int a[], int Size)
{
    int i;
    for (i = 0; i < Size; i++)
    {
        printf("%d ", a[i]);
    }
    printf("\n");
}

我正在编写代码来打印反向数组的输出。数组的最大大小只能是 20。我放了一个 max_size = 20.

的 while 循环

这是不让数组大于20的最好方法吗?

a[20]有什么帮助?它是否适合最大尺寸?

Is this the best way to not let the array be greater than 20?

我认为最好的选择是确保输入的大小在要求的区间内。你需要类似这样的东西(评论中的解释):

int main(){
    
    int a[20], Size = 0;
    int max_size = 20;

    printf("\nPlease Enter the size of an array: ");

    // loop keeps asking for a size if it's not in the interval
    do
    {
        int ret = scanf_s("%d", &Size);

        if(ret != 1){   // if the input is not parsed correctly...
            printf("Bad input. "); 
            int c;
            while((c = getchar()) != '\n' && c != EOF ) {} // clear the buffer...
        }
    // size must be more than 0 and less than 20, if not ask again
    } while (Size >= max_size &&  Size < 1 && printf("Max size of array is 20, try again: "));

    // Size is guaranteed to be [1, 20[ no further checks needed here
    for (int i = 0; i < Size; i++)
    {
        scanf_s("%d", &a[i]); // check the return of this scanf also
    }

    ArrayReverese(a, 0, Size - 1); //Array Reverse
    printf("Result of an Reverse array is: \n");
    printArray(a, Size); //Printing Array
}

What does the a[20] help? Does it make for max size?

一个数组必须有一个大小,在本例中是 20,这是它的最大大小,但这并不能阻止它被溢出,由程序员来防止这种情况发生,如下所示上面的代码。