cLanguage array inverse output function question!

Asked 2 years ago, Updated 2 years ago, 52 views

#define _crt_secure_no_warnings
#include<stdio.h>
void GetIntArray(int v[], int n);
void ReverseIntArray(int v[], int n);
void PrintIntArray(int v[], int n);

int main()
{
    int tem[] = { 0 };
    int n;
    GetIntArray(tem,n);
    ReverseIntArray(tem,n);
    PrintIntArray(tem,n);
}

void GetIntArray(int v[], int n)
{
    int i;
    for (i = 0; i < n; i++)
        scanf("%d", &v[i]);
}

void ReverseIntArray(int v[], int n)
{
    int i;
    int tem2[] = { 0 };
    for (i = 0; i < n; i++)
        tem2[i] = v[i];
    for (i = 0; i < n; i++)
        v[i] = tem2[n - 1 - i];
}

void PrintIntArray(int v[], int n)
{
    int j;
    for (j = 0; j < n; j++)
        printf("%d", v[j]);
}

Array inverse output function question. n integers must be entered and 1,2,3,4,5-> 5,4,3,2,1 must be given I don't know where the error is.

c array function

2022-09-20 17:22

1 Answers

The array must be fixed in size with the declaration. Now, the array declaration part is all wrong, and n seems to be a variable to store the number of values to be entered, but no values are entered, so it does not work properly.

Please refer to the revised code below.

#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
void GetIntArray(int v[], int n);
void ReverseIntArray(int v[], int n);
void PrintIntArray(int v[], int n);

#define ARRAY_SIZE 20

int main()
{
    int tem[ARRAY_SIZE] = { 0 };
    int n = 5;
    GetIntArray(tem, n);
    ReverseIntArray(tem, n);
    PrintIntArray(tem, n);
}

void GetIntArray(int v[], int n)
{
    int i;
    for (i = 0; i < n; i++)
        scanf("%d", &v[i]);
}

void ReverseIntArray(int v[], int n)
{
    int i;
    int tem2[ARRAY_SIZE] = { 0 };
    for (i = 0; i < n; i++)
        tem2[i] = v[i];
    for (i = 0; i < n; i++)
        v[i] = tem2[n - 1 - i];
}

void PrintIntArray(int v[], int n)
{
    int j;
    for (j = 0; j < n; j++)
        printf("%d", v[j]);
}


2022-09-20 17:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.