Hi guys,

I have an input file as below (it is a family tree):

1 0 0
2 0 0
3 0 0
4 1 2
5 1 2
6 0 0
7 3 4
8 5 6
9 7 8
10 7 8

where column 1 = individual number
column 2 = mother number
column 3 = father number

so for example, individual 7's mom is individual 3, and his father is individual 4.

A further 2 columns are added. The program places a 0 or 1 in there depending on probabilities I have set out myself. An example of the output is as follows:

1 0 0 0 1
2 0 0 1 1
3 0 0 1 0
4 1 2 0 1
5 1 2 0 1
6 0 0 0 0
7 3 4 0 0
8 5 6 1 0
9 7 8 0 0
10 7 8 0 0

What I want to do is set a counter on how many 00's there are, how many 01's there are, and how many 11's there are. So for the above example, it would say:

00 = 4
01 = 4
11 = 2

My code is as below.

#include <fstream.h>
#include <stdlib.h>


int i, j;
int matrix[10000][5] = {0};                              //Define a matrix with a maximum of 100 rows and 6 columns
int counters[2][2] = {0};                                // Define the genotype counter     
int repeats = 0;
int norepeats;
float p, q;
float rand1, rand2, rand3, rand4;
void SetSeed();

int main () {

FILE *output_file = fopen("output.txt", "w");

printf("Enter the frequency of allele A: ");
scanf("%f", &p);
printf("\n");
printf("Frequency of allele A (p) is: %.3f\n", p);       // States the frequency of the A allele
q = 1 - p;                                               // The value of q is 1 - (the probability entered for p)

printf("Frequency of allele B (q) is: %.3f\n", q);       // States the frequency of the B allele
printf("\n");
printf("Please enter the desired number of repeats: ");
scanf("%d", &norepeats);                                 // Desired number of repeats of the program stored in 'norepeats'
printf("\n");

while (repeats != norepeats){                            // A while loop to loop the program 'norepeats' amount of times

const float RANGE = 1.0;                                 // Random number generated is below 1
SetSeed();                                               // Seed file for RNG set here

//Open the input file//
ifstream inFile;
inFile.open("input.txt");                                // Genealogy input file opened

  if (!inFile)
  {
  printf ("Unable to open input file");                  // Error message reported if input.txt cannot be opened
  exit(1);
  }

//Read the input file into the array
while (inFile >> matrix[i][0])
{
printf ("%d\t", matrix[i][0]);
for (j = 1; j < 3; j++)
{
inFile >> matrix[i][j];
printf ("%d\t", matrix[i][j]);
}

int counter;

if (matrix[i][1] == 0 && matrix[i][2] == 0){ // If the individual is a founder:
  
   rand1 = (float) rand() * RANGE / RAND_MAX;            // Random number 1 generated 
   rand2 = (float) rand() * RANGE / RAND_MAX;            // Random number 2 generated
   
   if (rand1 < p){                                       // Place a 0 allele down in column 4 if random number 1 is below the set p value
   matrix[i][3] = 0;
   }   
   else{
   matrix[i][3] = 1;                                     // Place a 1 allele down in column 4 if random number 1 is above the set p value
   }      
   printf("\t%d", matrix[i][3]);
   

   if (rand2 < p){                                       // Place a 0 allele down in column 5 if random number 2 is below the set p value
   matrix[i][4] = 0;
   }      
   else{                                                 // Place a 1 allele down in column 5 if random number 2 is above the set p value
   matrix[i][4] = 1;
   }
   printf("\t%d", matrix[i][4]);
   }
   
else{
   int dad = matrix[i][1];                               // 1 is "dad" column
   int mum = matrix[i][2];                               // 2 is "mum" column

   int dadRowIndex = dad - 1;                            // The row of the father is 'dad - 1' as the first row is set at zero
   int mumRowIndex = mum -1;                             // The row of the mother is 'mum - 1' as the first row is set at zero

   for (counter = 1;counter <= 1;counter++){
   rand3 = (float) rand() * RANGE / RAND_MAX;            // Random number 3 generated
   rand4 = (float) rand() * RANGE / RAND_MAX;            // Random number 4 generated
   }
   if (rand3 < 0.5){                                     // If random number 3 is less than a half, receive allele stored in column 4 from dad
   matrix[i][3] = matrix[dadRowIndex][3];
   }   
   else{                                                 // If random number 3 is more than a half, receive allele stored in column 5 from dad
   matrix[i][3] = matrix[dadRowIndex][4];
   }
   printf("\t%d", matrix[i][3]);
      
   if (rand4 < 0.5){                                     // If random number 4 is less than a half, receive allele stored in column 4 from mum
   matrix[i][4] = matrix[mumRowIndex][3];
   }
   else{                                                 // If random number 4 is more than a half, receive allele stored in column 5 from mum
   matrix[i][4] = matrix[mumRowIndex][4];
   }
   printf("\t%d", matrix[i][4]);
}

i++;
printf ("\n");
}

inFile.close ();                                         // Close the infile
printf("\n");
repeats++;                                               // End loop here
}

fclose(output_file);

}
 
