Counter help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 70
Reputation: andyg55 is an unknown quantity at this point 
Solved Threads: 0
andyg55 andyg55 is offline Offline
Junior Poster in Training

Re: Counter help

 
0
  #11
Mar 27th, 2008
Oh, and I tried the above suggestion, but that didn't work either. Still weird numbers.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Counter help

 
0
  #12
Mar 27th, 2008
> ... 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] ;
Last edited by vijayan121; Mar 27th, 2008 at 3:27 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,842
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Counter help

 
0
  #13
Mar 27th, 2008
Originally Posted by andyg55 View Post
Hate to do this vernon but...

counters array???
I think I cut and pasted your first post before I saw your second. You had:
  1. 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.
Last edited by VernonDozier; Mar 27th, 2008 at 3:28 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Counter help

 
0
  #14
Mar 27th, 2008
The code you posted previously for doing the counting:
  1. for(i = 0; i < 10; i++) // Set the counter for the genotypes
  2. {
  3. if (matrix[i][3]==0 && matrix[i][4] ==0) // If alleles AA, increase counter [0][0] by 1
  4. {
  5. counters[0][0]++;
  6. }
  7.  
  8. else
  9. 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
  10. {
  11. counters[0][1]++;
  12. }
  13.  
  14. else
  15. if (matrix[i][3]==1 && matrix[i][4] ==1) // If alleles BB, increase counter [1][1] by 1
  16. {
  17. counters[1][1]++;
  18. }
  19. }
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?
Last edited by vmanes; Mar 27th, 2008 at 3:30 pm.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 70
Reputation: andyg55 is an unknown quantity at this point 
Solved Threads: 0
andyg55 andyg55 is offline Offline
Junior Poster in Training

Re: Counter help

 
0
  #15
Mar 27th, 2008
That asseret didnt work either... just doubles 10 and 11, leaving 00 as zero.

Yeah the counters are there... they're just not working
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 70
Reputation: andyg55 is an unknown quantity at this point 
Solved Threads: 0
andyg55 andyg55 is offline Offline
Junior Poster in Training

Re: Counter help

 
0
  #16
Mar 27th, 2008
yeah i tried changing that part to different combinations of code that mean the same thing... no change though
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Counter help

 
0
  #17
Mar 27th, 2008
Please show your current version. And a set of results that it generates.
Last edited by vmanes; Mar 27th, 2008 at 3:36 pm.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 70
Reputation: andyg55 is an unknown quantity at this point 
Solved Threads: 0
andyg55 andyg55 is offline Offline
Junior Poster in Training

Re: Counter help

 
0
  #18
