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
  #21
Mar 28th, 2008
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)

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

What do people reckon?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Counter help

 
0
  #22
Mar 28th, 2008
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.
Last edited by Lerner; Mar 28th, 2008 at 4:28 pm.
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
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC