The task of the algorithm is to calculate the spread of a disease in a sane population starting with 1 sick person.
Now the algorithm i made works with 2 for cycles.
First question: would you find advices for the actual code?
Second question: how can i make it with a recursion of the function epidemy?
What I have tried:
Actual code working:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int read_input();
int epidemy(int sick_people, int time_past, int sane_people);
int main(){
int sick_people = 1;
int time_past;
int sane_people;
srand(time(NULL));
time_past = read_input();
sane_people = read_input();
epidemy( sick_people , time_past, sane_people );
return 0;
}
int read_input(){
printf(" Write a positive integer.\n");
int val;
while (scanf("%d", &val) != 1 || val < 0){
scanf("%*[^\n]%*c");
printf("Input incorrect. Write a positive integer.\n");
}
printf("The input was %d\n", val);
return val;
}
int epidemy(int sick_people, int time_past ,int sane_people){
int t = 0;
int i = 0;
float p = 0.001;
sane_people= sane_people- sick_people;
for(t = 0 ;t <time_past; t++){
for(i = 0 ; i <= sane_people ; i++){
float x = rand() / (float) RAND_MAX;
if(x <= p){
sick_people=sick_people+1;
sane_people= sane_people-1;
}
if(sane_people<=0){
printf("Sick people at time %d are %d\n", t , sick_people);
exit(0);
}
}
p = p * sick_people;
if(sane_people<=0){
printf("Sick people at time %d are %d\n", t , sick_people);
exit(0);
}
}
printf("Sick people at time %d are %d\n", t , sick_people);
}