void SetSeed() 
{
  
                                                          // Determining the seed file
     FILE *seed_file;
     long int seed_value;

     seed_file = fopen("seed.txt","r");                   // Open seed file
     fscanf(seed_file,"%d",&seed_value);                  // Exctract seed from file
     fclose(seed_file);                                   // Close seed file
     srand(seed_value);                                   // Randomise seed value
     seed_file = fopen("seed.txt","w");                   // Create temporary output
     fprintf(seed_file,"%d",rand());                      // Print random number to output
     fclose(seed_file);                                   // Close temporary seed output file
}

Any ideas guys?

ok guys... i attempted a counter of my own accord. Code as follows:

#include <fstream.h>
#include <stdlib.h>


int i, j;
int matrix[10000][5] = {0};                              //Define a matrix with a maximum of 100 rows and 6 columns
int counters[2][2] = {0};                                // Define the genotype counter     
int repeats = 0;
int norepeats;
float p, q;
float rand1, rand2, rand3, rand4;
void SetSeed();

int main () {

FILE *output_file = fopen("output.txt", "w");

printf("Enter the frequency of allele A: ");
scanf("%f", &p);
printf("\n");
printf("Frequency of allele A (p) is: %.3f\n", p);       // States the frequency of the A allele
q = 1 - p;                                               // The value of q is 1 - (the probability entered for p)

printf("Frequency of allele B (q) is: %.3f\n", q);       // States the frequency of the B allele
printf("\n");
printf("Please enter the desired number of repeats: ");
scanf("%d", &norepeats);                                 // Desired number of repeats of the program stored in 'norepeats'
printf("\n");

while (repeats != norepeats){                            // A while loop to loop the program 'norepeats' amount of times

const float RANGE = 1.0;                                 // Random number generated is below 1
SetSeed();                                               // Seed file for RNG set here

//Open the input file//
ifstream inFile;
inFile.open("input.txt");                                // Genealogy input file opened

  if (!inFile)
  {
  printf ("Unable to open input file");                  // Error message reported if input.txt cannot be opened
  exit(1);
  }

//Read the input file into the array
while (inFile >> matrix[i][0])
{
printf ("%d\t", matrix[i][0]);
for (j = 1; j < 3; j++)
{
inFile >> matrix[i][j];
printf ("%d\t", matrix[i][j]);
}

int counter;

if (matrix[i][1] == 0 && matrix[i][2] == 0){ // If the individual is a founder:
  
   rand1 = (float) rand() * RANGE / RAND_MAX;            // Random number 1 generated 
   rand2 = (float) rand() * RANGE / RAND_MAX;            // Random number 2 generated
   
   if (rand1 < p){                                       // Place a 0 allele down in column 4 if random number 1 is below the set p value
   matrix[i][3] = 0;
   }   
   else{
   matrix[i][3] = 1;                                     // Place a 1 allele down in column 4 if random number 1 is above the set p value
   }      
   printf("\t%d", matrix[i][3]);
   

   if (rand2 < p){                                       // Place a 0 allele down in column 5 if random number 2 is below the set p value
   matrix[i][4] = 0;
   }      
   else{                                                 // Place a 1 allele down in column 5 if random number 2 is above the set p value
   matrix[i][4] = 1;
   }
   printf("\t%d", matrix[i][4]);
   }
   
else{
   int dad = matrix[i][1];                               // 1 is "dad" column
   int mum = matrix[i][2];                               // 2 is "mum" column

   int dadRowIndex = dad - 1;                            // The row of the father is 'dad - 1' as the first row is set at zero
   int mumRowIndex = mum -1;                             // The row of the mother is 'mum - 1' as the first row is set at zero

   for (counter = 1;counter <= 1;counter++){
   rand3 = (float) rand() * RANGE / RAND_MAX;            // Random number 3 generated
   rand4 = (float) rand() * RANGE / RAND_MAX;            // Random number 4 generated
   }
   if (rand3 < 0.5){                                     // If random number 3 is less than a half, receive allele stored in column 4 from dad
   matrix[i][3] = matrix[dadRowIndex][3];
   }   
   else{                                                 // If random number 3 is more than a half, receive allele stored in column 5 from dad
   matrix[i][3] = matrix[dadRowIndex][4];
   }
   printf("\t%d", matrix[i][3]);
      
   if (rand4 < 0.5){                                     // If random number 4 is less than a half, receive allele stored in column 4 from mum
   matrix[i][4] = matrix[mumRowIndex][3];
   }
   else{                                                 // If random number 4 is more than a half, receive allele stored in column 5 from mum
   matrix[i][4] = matrix[mumRowIndex][4];
   }
   printf("\t%d", matrix[i][4]);
}

i++;
printf ("\n");
}

inFile.close ();                                         // Close the infile

for(i = 0; i < 10; i++)                                  // Set the counter for the genotypes
{
if (matrix[i][3]==0  && matrix[i][4] ==0)                // If alleles AA, increase counter [0][0] by 1
{ 
counters[0][0]++; 
}

else
if (matrix[i][3]==0  && matrix[i][4] ==1 || matrix[i][3]==1  && matrix[i][4] ==0) // If allele is heterozygous, increase counter [0][1] by 1
{ 
counters[0][1]++; 
}

else
if (matrix[i][3]==1 && matrix[i][4] ==1)                 // If alleles BB, increase counter [1][1] by 1
{
counters[1][1]++;
}
}

printf("\n");
printf("Cumulative genotype totals:\n");        
printf("AA: %d\n", counters[0][0]);                      // Print total number of AA individuals
printf("AB: %d\n", counters[0][1]);                      // Print total number of heterozygotes
printf("BB: %d\n", counters[1][1]);                      // Print total number of BB individuals
printf("\n");
printf("\n");
repeats++;                                               // End loop here
}

fclose(output_file);

}
 
