### Data Analysis Program help

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:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253`` `````` // program for project.cpp : main project file. #include "stdafx.h" #include using namespace std; const unsigned int NOA = 500000; //Number of Animals const unsigned int NOSA = 1000; //Number of sick animals const int sick = 1; const int notsick = 0; const int 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 void char A[NOA]; for (int i=0; i

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).
Close, but the code of the for loops have to go into the main or a function.
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364`` ``````include #include // rand and srand #include // fill_n #include // time #include // accumulate const unsigned int NOA(500000); //Number of Animals const unsigned int NOSA(1000); //Number of sick animals const int 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(new char [NOA]); //---------------------------------------------------- // Set all of the animals to notsick for (int a(0); a
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.
//

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <algorithm>

using namespace std;

const unsigned int N = 1000;

const char sick = '1';
const char notsick = '0';

// probability of contact

void infection(char touch[N]){
for(int j=0; j<N; j++)
touch[N] = notsick;
}

float P = .02;

void randomContact(char touch [N], float P){
for(int j=0; j<N; j++)
if( (rand()/float(RAND_MAX)) < P )
touch[j] = sick;

}

void display(char touch[N]){
for(int j=0; j<N; j++)
cout << touch[N];
cout << endl;

}

unsigned int count(int j, const char touch[N]){
int cnt=0;
for(int ry = (j-1); ry <=(j+1); ry++) {
if(ry >=0 && ry < N)
if( !(ry==j)) {
if(touch [ry] == sick)
cnt++;
}
}
return cnt;
}

char tmp_touch[N];
for(unsigned j=0; j < N; j++){
tmp_touch[j] = touch[j];

for(unsigned j=0; j< N; j++){
int cnt = count(j, tmp_touch);
//cout << cnt << " ";
if(cnt<2 || cnt>3)
touch[j] = notsick;
else
if(cnt == 3)
touch[j] = sick;
}
cout << endl;
}

}

int main()
{
char u[N];
infection(u);

randomContact(u, .25);

display(u);

int j=0;
while(1) {
cout << "Year: " << j++ << endl;
display(u);
for(int k=0; k<1000000000; k++) {};
}

return 0;
}

This would move you along further.
 ``123456789101112`` ``````void infection(char touch[N]){ for(int j=0; j

I would also make the change to the values of sick and nonsick because '1' and '0' are hard to see but 'X' and '.' are not.
 ``123456789101112131415161718192021`` ``````const char sick('X'); const char notsick('.'); `````` ```\$ ./a.out XX...XX......XX..X.X......X......X......X....X.... ..X..X...X.......X.X.......X...XX.......X......... ...XX..X........XX.XX.X.X..XX...X....X.....X..X... ..XX..X...X..XX..X..X.XXX..X.......X.............. ........X..........X...X....X....X..X........X.X.. XX......X...XX..XXX...........XXX.X.X..X.X..XXX... ....X.X..X......XX......X............X............ .....X.....X......X....XX.........XX.XX....X..X..X ..........XX..XXXX..X....X......X..X.X......XX.X.. .X..........X.....XXX.XX..X...X.XX...........XXX.. ..XX.X.XX.X....X...X...X......XX....XX.X.X........ .........XX...XX....X...X.X..X...X.X..X.X......... XXX.X..XXX......XX.....X.X....X........X........X. .XX.XX.X......XX..XX.X..XX...X...........X......X. ......X..XX..XXX.....................X.......XX... X......X...X.X......X..XX.....XXXX.......X.....X.X ...............X...X...XX.X.........X...XXX..X.... .....X.......XXX..X..XXX.........X........X....... ...X....X.X.......X.X..X..X........X..XXX.....X... .....X....XX.....X.X...................X.........X```

Lastly, you need have an infection inside of the while loop and if you want to pause for a second use the sleep function.

What are the other functions (infectionSpread, count) for and do you plan to use them?

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?
Last edited on
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.
Topic archived. No new replies allowed.