如何删除用户在c中选择的2d数组的N个行和列

时间:2018-05-26 14:49:33

标签: c pointers multidimensional-array

我正在编写一段代码,它使用包含2d数组和预定函数的结构,我已经列出了描述函数功能的注释。

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 

这是我遇到问题的最后一项功能。

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

我知道如何在给定的索引处读取和传递值,但是当我没有在给定索引处读取和传递值时,我仍然坚持这些想法。

有人能指出我正确的方向吗?

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

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

如果你不想改变void subMatrix(struct matrix m1,...),那么函数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;
}