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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  // program for project.cpp : main project file.

#include "stdafx.h"
#include <iostream>

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<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).
Close, but the code of the for loops have to go into the main or a function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
include <iostream>
#include <cstdlib> // rand and srand
#include <algorithm> // fill_n
#include <ctime> // time
#include <numeric> // 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<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.
//

#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;
}

void infectionSpread(char touch[N]){
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.
1
2
3
4
5
6
7
8
9
10
11
12
void infection(char touch[N]){
   for(int j=0; j<N; j++)
      touch[N] = notsick;   // <-------- The N needs to be j
}
void display(char touch[N]){
   for(int j=0; j<N; j++) {
      if ( j && !(j%50) )
         cout << endl;  
      cout << touch[j];    // <-------- The N needs to be j
   }
   cout << endl;
}

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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.