【发布时间】: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 矩阵中删除行和列并显示生成的子矩阵。
-
那为什么不简单地按照函数描述的方式做,用新的
nrows和ncols值创建一个新的struct matrix并相应地存储剩余的值呢?尝试对其进行编码,如果您遇到困难,请询问它。不过,嵌套循环可能会起作用。
标签: c pointers multidimensional-array