Hello, I need to write a program that can calculate the rate at which humans get infected with Lyme disease in the span of 10 years. Our variables include number of sick and non sick animals, the probability of transmission from animal to animal, and the human to sick animal contact ratio. Here's the code we have so far:
// program for project.cpp : main project file.
#include "stdafx.h"
#include <iostream>
usingnamespace std;
constunsignedint NOA = 500000; //Number of Animals
constunsignedint NOSA = 1000; //Number of sick animals
constint sick = 1;
constint notsick = 0;
constint humans = 200000000;
float P = .002; // probability of contact b/t sick and non sick animals
// Probablity in which sick animals come in contact with non-sick animals
voidchar A[NOA];
for (int i=0; i<NOA; i++)
A[i]= notsick;
for (int i=0; i<NOA; i++)
if( (rand()/float(RAND_MAX)) < P ) // with probability P
A[i] = sick;
//Probability of transmission b/t sick and non sick animals
float t = 0.2;
for(int y=0; y < 10; y++)
for(int c= 0; c<NOA; c++)
int x = ((rand()/float(Rand_Max))*N)
int y = ((rand()/float(Rand_Max))*N)
if(A[x]!=A[y])
if ((rand()/float(Rand_Max))*t)
A[x]=A[y]=1;
int main()
{
int n = sum(A)
float s = n/float(NOA);
float contact = .002;
int affected = contact*s*humans;
cout << affected << endl;
}
My partner and I have been stumped on this for a while and can't seem to figure out what's going wrong. We're quite unsure about the random number generator for this program and our professor told us we had to have array A[] to randomly pick out a 0 or a 1. So if A[x]!=A[y) then the animal should be sick (or 1).
include <iostream>
#include <cstdlib> // rand and srand
#include <algorithm> // fill_n
#include <ctime> // time
#include <numeric> // accumulate
constunsignedint NOA(500000); //Number of Animals
constunsignedint NOSA(1000); //Number of sick animals
constint sick(1), notsick(0), humans(200000000);
float P(0.002); // probability of contact b/t sick and non sick animals
int main() {
//----------------------------------------------------
// Seed the random number generator to get a different
// sequence of numbers on each run of program
srand(time(0));
//----------------------------------------------------
// You need to allocate this array off of the heap
// because it is too large to be on the stack
char *animals(newchar [NOA]);
//----------------------------------------------------
// Set all of the animals to notsick
for (int a(0); a<NOA;a++){
animals[a] = notsick;
}
//----------------------------------------------------
// Setup the sick animals we are starting with
int i(0),tmp;
while(i!=NOSA){
tmp = rand()%NOA;
if ( animals[tmp] == notsick ){
animals[tmp] = sick;
i++;
}
}
//----------------------------------------------------
// Or the for loop and while loop could be reduced
// to the code below
// http://www.cplusplus.com/reference/algorithm/fill_n/
// std::fill_n(animals,NOA,notsick);
// std::fill_n(animals,NOSA,sick);
// http://www.cplusplus.com/reference/algorithm/random_shuffle/
// std::random_shuffle(animals,animals+NOA);
//----------------------------------------------------
// Now you have all of the infected animals and start
// to have them infect each other
//
// Add the code
//
//----------------------------------------------------
// accumulate will sum up the ones in the array
// http://www.cplusplus.com/reference/numeric/accumulate/
std::cout << "Total: " << NOA << " infected: " << std::accumulate(animals,animals+NOA,0) << std::endl;
delete [] animals;
return(0);
}
Hey thanks so much for the quick response! another question, I figured out how to visually see the animals using the 0s but i can't seem to get them to infect each other over time...
//
// main.cpp
// random
//
// Created by Justin on 11/29/13.
// Copyright (c) 2013 Justin Sanchez. All rights reserved.
//
Yeah I took them out because it seemed kind of pointless to have them there and the program still worked without them. Would I have to use srand() to get the gradual spread of infection per year?
srand is use to seed the random number generator. If you don't seed it or use the same seed value the sequence of random numbers will be the same. So it's not required but you should use it.
To spread the infection, you need create a function that grabs two animals and infects them like your first post. Call it from within the while loop.