【问题标题】:How can I delete N number of rows and columns of a 2d array selected by the user in c如何删除用户在c中选择的二维数组的N行和列
【发布时间】:2018-05-26 14:49:32
【问题描述】:

我正在编写一段代码,该代码使用一个包含二维数组和预定函数的结构,我用 cmets 列出了这些函数,描述了函数的作用。

struct matrix
{
    char name;
    int mValues[10][10[;
    int nrows;
    int ncols;
};
/** Function Prototypes**/

// Lets user name, choose dimensions and populates matrix from a 10x10 .txt file
void matrixInput(struct matrix *matA); 

// Asks the user to choose how many rows to delete and select which rows 
// Asks the user to choose how many columns to delete and select which columns.
// The result should be a sub matrix of the input matrix stored in a new struct matrix
void subMatrix(struct matrix m1, struct matrix *m2);

// What the Input/Output should look like

How many rows do you want to delete? : 2

Please enter, one per row, the number(s) of the 2 rows you want to delete : 2
Please enter, one per row, the number(s) of the 2 rows you want to delete : 1

How many columns do you want to delete? : 3

Please enter, one per column, the number(s) of the 3 columns you want to delete : 4
Please enter, one per column, the number(s) of the 3 columns you want to delete : 2
Please enter, one per column, the number(s) of the 3 columns you want to delete : 5

// Displays sub matrix 

这是我遇到问题的最后一个函数。

我知道输入矩阵的大小,我认为我需要告诉编译器如何将输入矩阵的值传递给新的结构矩阵,同时排除用户输入的行/列号值删除。我不确定这是否可以在嵌套循环中完成,或者我是否需要其他变量来存储值。

我知道如何读取和传递给定索引处的值,但是当谈到不读取和传递给定索引处的值时,我会陷入困境。

谁能指出我正确的方向?

旁注,欢迎任何关于如何提高我的问题质量的提示。

【问题讨论】:

  • 所有矩阵都是 10x10 的吗?使用该定义创建更小的矩阵似乎是不可能的。
  • @cadaniluk 输入矩阵最大为 10x10,尺寸由用户输入定义,.txt 文件有一个 10x10 整数列表。因此,如果用户输入 7x4,我将从 7x4 矩阵中删除行和列并显示生成的子矩阵。
  • 那为什么不简单地按照函数描述的方式做,用新的nrowsncols 值创建一个新的struct matrix 并相应地存储剩余的值呢?尝试对其进行编码,如果您遇到困难,请询问它。不过,嵌套循环可能会起作用。

标签: c pointers multidimensional-array


【解决方案1】:

如果您知道要删除哪些列和行,并且确定结果将适合新矩阵,那么只需执行一个嵌套循环并告诉它忽略某个范围的值。

但您真正想做的是在复制函数中创建新矩阵并将其返回。如果它们是动态创建的,您可以忽略您尝试以相同方式(嵌套循环)复制的列或行的分配,并使其完全适合您需要的大小。

【讨论】:

    【解决方案2】:

    您不能轻易地将删除信息存储在矩阵中,因为matrix->values[0][0] 可以引用行或列。相反,它更容易声明为整数。

    如果您不想更改m1,函数void subMatrix(struct matrix m1,...) 在技术上是可以的,但是这会产生m1 的额外副本,效率不高。最好改用void subMatrix(const struct matrix *source,...)

    您也可以使用动态分配来代替value[10][10] 的固定数组。示例:

    struct matrix {
        int **data;
        int rows;
        int cols;
    };
    
    void create(struct matrix *m, int rows, int cols)
    {
        m->rows = rows;
        m->cols = cols;
        m->data = malloc(rows * sizeof(int*));
        for(int r = 0; r < rows; r++)
            m->data[r] = malloc(sizeof(int) * cols);
    }
    
    void destroy(struct matrix *m)
    {
        for(int i = 0; i < m->rows; i++)
            free(m->data[i]);
        free(m->data);
    }
    
    void print(const struct matrix *m)
    {
        for(int r = 0; r < m->rows; r++)
        {
            for(int c = 0; c < m->cols; c++)
                printf("%4d", m->data[r][c]);
            printf("\n");
        }
        printf("\n");
    }
    
    void change(struct matrix *new, struct matrix *m, int *delete_rows, int *delete_cols)
    {
        int rows = 0;
        for(int row = 0; row < m->rows; row++)
            if(!delete_rows[row])
                rows++;
        int cols = 0;
        for(int col = 0; col< m->cols; col++)
            if(!delete_cols[col])
                cols++;
        create(new, rows, cols);
    
        int next_row = 0;
        for(int row = 0; row < m->rows; row++)
        {
            if(delete_rows[row]) continue;
            int next_col = 0;
            for(int col = 0; col < m->cols; col++)
            {
                if(delete_cols[col]) continue;
                new->data[next_row][next_col] = m->data[row][col];
                next_col++;
            }
            next_row++;
        }
    }
    
    int main(void)
    {
        struct matrix m;
        create(&m, 10, 10);
        for(int r = 0; r < m.rows; r++)
            for(int c = 0; c < m.rows; c++)
                m.data[r][c] = r * 100 + c;
        print(&m);
    
        //get delete information
        int delete_rows[10] = { 0 };
        int delete_cols[10] = { 0 };
        delete_rows[0] = 1;//delete row 0
        delete_cols[7] = 1;//delete col 7
    
        struct matrix new;
        change(&new, &m, delete_rows, delete_cols);
        print(&new);
        destroy(&m);
        destroy(&new);
        return 0;
    }
    

    【讨论】:

    • 我应该提到我必须使用声明的函数,但是,您的回答非常有帮助。星期二我有一次考试,之后我会回到我的问题上,我会发布我的解决方案,或者要求进一步澄清。感谢您抽出宝贵时间回答我的问题。
    猜你喜欢
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 2016-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多