Counting numbers in an array

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
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

Counting numbers in an array

 
0
  #1
Feb 1st, 2008
Hi guys,

I have an array which reads in a file and displays it to the screen. The output is as follows:

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

I then used a random number generator to add a 1 or a 0 to a further 2 columns in the array. An example of an output is as follows:

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

I was wondering if anyone knew how to count the 1's and 0's I have in the 4th and 5th columns? For the above example, below the output of the array, I would like it to print the following to the screen:

Number of 0's present: 6
Number of 1's present: 14

I'm struggling with the code. Does anyone have any ideas? My current code is:

  1. #include <iostream.h>
  2. #include <fstream.h>
  3. #include <stdlib.h>
  4.  
  5. int j;
  6. int matrix[100][5] = {0};
  7. int numCols = 3;
  8. int numRows = 0;
  9. float rand1, rand2, rand3, rand4;
  10. void SetSeed();
  11.  
  12. int main () {
  13.  
  14. const float RANGE = 1.0;
  15. SetSeed();
  16.  
  17. ifstream inFile;
  18. inFile.open("input.txt");
  19.  
  20. if (!inFile)
  21. {
  22. printf ("Unable to open input file");
  23. exit(1);
  24. }
  25.  
  26. while (inFile >> matrix[numRows][0])
  27. {
  28. printf ("%d\t", matrix[numRows][0]);
  29. for (j = 1; j < numCols; j++)
  30. {
  31. inFile >> matrix[numRows][j];
  32. printf ("%d\t", matrix[numRows][j]);
  33. }
  34.  
  35. int counter;
  36.  
  37. if (matrix[numRows][1] == 0 && matrix[numRows][2] == 0){
  38. for (counter = 1;counter <= 1;counter++){
  39. rand1 = (float) rand() * RANGE / RAND_MAX;
  40. rand2 = (float) rand() * RANGE / RAND_MAX;
  41. }
  42. if (rand1 < 0.4){
  43. matrix[numRows][3] = 0;
  44. printf("\t%d", matrix[numRows][3]);
  45. }
  46. if (rand1 >= 0.4){
  47. matrix[numRows][3] = 1;
  48. printf("\t%d", matrix[numRows][3]);
  49. }
  50. if (rand2 < 0.4){
  51. matrix[numRows][4] = 0;
  52. printf("\t%d", matrix[numRows][4]);
  53. }
  54. if (rand2 >= 0.4){
  55. matrix[numRows][4] = 1;
  56. printf("\t%d", matrix[numRows][4]);
  57. }
  58. }
  59.  
  60. else{
  61.  
  62. for (counter = 1;counter <= 1;counter++){
  63. rand3 = (float) rand() * RANGE / RAND_MAX;
  64. rand4 = (float) rand() * RANGE / RAND_MAX;
  65. }
  66. if (rand3 < 0.5){
  67. matrix[numRows][3] = 0;
  68. printf("\t%d", matrix[numRows][3]);
  69. }
  70. if (rand3 >= 0.5){
  71. matrix[numRows][3] = 1;
  72. printf("\t%d", matrix[numRows][3]);
  73. }
  74. if (rand4 < 0.5){
  75. matrix[numRows][4] = 0;
  76. printf("\t%d", matrix[numRows][4]);
  77. }
  78. if (rand4 >= 0.5){
  79. matrix[numRows][4] = 1;
  80. printf("\t%d", matrix[numRows][4]);
  81. }
  82. }
  83.  
  84.  
  85.  
  86. numRows++;
  87. printf ("\n");
  88. }
  89.  
  90. inFile.close ();
  91. }
  92.  
  93. void SetSeed() {
  94.  
  95. FILE *seed_file;
  96. long int seed_value;
  97.  
  98. seed_file = fopen("seed.txt","r");
  99. fscanf(seed_file,"%d",&seed_value);
  100. fclose(seed_file);
  101. srand(seed_value);
  102. seed_file = fopen("seed.txt","w");
  103. fprintf(seed_file,"%d",rand());
  104. fclose(seed_file);
  105. }

