【发布时间】:2021-01-25 08:58:08
【问题描述】:
所以我的任务是编写一个模拟 covid-19 患病几率的程序。这是大学的任务,而不是工作。
我已尝试将我的代码注释为任何人都可以阅读。
我的主要问题是,例如,当我将 L 的值更改为大于 80 的数字时,我会得到错误代码:-1073741819 (0xC0000005)。
老师期望的数字是 150,所以矩阵是 150*150 = 22500
代码的基本作用: 它找到一个随机的行和列=随机的人,并检查他是否对病毒敏感(数字=0)或者他是否已经生病(数字=1)..
如果他很敏感,我们会生成一个随机数,然后将其与 Gamma 或 Beta 的数量进行比较,这取决于我们是想治愈他还是让他生病。
在我对 stackoverflow 的评论中,我发现我可能必须将其设为动态矩阵,但我不知道该怎么做,因为当我尝试将其更改为以下内容时:
int *nepesseg= (int *)malloc(L*L* sizeof(int));
我只是得到了更多的错误,因为我不知道如何引用矩阵的元素了,等等。
我目前正在使用 Code::Blocks + GNU GCC 编译器。
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#define L 70 // IF THIS NUMBER IS MORE, LIKE 100, I GET "-1073741819 (0xC0000005)" Error code
int main()
{
srand(time(NULL));
int day=365, randomPersonRow=0,randomPersonColumn=0;
double beta=0.7,gamma=0.1,odds=0.0; //Gamma = healing chance, Beta = chance for getting sick
int SensitiveCount,SickCount,HealedCount;
int population[L][L];
SensitiveCount=(L*L)-1;SickCount=1;HealedCount=0;
int i,j,v,u;
for(i=0; i<L; i++) {
for(j=0;j<L;j++) {
population[i][j]=0;
}
}
randomPersonRow=(rand()%L);
randomPersonColumn=(rand()%L);
population[randomPersonRow][randomPersonColumn]=1; //first sick person - generated random
FILE *fp;
fp = fopen("result.dat", "w");
for(v=1; v<=day; v++){ //365 days
for(u=0;u<=L*L;u++){ //1 day
randomPersonRow=(rand()%L); //Find a random person to see if he is sick or not
randomPersonColumn=(rand()%L); //Find a random person to see if he is sick or not
//for(i=0; i<L; i++) {
//for(j=0;j<L;j++) {
if(population[randomPersonRow][randomPersonColumn]==0){ //0 means he is Sensitive, and not sick or healed yet
if(population[randomPersonRow+1][randomPersonColumn]==1){ // If he meets a sick person in his "matrix" - 4 chance(up, down, left, right)
odds=((double) rand() / (RAND_MAX));
if(odds<=beta){
population[randomPersonRow][randomPersonColumn]=1; // He is getting sick, if he has been in contact with a sick person
SensitiveCount=SensitiveCount-1;SickCount=SickCount+1;
}
}
else if(population[randomPersonRow][randomPersonColumn+1]==1){
odds=((double) rand() / (RAND_MAX));
if(odds<=beta){
population[randomPersonRow][randomPersonColumn]=1; // He is getting sick, if he has been in contact with a sick person
SensitiveCount=SensitiveCount-1;SickCount=SickCount+1;
}
}
else if(population[randomPersonRow-1][randomPersonColumn]==1){
odds=((double) rand() / (RAND_MAX));
if(odds<=beta){
population[randomPersonRow][randomPersonColumn]=1; // He is getting sick, if he has been in contact with a sick person
SensitiveCount=SensitiveCount-1;SickCount=SickCount+1;
}
}
else if(population[randomPersonRow][randomPersonColumn-1]==1){
odds=((double) rand() / (RAND_MAX));
if(odds<=beta){
population[randomPersonRow][randomPersonColumn]=1; // He is getting sick, if he has been in contact with a sick person
SensitiveCount=SensitiveCount-1;SickCount=SickCount+1;
}
}
}
if(population[randomPersonRow][randomPersonColumn]==1){ // 1 = If the random person is sick
odds=((double) rand() / (RAND_MAX));
if(odds<=gamma){
population[randomPersonRow][randomPersonColumn]=2; // He is healed now, if the odds are less then the random gamma value.
SickCount=SickCount-1;HealedCount=HealedCount+1;
}
}
//}
//}
}
if(fp!=NULL){
fprintf(fp,"%d; %d; %d; %d; \n",v,SensitiveCount,SickCount,HealedCount); // writing to file
}else{
printf("Error!");
}
}
fclose(fp);
return 0;
}
【问题讨论】:
标签: c windows matrix malloc stack-overflow