这是我的代码不起作用(抱歉,如果格式非常糟糕 - 代码块很糟糕):
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define m 10
void matrix_gen(int s, int i, int j, double matrix[s][s], int opt_strat[m][m][m]);
for (int u=0; u<s; ++u) {
for (int v=0; v<s; ++v) {
if (u==v) {
matrix[u][v]=1;
}
if (u == s/2) {
if (opt_strat[j][i][0]== 1) {
if (v == m-i) {
matrix[u][v]=1;
}
else {
if (v==m-i+(s/2)) {
matrix[u][v]=1;
}
}
}
}
if (u==(s/2)+m-i) {
if (opt_strat[i][j][0] == 1) {
if (v==0) {
matrix[u][v]=1;
}
else {
if (v==(s/2)) {
matrix[u][v]=1;
}
}
}
}
}
}
for (int u=0; u<m-i-1; ++u) {
for (int v=0; v<s; ++v) {
if (opt_strat[i][j][u]==1) {
if (v==u+1) {
matrix[u][v]= -0.5;
}
else {
if (v==u+(s/2)+1) {
matrix[u][v]= -0.5;
}
}
}
}
}
for (int u=m-i; u<(s/2)-1; ++u) {
for (int v=0; v<s; ++v) {
if (opt_strat[j][i][u-m+i]==1) {
if (v==u+1) {
matrix[u][v]= -0.5;
}
else {
if (v==u+1+(s/2)) {
matrix[u][v]= -0.5;
}
}
}
}
}
}
void v_vector_gen(){
}
int main () {
/* p_vector = probability vector, M the matrix and v_vector the constants in system */
int i,j,s,level=18;
int opt_strat[m][m][m]={1};
double probs[m][m][m]={0};
while (level >=0) {
for (i=9; i>=0; --i) {
for (j=9; j>=0; --j) {
if (j>i) {
continue;
}
if (i+j==level) {
printf("\nSublevel %d,%d\n",i,j);
s=2*(2*m-i-j);
double* p = NULL;
double* p_vector = malloc(s * sizeof *p);
double* v_vector = malloc(s * sizeof *p);
double (*matrix)[s] = malloc(sizeof(double[s][s]));
//double** matrix;
// matrix = (double**) malloc(s * sizeof(double*));
// for (int i = 0; i < s; i++)
// matrix[i] = (double*) malloc(s * sizeof(double));
/*Use separate functions to define matrix and vector*/
matrix_gen(s, i, j, matrix, opt_strat);
/*Print the matrix to check*/
for (int i=0; i<s; ++i) {
printf("\n\n");
for (int j=0; j<s; ++j) {
printf(" %lf ", matrix[i][j]);
}
}
}
}
}
--level;
}
while (1==1) {}
return 0;
}
问题是第一个函数中的代码应该是改变矩阵的条目,而不是!第一个if语句有效,我得到一个单位矩阵,但是应该出现的所有半部分和减半部分始终保持为零。我想也许像(s / 2)+ m-i这样的值可能是问题(这不是我的算法!这是一个小组项目......)但所有这些值对矩阵条目都有意义。似乎代码忽略了除第一个if语句之外的所有代码,我不知道为什么。我们知道上面的过程应该是正确的矩阵 - 它已经在python中完成并且结果很好 - 然而大部分编程都在C中,所以我们试图将它传输出来。
有人可以发现出错的地方,并防止矩阵条目被改变吗?提前谢谢。
答案 0 :(得分:0)
问题是第一个函数中的代码应该改变矩阵的条目,不要!
从函数;
matrix_gen
void matrix_gen(......, int opt_strat[m][m][m]);
^Remove this semicolon
尽管如此,您的代码存在太多问题。你必须解决所有问题。