编写一个C程序以使用指针将一维数组转换为二维数组

编写一个C程序,使用指针将一维数组转换为二维数组。 请按照给定的步骤来实现上述代码。

  1. Ask the user to input the number of rows (​ m ​ ) and the number of columns (​ n ​ ) of the 2D array. So the total number of elements of 1D array = (​ m * n ​ ) elements.
  2. Call the function​ ​ input_array​ to store elements in 1D array.

    void input_array (int *arr,int size) // size = ​ m * n

  3. Call the function ​ print_array​ to print the elements of 1D array.

    void print_array (int *arr, int size)

  4. Call the function ​ array_to_matrix​ to convert 1D array to 2D array.

    void array_to_matrix(int **matrix, int *arr, int row, int col)

  5. Call function ​ print_matrix​ to print the elements of the 2D array.

    void print_matrix(int **mat, int row, int col)

所有函数都应从main()中调用。只能通过使用指针来访问和存储指针中的元素。

码:

#include <stdio.h>
#include <stdlib.h>
#define max 100

void input_array (int *arr,int size);
void print_array (int *arr, int size);
void array_to_matrix(int **matrix, int *arr, int row, int col);
void print_matrix(int **matrix, int row, int col);


int main()
{
    int m, n, arr[max], mat[max][max];
    printf("Enter the number of rows(m):");
    scanf("%d",&m);
    printf("Enter the number of columns(n):");
    scanf("%d",&n);
    int size = m*n;

    input_array (arr,size);
    print_array (arr,size);
    array_to_matrix(mat, arr, m, n);
    print_matrix(mat, m, n);
}

void input_array (int *arr,int size)
{
    int i;
    for(i=0;i<size;i++)
    {
        printf("Enter element a[%d]",i);
        scanf("%d",&arr[i]);    
    }
}

void print_array (int *arr, int size)
{
    int i;
    printf("\n 1D array is as follows : \n");
    for(i=0;i<size;i++)
    {
        printf("%d",arr[i]);
    }
}

void array_to_matrix(int **matrix, int *arr, int row, int col)
{
    int i,j,k=0;     
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            matrix[i][j] = arr[k];
            k++;
        }
    }

}

void print_matrix(int **matrix, int row, int col)
{
    int i,j;
    printf("\n 2D matrix is as follows : \n");
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            printf("%d ",matrix[i][j]);
        }
        printf("\n");
    }
}

错误: 我遇到了细分错误。我遇到的问题与指向数组的指针以及如何将它们传递给函数有关。

评论
  • ciste
    ciste 回复

    void main()
    {
        int m, n, arr[max], mat[max][max];
        int size = m*n;
    

    您在尚未初始化m和n的情况下计算大小,因此size的值未定义且行为未定义

    void array_to_matrix(int **matrix, int *arr, int row, int col)
    {
        int i,j,k=0;     
        for(i=0;i<row;i++)
        {
            for(j=0;j<col;j++)
            {
                matrix[i][j] = arr[k];
            }
        }
    }
    

    该函数的签名表示matrix是一个指向int的指针的数组,但这是错误的,它是一个2D数组。

    void array_to_matrix(int (*matrix)[max], int *arr, int row, int col)
    

    与print_matrix相同

    void print_matrix(int (*matrix)[max], int row, int col)
    

    Out of that note k always value 0 in array_to_matrix, so you always use arr[0]

    另外:

    无效main()

    是错误的,main返回一个int

    我还鼓励您始终检查scanf的结果,以确保使用了有效的输入,否则您将使用具有未定义行为的未初始化值。当您读取行数和列数时,值太大,您会从数组中读取/写入数据

    在print_array中用空格分隔打印值将有助于使结果可读

    结局:

    #include <stdio.h>
    #include <stdlib.h>
    
    #define max 100
    
    void input_array (int *arr,int size);
    void print_array (int *arr, int size);
    void array_to_matrix(int (*matrix)[max], int *arr, int row, int col);
    void print_matrix(int (*matrix)[max], int row, int col);
    
    
    int main()
    {
        int m, n, arr[max], mat[max][max];
        int size;
    
        printf("Enter the number of rows(m):");
        if ((scanf("%d",&m) != 1) || (m > max)) {
          puts("invalid value for rows");
          return -1;
        }
    
        printf("Enter the number of columns(n):");
        if ((scanf("%d",&n) != 1) || (n > max)) {
          puts("invalid value for columns");
          return -1;
        }
    
        size = m*n;
    
        input_array (arr,size);
        print_array (arr,size);
        array_to_matrix(mat, arr, m, n);
        print_matrix(mat, m, n);
    }
    
    void input_array (int *arr,int size)
    {
        int i;
        for(i=0;i<size;i++)
        {
            printf("Enter element a[%d]",i);
            if (scanf("%d",&arr[i]) != 1) {
              int c;
    
              puts("invalid value, redo");
    
              /* flush invalid value up to the end of line */
              while ((c = getchar()) != '\n') {
                if (c == EOF) {
                  puts("EOF, abort");
                  exit(-1);
                }
              }
    
              i -= 1;
            }
        }
    }
    
    void print_array (int *arr, int size)
    {
        int i;
        printf("\n 1D array is as follows : \n");
        for(i=0;i<size;i++)
        {
            printf("%d ",arr[i]);
        }
    }
    
    void array_to_matrix(int (*matrix)[max], int *arr, int row, int col)
    {
        int i,j,k=0;     
        for(i=0;i<row;i++)
        {
            for(j=0;j<col;j++)
            {
                matrix[i][j] = arr[k++];
            }
        }
    
    }
    
    void print_matrix(int (*matrix)[max], int row, int col)
    {
        int i,j;
        printf("\n 2D matrix is as follows : \n");
        for(i=0;i<row;i++)
        {
            for(j=0;j<col;j++)
            {
                printf("%d ",matrix[i][j]);
            }
            printf("\n");
        }
    }
    

    编译与执行:

    pi@raspberrypi:/tmp $ gcc -g -Wall c.c
    pi@raspberrypi:/tmp $ ./a.out
    Enter the number of rows(m):a
    invalid value for rows
    pi@raspberrypi:/tmp $ ./a.out
    Enter the number of rows(m):2
    Enter the number of columns(n):3
    Enter element a[0]1
    Enter element a[1]a
    invalid value, redo
    Enter element a[1]2
    Enter element a[2]3
    Enter element a[3]4
    Enter element a[4]5
    Enter element a[5]6
    
     1D array is as follows : 
    1 2 3 4 5 6 
     2D matrix is as follows : 
    1 2 3 
    4 5 6 
    pi@raspberrypi:/tmp $