【问题标题】:Remove unnecessary value entries from multidimensional array in c?从c中的多维数组中删除不必要的值条目?
【发布时间】:2020-07-20 14:13:51
【问题描述】:

您好,我正在处理用户输入多个不同长度的连续数组的场景,我想存储这些数组以供进一步使用。

我为此目的使用多维数组。 这是代码:

#include <stdio.h>

int main()
{
    int rows,cols;
    
    printf("Enter the number of user input arrays ? ");
    scanf("%d",&rows);
    
    printf("Enter the maximum number of inputs in a single array ?"); //Need to remove these lines
    scanf("%d", &cols); //Need to remove these lines if possible
    
    int array[rows][cols];
    for(int i=0;i<rows;i++)
    {
        for(int j=0;j<cols;j++)
        {
            array[i][j]=0;
        }
    }
    
    for(int i=0;i<rows;i++)
    {
        int count;
        printf("Enter the number of inputs for array %d - ", i);
        scanf("%d",&count);
        for(int j=0;j<count;j++)
        {
            scanf("%d",&array[i][j]);
        }
    }
    
    //// Use array for other purpose

    ////printf("\n\nArray --> \n");
    ////for(int i=0;i<rows;i++)
    ////{
        ////for(int j=0;j<cols;j++)
        ////{
            ////printf("%d ",array[i][j]);
        ////}
        ////printf("\n");
    ////}
    
    return 0;
}

示例输入:

输入用户输入数组的数量? 5
输入单个数组中的最大输入数 ?5
输入数组 0 - 5 的输入数量
1 2 6 3 5
输入数组 1 - 1 的输入数量
3
输入数组 2 - 2 的输入数量
6 5
输入数组 3 - 1 的输入数量
3
输入数组 4 - 1 的输入数量
9

在这种情况下创建的数组:

1 2 6 3 5
3 0 0 0 0
6 5 0 0 0
3 0 0 0 0
9 0 0 0 0

现在我在这种情况下遇到了一些问题:

  1. 我想通过删除数组中不必要的条目来减少使用的空间。

  2. 我不想使用“0”或任何其他整数来定义不必要的条目,因为它是有效的输入。

  3. 我想删除该行

    printf("输入单个数组的最大输入数?"); scanf("%d", &cols);

谁能帮我解决这些问题。

【问题讨论】:

  • 这将需要创建和重新创建指针数组,每个指针的容量由运行时用户输入决定。做到这一点的唯一方法是动态分配内存,使用 [c][m]alloc 和可能的 realloc。这是您的预期吗?
  • 为什么不想要"Enter the maximum number of inputs in a single array ?"这一行,用户怎么知道该怎么做?
  • 您可能会从了解更多关于 sparse matrices 的信息中受益。
  • @ryyker 那么它会像锯齿状数组还是其他东西,我只需要在第一个输入中获取总数组,然后根据用户输入决定内部数组的大小。
  • @FredLarson - 稀疏矩阵是否提供超出初始定义的运行时修改?

标签: arrays c


【解决方案1】:

根据您描述的设计标准:

  • 用户确定行数的数组。
  • 行有不同的长度,也是用户确定的。
  • 减少正在使用的空间。 (仅用于实际输入的空格,没有填充或填充值。)
  • 在运行时根据用户输入创建数组定义,但不需要在同一运行时会话期间更改。

注意:一个设计标准://Need to remove these lines if possible 不包含在此解决方案中。没有描述期望方法来指导用户,不知道如何改进用户提示方法。

Jagged arrays 可能就是您要找的。以下是直接来自链接的简单示例,其中包含可以适应您已经讨论过的代码的动态内存分配:

int main() 
{ 
    int rows;
    //Place you user input prompts and scans here
    
    // User input number of Rows 
    int* jagged[2];// 
  
    // Allocate memory for elements in row 0 
    jagged[0] = malloc(sizeof(int) * 1); 
  
    // Allocate memory for elements in row 1 
    jagged[1] = malloc(sizeof(int) * 3); 
  
    // Array to hold the size of each row 
    int Size[2] = { 1, 3 }, k = 0, number = 100; 
  
    // User enters the numbers 
    for (int i = 0; i < 2; i++) { 
  
        int* p = jagged[i]; 
  
        for (int j = 0; j < Size[k]; j++) { 
            *p = number++; 
  
            // move the pointer 
            p++; 
        } 
        k++; 
    } 
  
    k = 0; 
  
    // Display elements in Jagged array 
    for (int i = 0; i < 2; i++) { 
  
        int* p = jagged[i]; 
        for (int j = 0; j < Size[k]; j++) { 
  
            printf("%d ", *p); 
            // move the pointer to the next element 
            p++; 
        } 
        printf("\n"); 
        k++; 
        // move the pointer to the next row 
        jagged[i]++; 
    } 
  
    return 0; 
} 

这是一个更接近我认为你想要的概念,改编自上面的代码以接受类似于你的代码所做的用户输入......

int main(int argc, char *argv[]) 
{ 
    int rows = 0;
    int cols = 0;
    int i, j;
    
    int number = 100; 
    
    printf("Enter the number of user input arrays ? ");
    scanf("%d",&rows);
    
    // n Rows 
    int* jagged[rows]; 
    int Size[rows];//array to keep size if each array

    for(i=0;i<rows;i++)
    {
        printf("Enter the maximum number of inputs for array[%d]: ", i); 
        scanf("%d", &cols); //Need to remove these lines if possible

        // Allocate memory for elements in row 0 
        jagged[i] = malloc(sizeof(jagged[i]) * cols);
        Size[i] = cols;//set size of nth array
    }
    
    // User enters the numbers (This is spoofed.  You will need to code per comment below. 
    for (i = 0; i < rows; i++) { 
  
        int* p = jagged[i]; 
  
        for (j = 0; j < Size[i]; j++) { 
            *p = number++; //Note, this is spoofing user input .
                           //actual user input would be done exactly as above
                           //with printf prompts and scans for value
            // move the pointer 
            p++; 
        } 
    } 
  
    // Display elements in Jagged array 
    for (i = 0; i < rows; i++) { 
  
        int* p = jagged[i]; 
        for (int j = 0; j < Size[i]; j++) { 
  
            printf("%d ", *p); 
            // move the pointer to the next element 
            p++; 
        } 
        printf("\n"); 

        // move the pointer to the next row 
        jagged[i]++; 
    } 
  
    return 0; 
} 

【讨论】:

  • 此代码仅对使用 C99、C11 或 C18 的编译器有效。它不适用于 C89。
  • @Lucas - 我不确定您希望我对此做什么,即C89 不是 OP 列出的标准之一。它很古老,但感谢您指出。
  • 不,不。不要误解它,伙计。 @ryyker 我只是告诉你,我曾经在清单中声明所有变量。但是您使用的是仅在 C99 之后才支持的 VLA,所以感觉就像我们在用 C++ 编码(尽管它 >=c99)。 使用前声明。否则,你的答案很好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-14
  • 2012-01-21
  • 1970-01-01
  • 1970-01-01
  • 2017-01-01
  • 1970-01-01
  • 2014-11-17
相关资源
最近更新 更多