| | |
Counting numbers in an array
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2008
Posts: 70
Reputation:
Solved Threads: 0
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:
Thanks for any help you can give me
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:
c++ Syntax (Toggle Plain Text)
#include <iostream.h> #include <fstream.h> #include <stdlib.h> int j; int matrix[100][5] = {0}; int numCols = 3; int numRows = 0; float rand1, rand2, rand3, rand4; void SetSeed(); int main () { const float RANGE = 1.0; SetSeed(); ifstream inFile; inFile.open("input.txt"); if (!inFile) { printf ("Unable to open input file"); exit(1); } while (inFile >> matrix[numRows][0]) { printf ("%d\t", matrix[numRows][0]); for (j = 1; j < numCols; j++) { inFile >> matrix[numRows][j]; printf ("%d\t", matrix[numRows][j]); } int counter; if (matrix[numRows][1] == 0 && matrix[numRows][2] == 0){ for (counter = 1;counter <= 1;counter++){ rand1 = (float) rand() * RANGE / RAND_MAX; rand2 = (float) rand() * RANGE / RAND_MAX; } if (rand1 < 0.4){ matrix[numRows][3] = 0; printf("\t%d", matrix[numRows][3]); } if (rand1 >= 0.4){ matrix[numRows][3] = 1; printf("\t%d", matrix[numRows][3]); } if (rand2 < 0.4){ matrix[numRows][4] = 0; printf("\t%d", matrix[numRows][4]); } if (rand2 >= 0.4){ matrix[numRows][4] = 1; printf("\t%d", matrix[numRows][4]); } } else{ for (counter = 1;counter <= 1;counter++){ rand3 = (float) rand() * RANGE / RAND_MAX; rand4 = (float) rand() * RANGE / RAND_MAX; } if (rand3 < 0.5){ matrix[numRows][3] = 0; printf("\t%d", matrix[numRows][3]); } if (rand3 >= 0.5){ matrix[numRows][3] = 1; printf("\t%d", matrix[numRows][3]); } if (rand4 < 0.5){ matrix[numRows][4] = 0; printf("\t%d", matrix[numRows][4]); } if (rand4 >= 0.5){ matrix[numRows][4] = 1; printf("\t%d", matrix[numRows][4]); } } numRows++; printf ("\n"); } inFile.close (); } void SetSeed() { FILE *seed_file; long int seed_value; seed_file = fopen("seed.txt","r"); fscanf(seed_file,"%d",&seed_value); fclose(seed_file); srand(seed_value); seed_file = fopen("seed.txt","w"); fprintf(seed_file,"%d",rand()); fclose(seed_file); }
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
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:
numRows++;
printf ("\n");
}
[/code]
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:
C++ Syntax (Toggle Plain Text)
if (matrix[numRows][1] == 0 && matrix[numRows][2] == 0){ rand1 = (float) rand() * RANGE / RAND_MAX; rand2 = (float) rand() * RANGE / RAND_MAX; if (rand1 < 0.4){ matrix[numRows][3] = 0; } else{ matrix[numRows][3] = 1; } printf("\t%d", matrix[numRows][3]); if (rand2 < 0.4){ matrix[numRows][4] = 0; } else{ matrix[numRows][4] = 1; } printf("\t%d", matrix[numRows][4]); } else{ rand3 = (float) rand() * RANGE / RAND_MAX; rand4 = (float) rand() * RANGE / RAND_MAX; if (rand3 < 0.5){ matrix[numRows][3] = 0; } else{ matrix[numRows][3] = 1; } printf("\t%d", matrix[numRows][3]); if (rand4 < 0.5){ matrix[numRows][4] = 0; } else{ matrix[numRows][4] = 1; } printf("\t%d", matrix[numRows][4]); }
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.
one way is to use another matrix
The above uses the 0 or 1 in the matrix column to index into the counters array.
C++ Syntax (Toggle Plain Text)
int counters[2] = {0} for(int i = 0; i < numRows; ++i) { counters[ matrix[i][3] ]++; // count the number of 0s or 1s in the 4th column counters[ matrix[i][4] ]++; // count the number of 0s or 1s in the 5th column }
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.
•
•
Join Date: Jan 2008
Posts: 70
Reputation:
Solved Threads: 0
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.
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.
>>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.
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.
•
•
Join Date: Jan 2008
Posts: 70
Reputation:
Solved Threads: 0
Ok I'm really struggling with positioning, sorry.
I've placed the variable globally, and then placed the code as follows:
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?
I've placed the variable globally, and then placed the code as follows:
C++ Syntax (Toggle Plain Text)
while (inFile >> matrix[numRows][0]) { printf ("%d\t", matrix[numRows][0]); for (j = 1; j < numCols; j++) { inFile >> matrix[numRows][j]; printf ("%d\t", matrix[numRows][j]); } for(int i = 0; i < numRows; ++i) { counters[ matrix[i][3] ]++; // count the number of 0s or 1s in the 4th column counters[ matrix[i][4] ]++; // count the number of 0s or 1s in the 5th column } 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?
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.
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"
•
•
•
•
Ok I'm really struggling with positioning, sorry.
I've placed the variable globally, and then placed the code as follows:
C++ Syntax (Toggle Plain Text)
while (inFile >> matrix[numRows][0]) { printf ("%d\t", matrix[numRows][0]); for (j = 1; j < numCols; j++) { inFile >> matrix[numRows][j]; printf ("%d\t", matrix[numRows][j]); } for(int i = 0; i < numRows; ++i) { counters[ matrix[i][3] ]++; // count the number of 0s or 1s in the 4th column counters[ matrix[i][4] ]++; // count the number of 0s or 1s in the 5th column } 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?
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.
•
•
Join Date: Jan 2008
Posts: 70
Reputation:
Solved Threads: 0
sorry yes, I got rid of int counter;
Forget that!
I placed the code as follows:
My program seems to run fine!! However, there is obviously no count being printed to screen.
Would I go something like:
or
Am I on the right track?
Forget that!
I placed the code as follows:
C++ Syntax (Toggle Plain Text)
else{ int dad = matrix[i][1]; // 1 is "dad" column int mom = matrix[i][2]; // 2 is "mom" column int dadRowIndex = dad - 1; int momRowIndex = mom -1; rand3 = (float) rand() * RANGE / RAND_MAX; rand4 = (float) rand() * RANGE / RAND_MAX; if (rand3 < 0.5){ matrix[i][3] = matrix[dadRowIndex][3]; } else{ matrix[i][3] = matrix[dadRowIndex][4]; } printf("\t%d", matrix[i][3]); if (rand4 < 0.5){ matrix[i][4] = matrix[momRowIndex][3]; } else{ matrix[i][4] = matrix[momRowIndex][4]; } printf("\t%d", matrix[i][4]); } i++; printf ("\n"); } for(int i = 0; i < i; ++i) { counters[ matrix[i][3] ]++; // count the number of 0s or 1s in the 4th column counters[ matrix[i][4] ]++; // count the number of 0s or 1s in the 5th column } inFile.close (); }
My program seems to run fine!! However, there is obviously no count being printed to screen.
Would I go something like:
C++ Syntax (Toggle Plain Text)
printf("counters[ matrix[i][3] ]") printf("counters[ matrix[i][4] ]")
or
C++ Syntax (Toggle Plain Text)
printf("counters[ matrix[i][3] ]++") printf("counters[ matrix[i][4] ]++")
Am I on the right track?
![]() |
Similar Threads
- counting a few numbers and storing the values (C++)
- Rand Max Problem (C++)
- trouble with counting letter from file, please help. (C++)
- Counting occurrence of numbers in C (C)
- How many different numbers are there? (C++)
- Counting the amount of letters in a phrase! (C++)
- counting comparisons when sorting (C++)
Other Threads in the C++ Forum
- Previous Thread: Is it possible to pass back both a return variable & reference variable in one funct?
- Next Thread: Read image pixel values
Views: 5594 | Replies: 48
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






