Reputation: 11
The error I'm getting is:
p1.c: In function 'WriteToFile': p1.c:219:2: warning: implicit declaration of function 'putw' [-Wimplicit-function-declaration]
Not sure how to fix the problem. I'm also getting a segmentation fault. The code compiles fine, but when running the code I get the segfault. I'm assuming the error I'm getting is linked to the segmentation fault? If someone could provide some insight or propose a solution, that would be greatly appreciated.
edit: The segfault is occurring at "int k2 = atoi(argv[2]);", still don't know what's causing it though.
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#pragma warning(disable : 4996).
typedef struct person_
{
int ID;
int mask;
int vaccine;
int status;
int days_infected;
int days_quarantined;
}Person;
#define POPULATION 500
static int vaccinated;
static int infected;
static int masked;
static int healthy;
static int oldinfected;
Person meetrandomly(FILE* IN, Person M, Person N[])
{
//reading from the pre formatted file!!!!!!!
float p_infected[4];
float np = 0, hwf = 0, iwf = 0, hv = 0;
fscanf(IN, "%*s %*s %*s %*s %*s %*s %*s %*s %f", &np);
fscanf(++IN, "%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %f", &hwf);
fscanf(++IN, "%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %f", &iwf);
fscanf(++IN, "%*s %*s %*s %*s %*s %*s %*s %*s %f", &hv);
p_infected[0] = np;
p_infected[1] = hwf;
p_infected[2] = iwf;
p_infected[3] = hv;
/////////////////////////////////////////////////////////
int n = rand() % 20 + 1;
int i;
for (i = 0;i < n;i++)
{
float* poniter1 = NULL;
int m = rand() % POPULATION;
Person temp = N[m];
if (M.status == 0 && N[m].status == 0)
{
return M;
}
else if (M.status == 0 && N[m].status == 1)
{
if (M.mask == 0 && N[m].mask == 0)
{
int num = rand() % 100 + 1;
int n = p_infected[0]*100;
if (M.vaccine == 1)
{
n = n / 10;
}
if (num < n)
{
M.status = 2;
M.days_infected = 0;
infected++;
}
}
else if (M.mask == 0 && N[m].mask == 1)
{
int num = rand() % 100 + 1;
int n = p_infected[2]*100;
if (M.vaccine == 1)
{
n = n / 10;
}
if (num < n)
{
M.status = 2;
M.days_infected = 0;
infected++;
}
}
else if (M.mask == 1 && N[m].mask == 0)
{
int num = rand() % 100 + 1;
int n = p_infected[1]*100;
if (M.vaccine == 1)
{
n = n / 10;
}
if (num <= n)
{
M.status = 2;
M.days_infected = 0;
infected++;
}
}
else if (M.mask == 1 && N[m].mask == 1)
{
int num = rand() % 100 + 1;
int n = (hwf*iwf)*100;
if (M.vaccine == 1)
{
n = n / 100;
}
if (num > (100-n))
{
M.status = 2;
M.days_infected = 0;
infected++;
}
}
}
}
return M;
}
void Simulation(FILE* A, Person M[])
{
int i;
for (i = 0; i < POPULATION; i++)
{
M[i] = meetrandomly(A, M[i], M);
M++;
}
}
Person* updation(Person B[])
{
oldinfected = infected;
int i;
for (i = 0;i < POPULATION;i++)
{
if (B[i].status == 1 && B[i].days_infected < 14)
{
B[i].days_infected++;
}
else if (B[i].status == 1 && B[i].days_infected > 14)
{
B[i].status = 0;
infected--;
healthy++;
}
else if (B[i].status == 3 && B[i].days_quarantined > 14)
{
B[i].status = 0;
infected--;
healthy++;
}
}
return B;
}
Person* Initialization(FILE* IN, Person N[], int k2, int k3, int k4, int k5, int *vaccinated, int *infected, int *healthy, int* masked)
{
//allocating the array with the necessary values!
/*(1) N* k2 initially infected person randomly; (2) N* k3 random person who
will wear face masks every day; (3) N* k4 random person who have been vaccinated; (4)
for each infected person, whether he / she is quarantined.The N - Person population should
be updated accordingly.*/
k2 = (POPULATION * k2) / 100;
*(infected)= k2;
k3 = (POPULATION * k3) / 100;
k4 = (POPULATION * k4) / 100;
*(vaccinated) = k4;
k5 = (POPULATION * k5) / 100;
*(masked) = k5;
int i;
for (i = 0;i < k2;i++)
{
N[i].days_infected = 0;
N[i].status = 2;
int k = rand() % 100;
if (k > k5)
{
N[i].status = 3;
N[i].days_quarantined = 0;
}
else
{
N[i].status = 2;
N[i].days_infected = 0;
}
}
int j;
for (j = k2;j < (k3)+(k2);j++)
{
N[j].mask = 1;
}
int k;
for (k = k3;k < ((k3)+(k4));k++)
{
N[k].vaccine = 1;
}
return N;
}
void WriteToFile(FILE* OUT, int week)
{
char str[] = "\n Week";
fputs(str, OUT);
putw(week, OUT);
char str1[] = ",\tHealthy=";
fputs(str1, OUT);
putw(healthy, OUT);
char str2[] = ", \tNew Cases=";
fputs(str2, OUT);
putw((infected-oldinfected), OUT);
char str3[] = ", \tTotal Cases=";
fputs(str3, OUT);
putw((infected), OUT);
}
int main(int argc, char* argv[])
{
int days = 0;
Person A[POPULATION];
int k2 = atoi(argv[2]);
int k3 = atoi(argv[3]);
int k4 = atoi(argv[4]);
int k5 = atoi(argv[5]);
if ((k2 < 0 && k2>100) || (k3 < 0 && k3>100) || (k4 < 0 && k4>100) || (k5 < 0 && k5>100))
{
printf(" \n Please enter k2's, k3's, k4's, k5's value to be between 1 and 100 and try again.");
return 0;
}
FILE* IN = fopen(argv[6], "r");
FILE* OUT = fopen(argv[7], "w");
if (IN < 0 || OUT < 0) { perror("\n No such file found!!\n"); exit(1); }
//reading from the pre-formatted file!!!
int i, j, k;
days = 7 * atoi(argv[1]);
Person *B;
B= Initialization(IN, A, k2, k3, k4, k5, &vaccinated, &healthy, &infected, &masked);
for (i = 0; i < days; i++)
{
Simulation(IN, B);
updation(B);
// add other necessary parameters for the function call
// this is the key function that does the simulation
if (i % 7 == 6) // at the end of each week
{
int q = i / 7;
WriteToFile(OUT,q);
}
// write the report to the output file
}
// close both input and output files
return 0;
}
Upvotes: 1
Views: 160