Thanks for any help you can give me
Last edited by Ancient Dragon; Feb 1st, 2008 at 8:17 pm. Reason: add line numbers for easier referencing
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,663
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Counting numbers in an array

 
0
  #2
Feb 1st, 2008
delete lines 38 and 41 because tha loop only executes once, so there is no point to having a loop there.

line 41: just make it a simple else statement. If the value is not < 0.4 then there is only one other choice, That whole loop can be simplified to this:
  1. if (matrix[numRows][1] == 0 && matrix[numRows][2] == 0){
  2. rand1 = (float) rand() * RANGE / RAND_MAX;
  3. rand2 = (float) rand() * RANGE / RAND_MAX;
  4. if (rand1 < 0.4){
  5. matrix[numRows][3] = 0;
  6. }
  7. else{
  8. matrix[numRows][3] = 1;
  9. }
  10. printf("\t%d", matrix[numRows][3]);
  11.  
  12. if (rand2 < 0.4){
  13. matrix[numRows][4] = 0;
  14. }
  15. else{
  16. matrix[numRows][4] = 1;
  17. }
  18. printf("\t%d", matrix[numRows][4]);
  19. }
  20.  
  21. else{
  22.  
  23. rand3 = (float) rand() * RANGE / RAND_MAX;
  24. rand4 = (float) rand() * RANGE / RAND_MAX;
  25.  
  26. if (rand3 < 0.5){
  27. matrix[numRows][3] = 0;
  28. }
  29. else{
  30. matrix[numRows][3] = 1;
  31. }
  32. printf("\t%d", matrix[numRows][3]);
  33.  
  34. if (rand4 < 0.5){
  35. matrix[numRows][4] = 0;
  36. }
  37. else{
  38. matrix[numRows][4] = 1;
  39. }
  40. printf("\t%d", matrix[numRows][4]);
  41. }

numRows++;
printf ("\n");
}
[/code]
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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: Counting numbers in an array

 
0
  #3
Feb 1st, 2008
Cheers for that mate, my code looks a bit neater now and it makes more sense.

Do you have any tips for the counting question though?

Any help is welcomed, thanks!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,663
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Counting numbers in an array

 
0
  #4
Feb 1st, 2008
one way is to use another matrix
  1. int counters[2] = {0}
  2.  
  3. for(int i = 0; i < numRows; ++i)
  4. {
  5. counters[ matrix[i][3] ]++; // count the number of 0s or 1s in the 4th column
  6. counters[ matrix[i][4] ]++; // count the number of 0s or 1s in the 5th column
  7. }

The above uses the 0 or 1 in the matrix column to index into the counters array.
Last edited by Ancient Dragon; Feb 1st, 2008 at 8:39 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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: Counting numbers in an array

 
0
  #5
Feb 1st, 2008
Ok, I'm thinking of placing the int counters[2]={0} at the top of my code as a global variable, and placing the code below it at line 89. Would this be appropriate placement? Or would you put them in all together at a different line?

I'm very unsure as to how to get the program to print out the results of the count? Do you have any tips?

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,663
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Counting numbers in an array

 
0
  #6
Feb 1st, 2008
>>I'm very unsure as to how to get the program to print out the results of the count? Do you have any tips?

Yes -- do it similar to how you did the other printf() lines

As for your first question -- give it your best shot and test your program to see if it works. If it doesn't work then try something else. Find out what kinds of solution(s) you can come up with.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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: Counting numbers in an array

 
0
  #7
Feb 1st, 2008
Ok I'm really struggling with positioning, sorry.

