Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 在c中从多维数组中删除不必要的值项?_Arrays_C - Fatal编程技术网

Arrays 在c中从多维数组中删除不必要的值项?

Arrays 在c中从多维数组中删除不必要的值项?,arrays,c,Arrays,C,您好,我正在处理一个场景,其中用户输入多个不同长度的连续数组,我希望存储这些数组以供进一步使用 为此,我使用多维数组。 代码如下: #include <stdio.h> int main() { int rows,cols; printf("Enter the number of user input arrays ? "); scanf("%d",&rows); printf(&q

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

为此,我使用多维数组。 代码如下:

#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;
}
#包括
int main()
{
int行,cols;
printf(“输入用户输入数组的数量?”);
scanf(“%d”,行和行);
printf(“输入单个数组中的最大输入数?”);//需要删除这些行吗
scanf(“%d”,&cols);//如果可能,需要删除这些行
int数组[行][cols];

对于(int i=0;i,根据您描述的设计标准:

  • 具有用户确定的行数的数组
  • 行具有不同的长度,也由用户确定
  • 减少使用的空间。(空间仅用于实际输入,无填充或填充值。)
  • 数组定义是在运行时按用户输入创建的,但不需要在同一运行时会话中更改
注意:一个设计标准:
//如果可能的话需要删除这些行
不包括在这个解决方案中。没有所需方法的说明来指导用户,我不知道如何改进用户提示方法

可能就是您要寻找的。下面是一个简单的示例,直接来自链接,该链接包含动态内存分配,可根据您已经讨论过的代码进行调整:

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; 
} 
intmain()
{ 
int行;
//在此处放置用户输入提示和扫描
//用户输入行数
int*锯齿状[2];//
//为第0行中的元素分配内存
锯齿状[0]=malloc(sizeof(int)*1);
//为第1行中的元素分配内存
锯齿状[1]=malloc(sizeof(int)*3);
//数组来保存每行的大小
int Size[2]={1,3},k=0,number=100;
//用户输入数字
对于(int i=0;i<2;i++){
int*p=锯齿状[i];
对于(int j=0;j
这是一个更接近我认为您想要的概念,根据上面的代码改编,以接受与您的代码类似的用户输入

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; 
} 
intmain(intargc,char*argv[])
{ 
int行=0;
int cols=0;
int i,j;
整数=100;
printf(“输入用户输入数组的数量?”);
scanf(“%d”,行和行);
//n行
int*锯齿状[行];
int Size[rows];//如果每个数组

对于(i=0;它需要创建和重新创建指针数组,每个指针的容量由运行时用户输入决定。唯一的方法是使用[c][m]alloc和realloc动态分配内存。这是您预期的吗?为什么不希望在单个数组中输入最大数量的输入
用户如何知道该做什么?您可能会从了解更多信息中受益。@Ryker那么它是否会像锯齿数组或其他什么,我只需要在第一次输入中获取总数组,然后根据用户输入确定内部数组的大小。@Fredrarson-稀疏矩阵是否提供了超出初始定义的运行时修改初始化?这段代码只对使用C99、C11或C18的编译器有效。它对C89不起作用。@Lucas-我不知道你想让我做什么,也就是说,
C89
不是OP列出的标准之一。它很古老,但感谢你指出。不,不,不要误解它,朋友。@Ryker我刚刚告诉你了,我用于声明列表中上面的所有变量。但您使用的VLA仅在C99之后才受支持,所以感觉就像我们在用C++编写代码(虽然它>=C99)。请在使用前声明。否则,您的答案很好。