Re: Identifing an isolated character in a matrix

Please show some code what you have done so far, at least try to create a matrix bool matrix[rows][cols];...

Usually, I don't write a single line of code until I've identified at least how to solve the problem ( via flow chart or pseudo-code )
To be honest, in this cade I've not written anything, just because as I said, I can't think of a single clean solution.
Anyway, I'll try...

Re: Identifing an isolated character in a matrix

Please show some code what you have done so far, at least try to create a matrix bool matrix[rows][cols];...

Usually, I don't write a single line of code until I've identified at least how to solve the problem ( via flow chart or pseudo-code )
To be honest, in this cade I've not written anything, just because as I said, I can't think of a single clean solution.
Anyway, I'll try...

1 Control of there is a single one present

May be decomposed into

0 Initialize counter
1 If there is an isolated 1 on a row
2 If there isn't a 1 around it
3 Increment counter
4 Else, go to the subsequent row.

Expanding line 1...

1 // It doesn't work if the line starts with a 1, and it doesn't recognize if the last character is a 1!
2 While the current value of the current row is 0
3 Go to the next cell
4 If the current value is 1
5 Go to the next cell
6 If the current value is 0
7 Increment i
8 Go to the next cell

It doesn't work if the line starts with a 1, and it doesn't recognize if the last character is a 1!

Anyway, am i pointing in the right direction?
Is the first top-down refinement the one you'd have taken?

Sorry for the double numbers at the beginning of every line.
Didn't know how the [co.de] tags work...

Umh.. I could add

If the current value is a 1
Go to the next line
If the current value is a 0
Increment counter

at the top of the first line to let it recognize the first isolated 1...

Re: Identifing an isolated character in a matrix

Well! Start with something, at least some wrong code, it doesn't matter. All that you have in mind, code them and then we will see if it works and how to make it better...

A pseudo:

bool matrix[2][2] = {{0, 1}, {1, 0}};
//Or simply declare the matrix and assign values to it using nested for loop
bool matrix[2][2];
for i = 1 to 2
for j = 1 to 2
matrix[i][j] = some_value
next j
next i

Re: Identifing an isolated character in a matrix

Posted 12 December 2012 - 05:37 PM

You've been thinking about the 1's, and logically so - but turn that around and focus on the 0's, instead.

Now the algorithm comes to the fore.

Scan through the array, looking for a zero. When you find it, you will check ahead (use another variable for this check), for two digits. If the first digit is a one as you scan ahead, and the second digit is a space, then you have found an isolated one.

else, continue scanning. (yes, you will be scanning over some digits twice [once in the look ahead, and once in the non look ahead scan], but you will be able to optimize that problem away.)

before the main and i've not ye the dimensions of the matrix.
The request is to let the user choose them, so i pass them in the main after i've called a function name set_matrix_dimensions(...), but the compiler doesn't like it!

Excluding the passage via pointers, is there another way to calm the compiler?

Sorry, this are the error messages i got from my IDE. ( when will i be able to edit my previous posts? )

However, let's get back to the last problem. As i said, I should let the user decide the dimensions of the matrix, and i don't wanna use pointers ( because all the code i've written is based on an index-oriented logic ). I guess i could even use pointers and convert them to indexes in the code.
Do anyone know a cleaner solution?

Re: Identifing an isolated character in a matrix

Posted 17 December 2012 - 01:30 PM

I tryed this way but it doesn't seem to work. If I understand correctly is because when the compiler reads something like bitmap[ 2 ][ 4 ] it jumps ( ( 2 * 100 ) + 4 ) - 1 memory location after the first element, right?