I've placed the variable globally, and then placed the code as follows:

  1. while (inFile >> matrix[numRows][0])
  2. {
  3. printf ("%d\t", matrix[numRows][0]);
  4. for (j = 1; j < numCols; j++)
  5. {
  6. inFile >> matrix[numRows][j];
  7. printf ("%d\t", matrix[numRows][j]);
  8. }
  9.  
  10. for(int i = 0; i < numRows; ++i)
  11. {
  12. counters[ matrix[i][3] ]++; // count the number of 0s or 1s in the 4th column
  13. counters[ matrix[i][4] ]++; // count the number of 0s or 1s in the 5th column
  14. }
  15.  
  16. int counter;

I get 3 error messages:
1. Keyword 'int' cannot be declared as a variable
2. Terminating semicolon missing
3. 'numCols' has not been declared

However, none of these make sense. Hmm, I have tried, but I just can't get it?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 234
Reputation: JRM will become famous soon enough JRM will become famous soon enough 
Solved Threads: 14
JRM's Avatar
JRM JRM is offline Offline
Posting Whiz in Training

Re: Counting numbers in an array

 
0
  #8
Feb 1st, 2008
your compiler should tell what lines they were on.
the first two are simple syntax errors
Perhaps the 'int' keyword problem is related to the undeclared variable 'numCols' where you ment to write:

int numCols;

but wrote

int int

which would give those exact three.

Do a string search on your code for int.
"I like beating by head against the wall because it feels so good when I stop"
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,663
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Counting numbers in an array

 
0
  #9
Feb 1st, 2008
Originally Posted by andyg55 View Post
Ok I'm really struggling with positioning, sorry.

I've placed the variable globally, and then placed the code as follows:

  1. while (inFile >> matrix[numRows][0])
  2. {
  3. printf ("%d\t", matrix[numRows][0]);
  4. for (j = 1; j < numCols; j++)
  5. {
  6. inFile >> matrix[numRows][j];
  7. printf ("%d\t", matrix[numRows][j]);
  8. }
  9.  
  10. for(int i = 0; i < numRows; ++i)
  11. {
  12. counters[ matrix[i][3] ]++; // count the number of 0s or 1s in the 4th column
  13. counters[ matrix[i][4] ]++; // count the number of 0s or 1s in the 5th column
  14. }
  15.  
  16. int counter;

I get 3 error messages:
1. Keyword 'int' cannot be declared as a variable
2. Terminating semicolon missing
3. 'numCols' has not been declared

However, none of these make sense. Hmm, I have tried, but I just can't get it?
1) counting the number of 0s and 1s can not be done until after the entire file has been read in and after the 4th and 5th columns have been completed. You put the cart before the horse.

2) int counter; What is that?
Last edited by Ancient Dragon; Feb 1st, 2008 at 10:09 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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: Counting numbers in an array

 
0
  #10
Feb 1st, 2008
sorry yes, I got rid of int counter;

Forget that!

I placed the code as follows:

  1. else{
  2. int dad = matrix[i][1]; // 1 is "dad" column
  3. int mom = matrix[i][2]; // 2 is "mom" column
  4.  
  5. int dadRowIndex = dad - 1;
  6. int momRowIndex = mom -1;
  7.  
  8. rand3 = (float) rand() * RANGE / RAND_MAX;
  9. rand4 = (float) rand() * RANGE / RAND_MAX;
  10.  
  11. if (rand3 < 0.5){
  12. matrix[i][3] = matrix[dadRowIndex][3];
  13. }
  14. else{
  15. matrix[i][3] = matrix[dadRowIndex][4];
  16. }
  17. printf("\t%d", matrix[i][3]);
  18.  
  19. if (rand4 < 0.5){
  20. matrix[i][4] = matrix[momRowIndex][3];
  21. }
  22. else{
  23. matrix[i][4] = matrix[momRowIndex][4];
  24. }
  25. printf("\t%d", matrix[i][4]);
  26. }
  27.  
  28. i++;
  29. printf ("\n");
  30. }
  31.  
  32. for(int i = 0; i < i; ++i)
  33. {
  34. counters[ matrix[i][3] ]++; // count the number of 0s or 1s in the 4th column
  35. counters[ matrix[i][4] ]++; // count the number of 0s or 1s in the 5th column
  36. }
  37.  
  38.  
  39. inFile.close ();
  40. }

My program seems to run fine!! However, there is obviously no count being printed to screen.

Would I go something like:

  1. printf("counters[ matrix[i][3] ]")
  2. printf("counters[ matrix[i][4] ]")

or

  1. printf("counters[ matrix[i][3] ]++")
  2. printf("counters[ matrix[i][4] ]++")

Am I on the right track?
Reply With Quote Quick reply to this message  
Reply

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




Views: 5594 | Replies: 48
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC