gpt4 book ai didi

c - 矩阵生成代码根本失败,我不知道为什么

转载 作者:行者123 更新时间:2023-11-30 17:43:23 24 4
gpt4 key购买 nike

这是我的代码,但不起作用(抱歉,如果它的格式很糟糕 - 代码块很糟糕):

#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 语言编写的,因此我们尝试将其传输过来。

任何人都可以发现问题所在,并防止矩阵条目被更改吗?提前致谢。

最佳答案

The issue is that the code in the first function, which should be altering the entries of the matrix, don't!

从函数末尾删除分号 ; matrix_gen

void matrix_gen(......, int opt_strat[m][m][m]);
^Remove this semicolon

尽管如此,您的代码仍然存在太多问题。你必须把它们全部修好。

关于c - 矩阵生成代码根本失败,我不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20247093/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com