C使用指针创建矩阵,然后从矩阵中复制元素
我试图使用一个函数来复制矩阵中的一行,并返回指向该行的指针,然后打印该行,但是我的get_row()函数失败了,请提供任何帮助, 随机数据是程序中指定的一部分,然后我必须以相同的方式获取列、转置和子矩阵,但我希望如果我了解如何为get_row()执行此操作,我将能够完成其余操作: 我在get_row()中的算法是错误的 这是我的密码: 我的主要意见(经修订):C使用指针创建矩阵,然后从矩阵中复制元素,c,pointers,matrix,C,Pointers,Matrix,我试图使用一个函数来复制矩阵中的一行,并返回指向该行的指针,然后打印该行,但是我的get_row()函数失败了,请提供任何帮助, 随机数据是程序中指定的一部分,然后我必须以相同的方式获取列、转置和子矩阵,但我希望如果我了解如何为get_row()执行此操作,我将能够完成其余操作: 我在get_row()中的算法是错误的 这是我的密码: 我的主要意见(经修订): int main(){ int m,n,t,检查; 双**垫,*matc; 检查=0; while(检查!=2) { printf(“\
int main(){
int m,n,t,检查;
双**垫,*matc;
检查=0;
while(检查!=2)
{
printf(“\n输入矩阵m x n的大小,格式为m,n:”);
检查=scanf(“%d,%d”,&m,&n);
_flushall();
};
int行;
mat=(双**)malloc(m*尺寸f(双*);
对于(row=0;row),我的第一个建议是在生成矩阵时使用C中的二维数组语法,而不是尝试使用一个指针访问所有元素
所以
这是如此简单的工作,你会很感激这样做。
它在内存中的开销最小。在许多算法中,由于不再涉及乘法,因此它将比使用算术获得(i,j)元素的平面(i*cols+j)坐标更有效
这样做之后,尝试重新审视手头的问题,您的代码会更简单。现在,您似乎对这个问题感到厌倦和沮丧。工作代码:
int i, j;
float** rand_matrix( float **mat, int m, int n){
float** backup = mat;
for (i=0;i<m;i++){
for (j=0;j<n;j++){
mat[i][j] = rand();
}
}
return backup;
}
//PRINTS MATRIX
void print_matrix( float **mat, int row, int col){
printf("\nMatrix dimensions: [%d,%d]\n", row, col);
for (i=0;i<row;i++){
for (j=0;j<col;j++){
printf(" %f ", mat[i][j]);
}
printf("\n");
}
}
void print_row(float **mat, int row, int cols)
{
printf("\nPrinting row: %d\n", row);
for(i = 0; i<cols; i++)
{
printf("%f", *(mat[row]+i));
}
}
//void print_col(float **mat, int rows, int col)
//{
// printf("\nPrinting col: %d\n", col);
// for(i = 0; i<rows; i++)
// {
// printf("%f ", (mat[i]+col));
// }
//}
//GET ROW
float* get_row( float **mat, int row){
return mat[row];
}
int main(){
int row, col, rownum, check;
float **mat;
float *matc;
check = 0 ;
while ( check != 2 )
{
printf("\nEnter the size of your matrix m x n in the form m,n : " );
check = scanf( "%d, %d", &row, &col );
_flushall() ;
};
mat = (float **) malloc(row * sizeof(float*)) ;
for(i = 0; i<row; i++) {
mat[i] = (float *) malloc(col * sizeof(float));
}
srand((unsigned int) time(NULL));
mat=rand_matrix(mat, row, col);
print_matrix(mat, row, col);
check = 0 ;
while ( check != 1 )
{
printf("\nEnter the row you would like to see : " );
check = scanf( "%d", &rownum );
_flushall() ;
};
matc = get_row(mat, rownum);
print_row(&matc, 0, col);
check = 0 ;
while ( check != 1 )
{
printf("\nEnter the column you would like to see : " );
check = scanf( "%d", &rownum );
_flushall() ;
}
//printf("\nMatrix column: [%d]\n", rownum);
//get_column( mat, row, col, rownum);
//getch();
//transpose( mat, row, col);
//getch();
free(mat);
}
inti,j;
浮点**随机矩阵(浮点**矩阵,整数m,整数n){
浮球**备用=垫;
例如(i=0;i运行程序时会发生什么?您希望发生什么?为了简化测试和调试,不要使用随机数据。例如,用单个值填充矩阵中的每一行(例如,0、1、2…到行-1)抱歉没有指定,我必须用这个程序解决的问题中指定的是必须使用随机数据。您可以在实际程序中使用随机数据,也可以在测试时使用非随机数据。您可以使用预处理器检查类似于调试
宏的内容,该宏仅在您自己构建时定义。完成后,使用1++对于现在的数据,下一次至少要多付一点格式化的钱,否则在破译最简单的代码行之前,没有人能帮到你。+1对于二维矩阵的建议…他正在做的令人困惑的计算将消失…很好的输入,我已经按照你的建议更改了我自己的代码,however我仍然坚持复制get_row func中的元素,你有什么建议吗?我是C级的本科生..+1,你的建议再次为我解决了另一个问题,干杯!首先显示修改后的代码。get_row与上面的代码成了一个简单的循环。这里的代码仍然是一样的…发布更新后的代码。…另外,尝试保持新代码缩进正确…为了释放内存泄漏您自己…:)另外,取消对print\u col()的注释以打印列。您将如何编写get\u col()函数?您无法获得这样的列。您需要创建一个新数组并将列元素放入其中。或者执行类似于print\u col()
正在执行的操作。
//FUNCTION TO POPULATE MATRIX WITH RANDOM DOUBLES
double *rand_matrix( double *mat, int m, int n) {
double *usermat=mat;
int i;
for (i=0;i<m*n;i++) {
*usermat++=i+1; //populates with 1 to m*n
}
return mat;
}
//PRINTS MATRIX
void print_matrix( double *mat, int m, int n) {
int i,j;
printf("\nMatrix dimensions: [%d,%d]\n",m,n);
double *usermat=mat;
for (i=0;i<m;i++) {
usermat=(mat+i*n);
for (j=0;j<n;j++) {
printf(" %0.2lf ",*usermat++);
}
printf("\n");
}
}
//GET ROW
double *get_row( double *mat, int n,int t) {
int i,j;
printf("\nMatrix row: [%d]\n",t);
double *usermat=mat;
printf("\n");
usermat=(mat+n*(t-1));
for (j=0;j<n;j++) {
*usermat++;
}
printf("\n");
return usermat;
}
double **mat;
int row;
mat = (double **) malloc(m * sizeof(double*)) ;
for(row = 0; row<m; row++) {
mat[row] = (double *) malloc(n * sizeof(double));
}
double element = mat[i][j];
int i, j;
float** rand_matrix( float **mat, int m, int n){
float** backup = mat;
for (i=0;i<m;i++){
for (j=0;j<n;j++){
mat[i][j] = rand();
}
}
return backup;
}
//PRINTS MATRIX
void print_matrix( float **mat, int row, int col){
printf("\nMatrix dimensions: [%d,%d]\n", row, col);
for (i=0;i<row;i++){
for (j=0;j<col;j++){
printf(" %f ", mat[i][j]);
}
printf("\n");
}
}
void print_row(float **mat, int row, int cols)
{
printf("\nPrinting row: %d\n", row);
for(i = 0; i<cols; i++)
{
printf("%f", *(mat[row]+i));
}
}
//void print_col(float **mat, int rows, int col)
//{
// printf("\nPrinting col: %d\n", col);
// for(i = 0; i<rows; i++)
// {
// printf("%f ", (mat[i]+col));
// }
//}
//GET ROW
float* get_row( float **mat, int row){
return mat[row];
}
int main(){
int row, col, rownum, check;
float **mat;
float *matc;
check = 0 ;
while ( check != 2 )
{
printf("\nEnter the size of your matrix m x n in the form m,n : " );
check = scanf( "%d, %d", &row, &col );
_flushall() ;
};
mat = (float **) malloc(row * sizeof(float*)) ;
for(i = 0; i<row; i++) {
mat[i] = (float *) malloc(col * sizeof(float));
}
srand((unsigned int) time(NULL));
mat=rand_matrix(mat, row, col);
print_matrix(mat, row, col);
check = 0 ;
while ( check != 1 )
{
printf("\nEnter the row you would like to see : " );
check = scanf( "%d", &rownum );
_flushall() ;
};
matc = get_row(mat, rownum);
print_row(&matc, 0, col);
check = 0 ;
while ( check != 1 )
{
printf("\nEnter the column you would like to see : " );
check = scanf( "%d", &rownum );
_flushall() ;
}
//printf("\nMatrix column: [%d]\n", rownum);
//get_column( mat, row, col, rownum);
//getch();
//transpose( mat, row, col);
//getch();
free(mat);
}