Mar 27th, 2008
  1. #include <fstream.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4.  
  5.  
  6. int i, j;
  7. int matrix[10000][5] = {0}; //Define a matrix with a maximum of 100 rows and 6 columns
  8. int counters[2][2] = {0}; // Define the genotype counter
  9. int repeats = 0;
  10. int norepeats;
  11. float p, q;
  12. float rand1, rand2, rand3, rand4;
  13. void SetSeed();
  14.  
  15. int main () {
  16.  
  17. FILE *output_file = fopen("output.txt", "w");
  18.  
  19. printf("Enter the frequency of allele A: ");
  20. scanf("%f", &p);
  21. printf("\n");
  22. printf("Frequency of allele A (p) is: %.3f\n", p); // States the frequency of the A allele
  23. q = 1 - p; // The value of q is 1 - (the probability entered for p)
  24.  
  25. printf("Frequency of allele B (q) is: %.3f\n", q); // States the frequency of the B allele
  26. printf("\n");
  27. printf("Please enter the desired number of repeats: ");
  28. scanf("%d", &norepeats); // Desired number of repeats of the program stored in 'norepeats'
  29. printf("\n");
  30.  
  31. while (repeats != norepeats){ // A while loop to loop the program 'norepeats' amount of times
  32.  
  33. const float RANGE = 1.0; // Random number generated is below 1
  34. SetSeed(); // Seed file for RNG set here
  35.  
  36. //Open the input file//
  37. ifstream inFile;
  38. inFile.open("input.txt"); // Genealogy input file opened
  39.  
  40. if (!inFile)
  41. {
  42. printf ("Unable to open input file"); // Error message reported if input.txt cannot be opened
  43. exit(1);
  44. }
  45.  
  46. //Read the input file into the array
  47. while (inFile >> matrix[i][0])
  48. {
  49. printf ("%d\t", matrix[i][0]);
  50. for (j = 1; j < 3; j++)
  51. {
  52. inFile >> matrix[i][j];
  53. printf ("%d\t", matrix[i][j]);
  54. }
  55.  
  56. int counter;
  57.  
  58. if (matrix[i][1] == 0 && matrix[i][2] == 0){ // If the individual is a founder:
  59.  
  60. rand1 = (float) rand() * RANGE / RAND_MAX; // Random number 1 generated
  61. rand2 = (float) rand() * RANGE / RAND_MAX; // Random number 2 generated
  62.  
  63. if (rand1 < p){ // Place a 0 allele down in column 4 if random number 1 is below the set p value
  64. matrix[i][3] = 0;
  65. }
  66. else{
  67. matrix[i][3] = 1; // Place a 1 allele down in column 4 if random number 1 is above the set p value
  68. }
  69. printf("\t%d", matrix[i][3]);
  70.  
  71.  
  72. if (rand2 < p){ // Place a 0 allele down in column 5 if random number 2 is below the set p value
  73. matrix[i][4] = 0;
  74. }
  75. else{ // Place a 1 allele down in column 5 if random number 2 is above the set p value
  76. matrix[i][4] = 1;
  77. }
  78. printf("\t%d", matrix[i][4]);
  79. }
  80.  
  81. else{
  82. int dad = matrix[i][1]; // 1 is "dad" column
  83. int mum = matrix[i][2]; // 2 is "mum" column
  84.  
  85. int dadRowIndex = dad - 1; // The row of the father is 'dad - 1' as the first row is set at zero
  86. int mumRowIndex = mum -1; // The row of the mother is 'mum - 1' as the first row is set at zero
  87.  
  88. for (counter = 1;counter <= 1;counter++){
  89. rand3 = (float) rand() * RANGE / RAND_MAX; // Random number 3 generated
  90. rand4 = (float) rand() * RANGE / RAND_MAX; // Random number 4 generated
  91. }
  92. if (rand3 < 0.5){ // If random number 3 is less than a half, receive allele stored in column 4 from dad
  93. matrix[i][3] = matrix[dadRowIndex][3];
  94. }
  95. else{ // If random number 3 is more than a half, receive allele stored in column 5 from dad
  96. matrix[i][3] = matrix[dadRowIndex][4];
  97. }
  98. printf("\t%d", matrix[i][3]);
  99.  
  100. if (rand4 < 0.5){ // If random number 4 is less than a half, receive allele stored in column 4 from mum
  101. matrix[i][4] = matrix[mumRowIndex][3];
  102. }
  103. else{ // If random number 4 is more than a half, receive allele stored in column 5 from mum
  104. matrix[i][4] = matrix[mumRowIndex][4];
  105. }
  106. printf("\t%d", matrix[i][4]);
  107. }
  108.  
  109. i++;
  110. printf ("\n");
  111. }
  112.  
  113. inFile.close (); // Close the infile
  114.  
  115. for(i = 0; i < 10; i++) // Set the counter for the genotypes
  116. {
  117. assert( ( matrix[i][3] == 0 ) || ( matrix[i][3] == 1 ) ) ;
  118. assert( ( matrix[i][4] == 0 ) || ( matrix[i][4] == 1 ) ) ;
  119. int sum = matrix[i][3] + matrix[i][4] ;
  120. if( sum == 2 ) ++counters[1][1] ;
  121. else if( sum == 1 ) ++counters[0][1] ;
  122. else ++counters[0][0] ;
  123. }
  124.  
  125. printf("\n");
  126. printf("Cumulative genotype totals:\n");
  127. printf("AA: %d\n", counters[0][0]); // Print total number of AA individuals
  128. printf("AB: %d\n", counters[0][1]); // Print total number of heterozygotes
  129. printf("BB: %d\n", counters[1][1]); // Print total number of BB individuals
  130. printf("\n");
  131. printf("\n");
  132. repeats++; // End loop here
  133. }
  134.  
  135. fclose(output_file);
  136.  
  137. }
  138.  
  139. void SetSeed()
  140. {
  141.  
  142. // Determining the seed file
  143. FILE *seed_file;
  144. long int seed_value;
  145.  
  146. seed_file = fopen("seed.txt","r"); // Open seed file
  147. fscanf(seed_file,"%d",&seed_value); // Exctract seed from file
  148. fclose(seed_file); // Close seed file
  149. srand(seed_value); // Randomise seed value
  150. seed_file = fopen("seed.txt","w"); // Create temporary output
  151. fprintf(seed_file,"%d",rand()); // Print random number to output
  152. fclose(seed_file); // Close temporary seed output file
  153. }

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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Counter help

 
0
  #19
