C 将矩阵表示为结构并填充随机值
我定义了一个新的typedef,如下所示:C 将矩阵表示为结构并填充随机值,c,function,data-structures,C,Function,Data Structures,我定义了一个新的typedef,如下所示: typedef struct matrep { int rows, columns; /*rows/columns represent the number of rows/columns in the matrix, data represents matrix entries.*/ double *data; } MATRIX; 现在,我要做的是用随机双值填充这个结构,使用函数gen_矩阵gen_矩阵获
typedef struct matrep
{
int rows, columns; /*rows/columns represent the number of rows/columns in the matrix, data represents matrix entries.*/
double *data;
}
MATRIX;
现在,我要做的是用随机双值填充这个结构,使用函数gen_矩阵gen_矩阵获取指向矩阵结构的指针并返回相同的值。
但是,当我执行下面的程序时,会出现运行时错误
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct matrep
{
int rows, columns; //rows and columns represent the number of columns in the matrix, data represents matrix entries.//
double *data;
}
MATRIX;
double random();
MATRIX *gen_matrix(MATRIX *ptr);
int main()
{
MATRIX *somepointer;
somepointer -> rows = 2; //The program crashes when I try to execute this.//
somepointer -> columns = 2;
}
double random()
{
double f = (double)rand() / RAND_MAX; //Generating a random number between -100 and 100//
return (-100 + f*(200));
}
MATRIX *gen_matrix(MATRIX *ptr)
{
int i, j;
int m, n;
MATRIX *newdata;
m = ptr -> rows;
n = ptr -> columns;
newdata = (MATRIX *)malloc(sizeof(double)*(m*n)); //Allocating suitable space.//
ptr = newdata;
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
*(ptr -> data)= random(); //Setting the value of each and every matrix entry to a random double.//
(ptr -> data)++;
}
}
return ptr;
}
#包括
#包括
#包括
类型定义结构matrep
{
int rows,columns;//行和列表示矩阵中的列数,数据表示矩阵项//
双*数据;
}
矩阵;
双随机();
矩阵*gen_矩阵(矩阵*ptr);
int main()
{
矩阵*somepointer;
somepointer->rows=2;//当我试图执行此操作时,程序崩溃//
somepointer->columns=2;
}
双随机
{
double f=(double)rand()/rand_MAX;//生成一个介于-100和100之间的随机数//
返回(-100+f*(200));
}
矩阵*gen_矩阵(矩阵*ptr)
{
int i,j;
int m,n;
矩阵*新数据;
m=ptr->行;
n=ptr->columns;
newdata=(矩阵*)malloc(sizeof(double)*(m*n));//分配合适的空间//
ptr=新数据;
对于(i=0;idata)=random();//将每个矩阵项的值设置为随机双精度//
(ptr->数据)+;
}
}
返回ptr;
}
我认为有两个问题:
1:由于某种原因,在main()中设置“行”和“列”的值是错误的。
2:我的gen_矩阵函数也可能有问题。
所以我的问题是,我该如何纠正这两个问题?(注意:我的random()函数肯定是可以的)。您有一些错误,其中之一是您以错误的方式分配空间,新数据的类型不是双精度矩阵,请将此更改为:
newdata = malloc(sizeof(*newdata));
newdata->data = malloc(sizeof(double)*(m*n));
不要投malloc;) 您的代码中有几个问题,下面是其中的一些问题:
somepointer
指针变量,这是导致第一次崩溃的原因,请更改main
例程,如下所示:
int main() {
MATRIX *somepointer = (MATRIX*) malloc(sizeof(MATRIX));
somepointer -> rows = 2;
somepointer -> columns = 2;
MATRIX *another_matrix = gen_matrix(somepointer);
// Don't forget to free your stuff
free(somepointer);
free(another_matrix);
}
gen_matrix
函数中的内存分配,以避免将来的崩溃
// Here do another malloc to avoid another access without initialization crash
MATRIX *newdata = (MATRIX*) malloc(sizeof(MATRIX));
m = ptr -> rows;
n = ptr -> columns;
newdata->data = (double*) malloc(sizeof(double) * (m * n));
ptr = newdata;
数据
指针,这是不正确的,因为在循环结束时,指针将指向数据的最后一个元素。您可以使用指针算法访问数组元素,根据数组[col,row]位置计算内存索引:
for(i = 0; i < m; i++)
for(j = 0; j < n; j++) {
// Memory address for array position i, j formula:
// array-base-position + (total-colums * current-row) + current-col
// example: given total-colums = 5, current-row = 1 and current-col = 3
// you'll get: ptr + (5 * 1) + 4 = 8
//
// ptr
// v
// ---------------------
// | 0 | 1 | 2 | 3 | 4 |
// ---------------------
// | 5 | 6 | 7 | 8 | 9 |
// ---------------------
// ... ^ here
*(ptr -> data + (n * i) + j) = random();
}
}
我建议您查看GNU GSL的源代码,它的矩阵库是使用一个结构实现的,您声明了一个指针而没有初始化它,然后尝试去引用它。谢谢。我按照您的建议更改了代码,但是编译器告诉我您的代码有错误?如果我将“newdata”初始化为一个矩阵,它告诉我这是一个问题,因为malloc返回一个void*,但是如果我将“newdata”声明为void*,那么我也会遇到问题;或矩阵*somepointer=malloc(sizeof(*somepointer));谢谢h3nr1x,这非常有用。我认为在“*(ptr->data+(n*I)+j)=drandom();”行中有一个小错误,因为当我运行它时程序崩溃了(没有这一行它运行得很好)。可能是语法问题吗?我运行了程序,但没有崩溃。
ptr->data
指针是否正确初始化?无论如何,我发布了上面建议的代码,请测试它。代码现在运行良好,我错误地定义了我的“newdata->data”。这应该让我走上正轨了,谢谢你的帮助!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct matrep {
int rows, columns;
double *data;
} MATRIX;
double drandom();
MATRIX *gen_matrix(MATRIX *ptr);
int main() {
MATRIX *somepointer = (MATRIX*) malloc(sizeof(MATRIX));
somepointer->rows = 2;
somepointer->columns = 2;
MATRIX *another_matrix = gen_matrix(somepointer);
free(somepointer);
free(another_matrix->data);
free(another_matrix);
}
double drandom() {
double f = (double) rand() / RAND_MAX;
return (-100 + f*(200));
}
MATRIX *gen_matrix(MATRIX *ptr) {
MATRIX *newdata = (MATRIX*) malloc(sizeof(MATRIX));
int nrows = ptr->rows;
int ncols = ptr->columns;
newdata->data = (double*) malloc(sizeof(double) * (nrows * ncols));
for(int row = 0; row < nrows; row++) {
for(int col = 0; col < ncols; col++) {
*(newdata->data + (ncols * row) + col) = drandom();
}
}
return newdata;
}