[英]matrix multiplication can't be compile
I have some trouble when compiling this 编译时我遇到了一些麻烦
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <time.h>
int perkalianMatriks(double ** A, double ** B, double ** C, int n);
int randomMatriks(double * m, int n);
int main(int argc, char *argv[]) {
int n_order = 0 , i, j;
double ** A, ** B, ** C;
time_t time1, time2;
double diff_sec;
randomMatriks(double * m, int n)
A = (double **) malloc(sizeof(double) * n_order);
B = (double **) malloc(sizeof(double) * n_order);
C = (double **) malloc(sizeof(double) * n_order);
for (i = 0; i < n_order; i++) {
A[i] = (double *) calloc(sizeof(double), n_order);
B[i] = (double *) calloc(sizeof(double), n_order);
C[i] = (double *) calloc(sizeof(double), n_order);
}
printf("\nMatrix Sizes : ");
scanf("%d", %n_order);
time(&time1);
perkalianMatriks(A, B, C, n_order);
time(&time2);
diff_sec = difftime (time2,time2);
printf ("Total time to execute %f seconds.\n", diff_sec);
return 0;
}
int randomMatriks(double * m, int n) {
int i;
for (i = 0; i < n_order; i++) {
m[i] = (double) (rand() % 10) + 1;
}
}
int perkalianMatriks(double ** a, double ** b, double ** c, int n) {
int i, j, k;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
c[i][j]=0;
for (k = 0; k < n; k++)
{
c[i][j] += a[i][k] * b[k][j];
}
}
}
return 0;
}
Error when compiling 编译时出错
Does anyone can help me fix this? 有没有人可以帮我解决这个问题?
You're missing a semi-colon. 你错过了一个分号。 And you can't name a variable
double
. 而且你不能将变量命名为
double
。 I guess this line should just be removed, since it is a cut and paste of the declaration above. 我想这条线应该被删除,因为它是上面声明的剪切和粘贴。 If you need to call it, you should call it with real arguments.
如果你需要调用它,你应该用真实的参数调用它。 I don't see that you defined anything suitable for the first parameter, I guess from later on in your code, you intend to use
n_order
for the second parameter. 我没有看到你定义了任何适合第一个参数的东西,我猜你以后会在你的代码中使用
n_order
作为第二个参数。
randomMatriks(double * m, int n)
^^^^^^ ^^^
This scanf
is wrong, you probably want &
: 这个
scanf
错了,你可能想要&
:
scanf("%d", %n_order);
^^^
The variable dif_sec
doesn't exist. 变量
dif_sec
不存在。 Typo. 错字。
dif_sec = difftime (time2,time2);
^^^^^^^
printf ("Total time to execute %f seconds.\n", dif_sec);
^^^^^^^
The variable n_order
doesn't exist. 变量
n_order
不存在。 n
instead probably. n
反而可能。
for (i = 0; i < n_order; i++) {
^^^^^^^
The randomMatriks
should return something. randomMatriks
应该返回一些东西。 It falls off without a return
statement, even though it is declared to return an int
. 它没有
return
语句就会掉线,即使它被声明为返回一个int
。
You have some unused variables and function parameters. 您有一些未使用的变量和函数参数。
int main(int argc, char *argv[]) {
^^^^ ^^^^
int n_order = 0 , i, j;
^^^
The problem is that you never initialized your C
array, which is an array of pointers to pointers to double. 问题是你从来没有初始化你的
C
数组,这是一个指向double的指针数组。 In perkalianMatriks
, it's deferenced, and at that point, all bets are off as to what the pointer is (0xfdfdfdfd in your case). 在
perkalianMatriks
,它是受尊重的,并且在那时,所有的赌注都是关于指针是什么(在你的情况下为0xfdfdfdfd)。
(I'm guessing you might not understand what a **
type really is and how it's supposed to be used?) (我猜你可能不明白什么是
**
类型以及它应该如何使用?)
That is not correct way of allocating memory for * B and so on. 这不是为* B等分配内存的正确方法。 See example on allocating memory for double pointers ( *B) and so for the rest of pointers.
请参阅为双指针( * B) 分配内存的示例,以及其他指针的内存 。 Also take care of semi colons.
还要照顾半冒号。
It is quite simple to perform multiplication of two matrices. 执行两个矩阵的乘法非常简单。
Your can get a simple clue at : solvedc.com 您可以在: solvec.com获得一个简单的线索
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.