Mar 27th, 2008
This bit:
  1. else{
  2. int dad = matrix[i][1]; // 1 is "dad" column
  3. int mum = matrix[i][2]; // 2 is "mum" column
  4.  
  5. int dadRowIndex = dad - 1; // The row of the father is 'dad - 1' as the first row is set at zero
  6. int mumRowIndex = mum -1; // The row of the mother is 'mum - 1' as the first row is set at zero
  7.  
  8. for (counter = 1;counter <= 1;counter++){
  9. rand3 = (float) rand() * RANGE / RAND_MAX; // Random number 3 generated
  10. rand4 = (float) rand() * RANGE / RAND_MAX; // Random number 4 generated
  11. }
  12. if (rand3 < 0.5){ // If random number 3 is less than a half, receive allele stored in column 4 from dad
  13. matrix[i][3] = matrix[dadRowIndex][3];
  14. }
  15. else{ // If random number 3 is more than a half, receive allele stored in column 5 from dad
  16. matrix[i][3] = matrix[dadRowIndex][4];
  17. }
  18. printf("\t%d", matrix[i][3]);
  19.  
  20. if (rand4 < 0.5){ // If random number 4 is less than a half, receive allele stored in column 4 from mum
  21. matrix[i][4] = matrix[mumRowIndex][3];
  22. }
  23. else{ // If random number 4 is more than a half, receive allele stored in column 5 from mum
  24. matrix[i][4] = matrix[mumRowIndex][4];
  25. }
  26. printf("\t%d", matrix[i][4]);
  27. }
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.
  1. for( int r = 0; r < 10; r++ )
  2. {
  3. for( int c = 0; c < 5; c++ )
  4. cout << matrix[r][c] << " " ;
  5. cout << endl;
  6. }
  7.  
  8.  
  9. for(i = 0; i < 10; i++) // Set the counter for the genotypes
  10. {
  11. assert( ( matrix[i][3] == 0 ) || ( matrix[i][3] == 1 ) ) ;
  12. assert( ( matrix[i][4] == 0 ) || ( matrix[i][4] == 1 ) ) ;
  13. int sum = matrix[i][3] + matrix[i][4] ;
  14. if( sum == 2 ) ++counters[1][1] ;
  15. else if( sum == 1 ) ++counters[0][1] ;
  16. else ++counters[0][0] ;
  17. }
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,842
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Counter help

 
0
  #20
Mar 27th, 2008
Originally Posted by andyg55 View Post

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. 1 0 0 0 0
  2. 2 0 0 1 0
  3. 3 0 0 0 1
  4. 4 1 2 1 1
  5. 5 1 2 1 1
  6. 6 0 0 0 1
  7. 7 3 4 1 1
  8. 8 5 6 1 1
  9. 9 7 8 1 1
  10. 10 7 8 1 1
  11.  
  12. Non-Cumulative totals:
  13. 00 = 1 (row 1)
  14. 10 = 3 (rows 2, 3, 6)
  15. 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?
Last edited by VernonDozier; Mar 27th, 2008 at 7:40 pm. Reason: math error
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 1333 | Replies: 21
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC