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

如何删除用户在c中选择的2d数组的N个行和列,c,pointers,multidimensional-array,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 fi

我正在编写一段代码,它使用一个包含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 
这是我遇到的最后一个函数

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

我知道如何在给定的索引中读取和传递值,但当涉及到不在给定的索引中读取和传递值时,我的想法被卡住了

谁能给我指出正确的方向吗


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

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


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

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

如果您不想更改
m1
,则函数
void subMatrix(结构矩阵m1,…)
在技术上是可以的,但这会生成一个额外的
m1
副本,这是无效的。最好使用
void子矩阵(const struct matrix*source,…)

您还可以使用动态分配代替固定数组
值[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;
}
结构矩阵{ int**数据; int行; int cols; }; void create(结构矩阵*m,整数行,整数列) { m->行=行; m->cols=cols; m->data=malloc(行*sizeof(int*); 对于(int r=0;rdata[r]=malloc(sizeof(int)*cols); } 空洞破坏(结构矩阵*m) { 对于(int i=0;irows;i++) 自由(m->数据[i]); 免费(m->数据); } 无效打印(常量结构矩阵*m) { 对于(int r=0;rrows;r++) { 对于(int c=0;ccols;c++) printf(“%4d”,m->data[r][c]); printf(“\n”); } printf(“\n”); } 无效更改(结构矩阵*新建,结构矩阵*m,整数*删除行,整数*删除列) { int行=0; 对于(int row=0;rowrows;row++) 如果(!删除_行[行]) 行++; int cols=0; 对于(int col=0;colcols;col++) 如果(!删除列[col]) cols++; 创建(新建、行、列); int next_row=0; 对于(int row=0;rowrows;row++) { 如果(删除_行[行])继续; int next_col=0; 对于(int col=0;colcols;col++) { 如果(删除列[col])继续; 新建->数据[下一行][下一列]=m->数据[行][列]; next_col++; } 下一行++; } } 内部主(空) { 结构矩阵m; 创建(&m,10,10); 对于(int r=0;r所有矩阵都是10x10吗?使用该定义创建一个较小的矩阵似乎是不可能的。@cadaniluk输入矩阵可以达到10x10,尺寸由用户输入定义,.txt文件有一个10x10整数的列表。因此,如果用户输入7x4,我将从7x4矩阵中删除行和列,并显示生成的子矩阵。那么,为什么不简单地按照函数描述的操作,使用新的
nrows
ncols
值创建一个新的
struct矩阵
,并相应地存储剩余的值呢?试着编写代码,如果你被卡住了,问一下。不过,嵌套循环可能会起作用。我应该提到,我必须使用声明的函数,但是,您的答案非常有用。星期二我有一个考试,之后我会回到我的问题,我会发布我的解决方案,或者要求进一步澄清。感谢您抽出时间回答我的问题。