void SetSeed() 
{
  
                                                          // Determining the seed file
     FILE *seed_file;
     long int seed_value;

     seed_file = fopen("seed.txt","r");                   // Open seed file
     fscanf(seed_file,"%d",&seed_value);                  // Exctract seed from file
     fclose(seed_file);                                   // Close seed file
     srand(seed_value);                                   // Randomise seed value
     seed_file = fopen("seed.txt","w");                   // Create temporary output
     fprintf(seed_file,"%d",rand());                      // Print random number to output
     fclose(seed_file);                                   // Close temporary seed output file
}

But the problem is, sometimes the counter works properly, and sometimes it doesn't.

I think it might have something to do with the number of repeats i do.

Does anyone see what I'm doing wrong?

You are missing counting the 1-0 combination.

Your main matrix defiition does not agree with your comment about it:

int matrix[10000][5] = {0};  //Define a matrix with a maximum of 100 rows and 6 columns

is really 10,000 rows by 5 columns.

This is one of the problems with excess comments - they get out of sync as you modify a program. Comments should not state what is obvious from the code. Good variable names make many comments unnecessary.

Ok, well forgetting that comment (I have to excess comment to submit it to my markers, they're really strict on it)...

I'm not missing the 1-0 count. It is there if you look again.

The counting is weird. It seems to double itself sometimes, when really the numbers havn't doubled at all. Other times it gives obsurd answers that are huge etc.

Something is majorly wrong?

OK, I didn't see the 10 01 combined test.

Can you give a sample of the data and the resulting counts? Assuming the data is read and stored correctly, the counting block looks good.

First thing I'd try is to make intent of this statement perferctly clear by adding additional ():

if (matrix[3]==0 && matrix[4] ==1 || matrix[3]==1 && matrix[4] ==0)

like this:

if ((matrix[i][3]==0  && matrix[i][4] ==1) || 
    (matrix[i][3]==1  && matrix[i][4] ==0))

If that didn't work I'd try to localize the problem by starting to comment out sections of the code until I came up with the section that seemed to cause the problem OR I'd print out baseline values of counters after initialization to be sure they were intialized correctly and then add output statements whereever the value is changed to be sure the values are changed appropriately.

I don't see where the counters array is being used. Can you point to the line number?

#include <fstream.h>
#include <stdlib.h>


int i, j;
int matrix[10000][5] = {0};                              //Define a matrix with a maximum of 100 rows and 6 columns
int counters[2][2] = {0};                                // Define the genotype counter     
int repeats = 0;
int norepeats;
float p, q;
float rand1, rand2, rand3, rand4;
void SetSeed();

int main () {

	FILE *output_file = fopen("output.txt", "w");

	printf("Enter the frequency of allele A: ");
	scanf("%f", &p);
	printf("\n");
	printf("Frequency of allele A (p) is: %.3f\n", p);       // States the frequency of the A allele
	q = 1 - p;                                               // The value of q is 1 - (the probability entered for p)

	printf("Frequency of allele B (q) is: %.3f\n", q);       // States the frequency of the B allele
	printf("\n");
	printf("Please enter the desired number of repeats: ");
	scanf("%d", &norepeats);                                 // Desired number of repeats of the program stored in 'norepeats'
	printf("\n");

	while (repeats != norepeats){                            // A while loop to loop the program 'norepeats' amount of times

		const float RANGE = 1.0;                                 // Random number generated is below 1
		SetSeed();                                               // Seed file for RNG set here

		//Open the input file//
		ifstream inFile;
		inFile.open("input.txt");                                // Genealogy input file opened

		if (!inFile)
		{
			printf ("Unable to open input file");                  // Error message reported if input.txt cannot be opened
			exit(1);
		}

		//Read the input file into the array
		while (inFile >> matrix[i][0])
		{
			printf ("%d\t", matrix[i][0]);
			for (j = 1; j < 3; j++)
			{
				inFile >> matrix[i][j];
				printf ("%d\t", matrix[i][j]);
			}

			int counter;

			if (matrix[i][1] == 0 && matrix[i][2] == 0){ // If the individual is a founder:

				rand1 = (float) rand() * RANGE / RAND_MAX;            // Random number 1 generated 
				rand2 = (float) rand() * RANGE / RAND_MAX;            // Random number 2 generated

				if (rand1 < p){                                       // Place a 0 allele down in column 4 if random number 1 is below the set p value
					matrix[i][3] = 0;
				}   
				else{
					matrix[i][3] = 1;                                     // Place a 1 allele down in column 4 if random number 1 is above the set p value
				}      
				printf("\t%d", matrix[i][3]);


				if (rand2 < p){                                       // Place a 0 allele down in column 5 if random number 2 is below the set p value
					matrix[i][4] = 0;
				}      
				else{                                                 // Place a 1 allele down in column 5 if random number 2 is above the set p value
					matrix[i][4] = 1;
				}
				printf("\t%d", matrix[i][4]);
			}

			else{
				int dad = matrix[i][1];                               // 1 is "dad" column
				int mum = matrix[i][2];                               // 2 is "mum" column

				int dadRowIndex = dad - 1;                            // The row of the father is 'dad - 1' as the first row is set at zero
				int mumRowIndex = mum -1;                             // The row of the mother is 'mum - 1' as the first row is set at zero

				for (counter = 1;counter <= 1;counter++){
					rand3 = (float) rand() * RANGE / RAND_MAX;            // Random number 3 generated
					rand4 = (float) rand() * RANGE / RAND_MAX;            // Random number 4 generated
				}
				if (rand3 < 0.5){                                     // If random number 3 is less than a half, receive allele stored in column 4 from dad
					matrix[i][3] = matrix[dadRowIndex][3];
				}   
				else{                                                 // If random number 3 is more than a half, receive allele stored in column 5 from dad
					matrix[i][3] = matrix[dadRowIndex][4];
				}
				printf("\t%d", matrix[i][3]);

				if (rand4 < 0.5){                                     // If random number 4 is less than a half, receive allele stored in column 4 from mum
					matrix[i][4] = matrix[mumRowIndex][3];
				}
				else{                                                 // If random number 4 is more than a half, receive allele stored in column 5 from mum
					matrix[i][4] = matrix[mumRowIndex][4];
				}
				printf("\t%d", matrix[i][4]);
			}

			i++;
			printf ("\n");
		}

		inFile.close ();                                         // Close the infile
		printf("\n");
		repeats++;                                               // End loop here
	}

	fclose(output_file);

}

void SetSeed() 
{

	// Determining the seed file
	FILE *seed_file;
	long int seed_value;

	seed_file = fopen("seed.txt","r");                   // Open seed file
	fscanf(seed_file,"%d",&seed_value);                  // Exctract seed from file
	fclose(seed_file);                                   // Close seed file
	srand(seed_value);                                   // Randomise seed value
	seed_file = fopen("seed.txt","w");                   // Create temporary output
	fprintf(seed_file,"%d",rand());                      // Print random number to output
	fclose(seed_file);                                   // Close temporary seed output file
}

Hi. I tried that but it still didn't work. An example of the output:

Desired number of repeats = 3

1   0   0  0  1
2   0   0  1  1
3   0   0  0  1
4   1   2  1  1
5   1   2  1  1
6   0   0  1  0
7   3   4  0  1
8   5   6  1  1
9   7   8  1  1
10  7   8  0  1

Cumulative totals:
00 = 0
10 = 5
11 = 5


1   0   0      0  0
2   0   0      1  0
3   0   0      0  1
4   1   2      1  1
5   1   2      1  1
6   0   0      0  1
7   3   4      1  1
8   5   6      1  1
9   7   8      1  1
10  7   8     1  1

Cumulative totals:
00 = 0
10 = 10
11 = 10


1   0   0      0  0
2   0   0      0  1
3   0   0      0  1
4   1   2      1  1
5   1   2      0  1
6   0   0      1  1
7   3   4      0  1
8   5   6      1  0
9   7   8      0  1
10  7   8     1  1

Cumulative totals:
00 = 0
10 = 15
11 = 15

Any ideas?

int sum =  matrix[i][3] + matrix[i][4] ;
  if( sum == 2 ) ++counters[1][1] ;
  else if( sum == 1 ) ++counters[0][1] ;
  else ++counters[0][0] ;

Hate to do this vernon but...

counters array???

Oh, and I tried the above suggestion, but that didn't work either. Still weird numbers.

> ... but that didn't work either. Still weird numbers.

assert( ( matrix[i][3] == 0  ) || ( matrix[i][3] == 1 ) ) ; 
  assert( ( matrix[i][4] == 0 ) || ( matrix[i][4] == 1 ) ) ; 
  int sum =  matrix[i][3] + matrix[i][4] ;
  if( sum == 2 ) ++counters[1][1] ;
  else if( sum == 1 ) ++counters[0][1] ;
  else ++counters[0][0] ;

Hate to do this vernon but...

counters array???

I think I cut and pasted your first post before I saw your second. You had:

int counters[2][2] = {0};

declared at the top, but I never saw it used later in the code. Looks like post 2 has it.

The code you posted previously for doing the counting:

for(i = 0; i < 10; i++)          // Set the counter for the genotypes
      {
         if (matrix[i][3]==0  && matrix[i][4] ==0)   // If alleles AA, increase counter [0][0] by 1
         { 
            counters[0][0]++; 
         }

         else
            if (matrix[i][3]==0  && matrix[i][4] ==1 || matrix[i][3]==1  && matrix[i][4] ==0) // If allele is heterozygous, increase counter [0][1] by 1
            { 
               counters[0][1]++; 
            }

            else
               if (matrix[i][3]==1 && matrix[i][4] ==1)    // If alleles BB, increase counter [1][1] by 1
               {
                  counters[1][1]++;
               }
      }

Seems OK. I don't see why the 0-0 combination is not being counted, and the cumulative counts don't correctly increment, seems they just keep adding the original value, in this example.

Have you changed this part?

That asseret didnt work either... just doubles 10 and 11, leaving 00 as zero.

Yeah the counters are there... they're just not working :(

yeah i tried changing that part to different combinations of code that mean the same thing... no change though :(

Please show your current version. And a set of results that it generates.

#include <fstream.h>
#include <stdlib.h>
#include <assert.h>


int i, j;
int matrix[10000][5] = {0};                              //Define a matrix with a maximum of 100 rows and 6 columns
int counters[2][2] = {0};                                // Define the genotype counter     
int repeats = 0;
int norepeats;
float p, q;
float rand1, rand2, rand3, rand4;
void SetSeed();

int main () {

FILE *output_file = fopen("output.txt", "w");

printf("Enter the frequency of allele A: ");
scanf("%f", &p);
printf("\n");
printf("Frequency of allele A (p) is: %.3f\n", p);       // States the frequency of the A allele
q = 1 - p;                                               // The value of q is 1 - (the probability entered for p)

printf("Frequency of allele B (q) is: %.3f\n", q);       // States the frequency of the B allele
printf("\n");
printf("Please enter the desired number of repeats: ");
scanf("%d", &norepeats);                                 // Desired number of repeats of the program stored in 'norepeats'
printf("\n");

while (repeats != norepeats){                            // A while loop to loop the program 'norepeats' amount of times

const float RANGE = 1.0;                                 // Random number generated is below 1
SetSeed();                                               // Seed file for RNG set here

//Open the input file//
ifstream inFile;
inFile.open("input.txt");                                // Genealogy input file opened

  if (!inFile)
  {
  printf ("Unable to open input file");                  // Error message reported if input.txt cannot be opened
  exit(1);
  }

//Read the input file into the array
while (inFile >> matrix[i][0])
{
printf ("%d\t", matrix[i][0]);
for (j = 1; j < 3; j++)
{
inFile >> matrix[i][j];
printf ("%d\t", matrix[i][j]);
}

int counter;

if (matrix[i][1] == 0 && matrix[i][2] == 0){             // If the individual is a founder:
  
   rand1 = (float) rand() * RANGE / RAND_MAX;            // Random number 1 generated 
   rand2 = (float) rand() * RANGE / RAND_MAX;            // Random number 2 generated
   
   if (rand1 < p){                                       // Place a 0 allele down in column 4 if random number 1 is below the set p value
   matrix[i][3] = 0;
   }   
   else{
   matrix[i][3] = 1;                                     // Place a 1 allele down in column 4 if random number 1 is above the set p value
   }      
   printf("\t%d", matrix[i][3]);
   

   if (rand2 < p){                                       // Place a 0 allele down in column 5 if random number 2 is below the set p value
   matrix[i][4] = 0;
   }      
   else{                                                 // Place a 1 allele down in column 5 if random number 2 is above the set p value
   matrix[i][4] = 1;
   }
   printf("\t%d", matrix[i][4]);
   }
   
else{
   int dad = matrix[i][1];                               // 1 is "dad" column
   int mum = matrix[i][2];                               // 2 is "mum" column

   int dadRowIndex = dad - 1;                            // The row of the father is 'dad - 1' as the first row is set at zero
   int mumRowIndex = mum -1;                             // The row of the mother is 'mum - 1' as the first row is set at zero

   for (counter = 1;counter <= 1;counter++){
   rand3 = (float) rand() * RANGE / RAND_MAX;            // Random number 3 generated
   rand4 = (float) rand() * RANGE / RAND_MAX;            // Random number 4 generated
   }
   if (rand3 < 0.5){                                     // If random number 3 is less than a half, receive allele stored in column 4 from dad
   matrix[i][3] = matrix[dadRowIndex][3];
   }   
   else{                                                 // If random number 3 is more than a half, receive allele stored in column 5 from dad
   matrix[i][3] = matrix[dadRowIndex][4];
   }
   printf("\t%d", matrix[i][3]);
      
   if (rand4 < 0.5){                                     // If random number 4 is less than a half, receive allele stored in column 4 from mum
   matrix[i][4] = matrix[mumRowIndex][3];
   }
   else{                                                 // If random number 4 is more than a half, receive allele stored in column 5 from mum
   matrix[i][4] = matrix[mumRowIndex][4];
   }
   printf("\t%d", matrix[i][4]);
}

i++;
printf ("\n");
}

inFile.close ();                                         // Close the infile

for(i = 0; i < 10; i++)                                  // Set the counter for the genotypes
{
  assert( ( matrix[i][3] == 0  ) || ( matrix[i][3] == 1 ) ) ; 
  assert( ( matrix[i][4] == 0 ) || ( matrix[i][4] == 1 ) ) ; 
  int sum =  matrix[i][3] + matrix[i][4] ;
  if( sum == 2 ) ++counters[1][1] ;
  else if( sum == 1 ) ++counters[0][1] ;
  else ++counters[0][0] ; 
}

printf("\n");
printf("Cumulative genotype totals:\n");        
printf("AA: %d\n", counters[0][0]);                      // Print total number of AA individuals
printf("AB: %d\n", counters[0][1]);                      // Print total number of heterozygotes
printf("BB: %d\n", counters[1][1]);                      // Print total number of BB individuals
printf("\n");
printf("\n");
repeats++;                                               // End loop here
}

fclose(output_file);

}
 
void SetSeed() 
{
  
                                                          // Determining the seed file
     FILE *seed_file;
     long int seed_value;

     seed_file = fopen("seed.txt","r");                   // Open seed file
     fscanf(seed_file,"%d",&seed_value);                  // Exctract seed from file
     fclose(seed_file);                                   // Close seed file
     srand(seed_value);                                   // Randomise seed value
     seed_file = fopen("seed.txt","w");                   // Create temporary output
     fprintf(seed_file,"%d",rand());                      // Print random number to output
     fclose(seed_file);                                   // Close temporary seed output file
}

Result:

Desired number of repeats = 3

1 0 0 0 1
2 0 0 1 1
3 0 0 0 1
4 1 2 1 1
5 1 2 1 1
6 0 0 1 0
7 3 4 0 1
8 5 6 1 1
9 7 8 1 1
10 7 8 0 1

Cumulative totals:
00 = 0
10 = 5
11 = 5


1 0 0 0 0
2 0 0 1 0
3 0 0 0 1
4 1 2 1 1
5 1 2 1 1
6 0 0 0 1
7 3 4 1 1
8 5 6 1 1
9 7 8 1 1
10 7 8 1 1

Cumulative totals:
00 = 0
10 = 10
11 = 10


1 0 0 0 0
2 0 0 0 1
3 0 0 0 1
4 1 2 1 1
5 1 2 0 1
6 0 0 1 1
7 3 4 0 1
8 5 6 1 0
9 7 8 0 1
10 7 8 1 1

Cumulative totals:
00 = 0
10 = 15
11 = 15

This bit:

else{
            int dad = matrix[i][1];                               // 1 is "dad" column
            int mum = matrix[i][2];                               // 2 is "mum" column

            int dadRowIndex = dad - 1;                            // The row of the father is 'dad - 1' as the first row is set at zero
            int mumRowIndex = mum -1;                             // The row of the mother is 'mum - 1' as the first row is set at zero

            for (counter = 1;counter <= 1;counter++){
               rand3 = (float) rand() * RANGE / RAND_MAX;            // Random number 3 generated
               rand4 = (float) rand() * RANGE / RAND_MAX;            // Random number 4 generated
            }
            if (rand3 < 0.5){                                     // If random number 3 is less than a half, receive allele stored in column 4 from dad
               matrix[i][3] = matrix[dadRowIndex][3];
            }   
            else{                                                 // If random number 3 is more than a half, receive allele stored in column 5 from dad
               matrix[i][3] = matrix[dadRowIndex][4];
            }
            printf("\t%d", matrix[i][3]);

            if (rand4 < 0.5){                                     // If random number 4 is less than a half, receive allele stored in column 4 from mum
               matrix[i][4] = matrix[mumRowIndex][3];
            }
            else{                                                 // If random number 4 is more than a half, receive allele stored in column 5 from mum
               matrix[i][4] = matrix[mumRowIndex][4];
            }
            printf("\t%d", matrix[i][4]);
         }

I'm not sure what it's supposed to do, but I suspect it's going back and changing some of the allele values in previous rows. So, what you display upon assignment is not always the value still there when you get to the counting portion. If you put a loop to display the matrix just before the counting loop, you'll see this occurs.

for( int r = 0; r < 10; r++ )
      {
         for( int c = 0; c < 5; c++ )
            cout << matrix[r][c] << "  " ;
         cout << endl;
      }


      for(i = 0; i < 10; i++)                                  // Set the counter for the genotypes
      {
         assert( ( matrix[i][3] == 0  ) || ( matrix[i][3] == 1 ) ) ; 
         assert( ( matrix[i][4] == 0 ) || ( matrix[i][4] == 1 ) ) ; 
         int sum =  matrix[i][3] + matrix[i][4] ;
         if( sum == 2 ) ++counters[1][1] ;
         else if( sum == 1 ) ++counters[0][1] ;
         else ++counters[0][0] ; 
      }


Result:

Desired number of repeats = 3

1 0 0 0 1
2 0 0 1 1
3 0 0 0 1
4 1 2 1 1
5 1 2 1 1
6 0 0 1 0
7 3 4 0 1
8 5 6 1 1
9 7 8 1 1
10 7 8 0 1

Cumulative totals:
00 = 0
10 = 5
11 = 5


1 0 0 0 0
2 0 0 1 0
3 0 0 0 1
4 1 2 1 1
5 1 2 1 1
6 0 0 0 1
7 3 4 1 1
8 5 6 1 1
9 7 8 1 1
10 7 8 1 1

Cumulative totals:
00 = 0
10 = 10
11 = 10


1 0 0 0 0
2 0 0 0 1
3 0 0 0 1
4 1 2 1 1
5 1 2 0 1
6 0 0 1 1
7 3 4 0 1
8 5 6 1 0
9 7 8 0 1
10 7 8 1 1

Cumulative totals:
00 = 0
10 = 15
11 = 15

These are the actual results or the desired results?
The results of the second group (non-cumulative) should be this, correct?

1 0 0 0 0
2 0 0 1 0
3 0 0 0 1
4 1 2 1 1
5 1 2 1 1
6 0 0 0 1
7 3 4 1 1
8 5 6 1 1
9 7 8 1 1
10 7 8 1 1

Non-Cumulative totals:
00 = 1 (row 1)
10 = 3 (rows 2, 3, 6)
11 = 6 (rows 4, 5, 7, 8, 9, 10)

Is that right? So 01 and 10 are the same result, correct? Or are they considered different, in which case you should have four totals rows, not three?

The first 3 columns are irrelevant to these totals, right? We are only looking at columns 4 and 5.

Note: I picked the second group since the first had no 00 rows. Also, do you want to display the current round's tallies in addition to the cumulative or just the cumulative tallies, as you have?

Thanks for everyones help. I went through it last night and started from scratch. A few points:

I had designated the counter in a for loop, which I didn't want to do. It was there by error. So there were more than one counter which there shouldn't have been. Also, to do the final counting, I did not use an array for the counter. I did it in a slightly different way. My new code is as follows: (and it works)

#include <stdlib.h>
#include <stdio.h>


int i, j, n;
int matrix[100][5] = {0};                              //Define a matrix with a maximum of 100 rows and 6 columns
int AA=0, AB=0,BB=0;                                  // Define the genotype counter     
int repeats = 0;
int norepeats;
float p, q;
float rand1;
float pp, freq;
void SetSeed();

int main () {

//FILE *output_file = fopen("output.txt", "w");

	printf("Enter the frequency of allele A: ");
	scanf("%f", &p);
	printf("\n");
	pp=p*10000;
	freq=0.5*10000;
	printf("Frequency of allele A (p) is: %.3f\n", p);       // States the frequency of the A allele
	q = 1 - p;                                               // The value of q is 1 - (the probability entered for p)

	printf("Frequency of allele B (q) is: %.3f\n", q);       // States the frequency of the B allele
	printf("\n");
	printf("How many individuals in the genealogy?");
	scanf("%d", &n);
	
	printf("\n");
	printf("Please enter the desired number of repeats: ");
	scanf("%d", &norepeats);                                 // Desired number of repeats of the program stored in 'norepeats'
	printf("\n");

	SetSeed(); 												 // Seed file for RNG set here		
	const float RANGE = 10000;                              // Random number generated is below 1
	rand1 = (float) rand() * RANGE / RAND_MAX;

	for(j=1;j<norepeats;j++){
                                              
		FILE *input_txt;        
	
		input_txt = fopen("input.txt","r");               
   
		for(i = 0; i < n; i++){
                                    
			fscanf(input_txt, "%d %d %d %d %d", &matrix[i][0], &matrix[i][1], &matrix[i][2], &matrix[i][3], &matrix[i][4]);
			if (feof(input_txt)) break;
		}
        

		for(i=0;i<n;i++){
			if (matrix[i][1] == 0 && matrix[i][2] == 0){             // If the individual is a founder:
  
				rand1 = (float) rand() * RANGE / RAND_MAX;            // Random number 1 generated 
				if (rand1 <= pp){matrix[i][3] = 0;}                // Place a 0 allele down in column 4 if random number 1 is below the set p value    
				else matrix[i][3] = 1;                    // Place a 1 allele down in column 4 if random number 1 is above the set p value
      
				rand1 = (float) rand() * RANGE / RAND_MAX;            // Random number 2 generated
				if (rand1 <= pp){matrix[i][4] = 0;}                                  // Place a 0 allele down in column 5 if random number 2 is below the set p value
				else matrix[i][4] = 1;                                         // Place a 1 allele down in column 5 if random number 2 is above the set p value
  
			}//end of if
   
		else{
			int dad = matrix[i][1];                               // 1 is "dad" column
			int mum = matrix[i][2];                               // 2 is "mum" column

			int dadRowIndex = dad - 1;                            // The row of the father is 'dad - 1' as the first row is set at zero
			int mumRowIndex = mum -1;                             // The row of the mother is 'mum - 1' as the first row is set at zero

   
			rand1 = (float) rand() * RANGE / RAND_MAX;            // Random number 3 generated
			if (rand1 <= freq){matrix[i][3] = matrix[dadRowIndex][3];}  // If random number 3 is less than a half, receive allele stored in column 4 from dad
			else{matrix[i][3] = matrix[dadRowIndex][4];}             // If random number 3 is more than a half, receive allele stored in column 5 from dad

 
			rand1 = (float) rand() * RANGE / RAND_MAX;            // Random number 4 generated
			if (rand1 <= freq){ matrix[i][4] = matrix[mumRowIndex][3];}        // If random number 4 is less than a half, receive allele stored in column 4 from mum
			else{matrix[i][4] = matrix[mumRowIndex][4];}            // If random number 4 is more than a half, receive allele stored in column 5 from mum

			}//end of else

            if (matrix[i][3] == 0 && matrix[i][4] == 0)   // Counting the number of times AA appear in the 1000 simulations.
     	AA++;
     if (matrix[i][3] == 0 && matrix[i][4] == 1)   // Counting the number of times AB appear in the 1000 simulations.
     	AB++;
     if (matrix[i][3] == 1 && matrix[i][4] == 0)
     AB++; 
     if (matrix[i][3] == 1 && matrix[i][4] == 1)   // Counting the number of times BB appear in the 1000 simulations.
     	BB++;

		}//end of for
 
	 

	}//end of for

    
	printf("the matrix is:\n");
	for(i=0;i<n;i++){
		for(j=0;j<5;j++){
		printf("%d\t", matrix[i][j]);
		}
	printf("\n");
	}

	printf("\n");
	printf("Cumulative genotype totals:\n");        
	printf("AA: %d\n", AA);                      // Print total number of AA individuals
	printf("AB: %d\n", AB);                      // Print total number of heterozygotes
	printf("BB: %d\n", BB);                      // Print total number of BB individuals
	printf("\n");
	printf("\n");



			}//end of main
 
	void SetSeed() 
	{
                                                           // Determining the seed file
     FILE *seed_file;
     long int seed_value;

     seed_file = fopen("seed.txt","r");                   // Open seed file
     fscanf(seed_file,"%d",&seed_value);                  // Exctract seed from file
     fclose(seed_file);                                   // Close seed file
     srand(seed_value);                                   // Randomise seed value
     seed_file = fopen("seed.txt","w");                   // Create temporary output
     fprintf(seed_file,"%d",rand());                      // Print random number to output
     fclose(seed_file);                                   // Close temporary seed output file
	}

What do people reckon?

If it provides the appropriate output for known input and if it runs appropriately given a variety of random input, particularly testing any "margins" or special cases, then it's probably pretty good.

A couple minor points.

In the future when you have programs using predominantly C style I/O please post to the C board rather than the C++ board.

Second, since the if statements used to determine allele status are mutually exclusive, I'd consider using if/else/else if statements instead of serial if statements.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.