Task Problem!!!!

I

Ivo

Could someone please solve the following problem:

There is a given txt file like this (example):

00011100001000
00100011110100
00100000000010
00010000000001
00010000000010
00001000000100
00000111111000

We should read the txt file and...
The 1's make an outline (shape) - contour. This info should be placed
in
a matrix [rxc] elements. The Task is to fill this shape (to color) the
contour with 1's - inside. Such colored matrix should be printed.

Please if someone solves the problem send it to me:
(e-mail address removed)
 
T

TroLoo

a matrix [rxc] elements. The Task is to fill this shape (to color) the
contour with 1's - inside. Such colored matrix should be printed.

Please if someone solves the problem send it to me:
(e-mail address removed)
What's the problem? I assume that you don't have problem with reading a
file and other simple stidio stuff. You just have to read row by row
(your matrix) and after finding first 1, replace all 0's with 1's until
finding the second 1. That's it, if you have problem with reading a file
let me know on priv.
 
T

TroLoo

a matrix [rxc] elements. The Task is to fill this shape (to color) the
contour with 1's - inside. Such colored matrix should be printed.

Please if someone solves the problem send it to me:
(e-mail address removed)
What's the problem? I assume that you don't have problem with reading a
file and other simple stdio stuff. You just have to read row by row
(your matrix) and after finding first 1, replace all 0's with 1's until
finding the second 1. That's it, if you have problem with reading a file
let me know on priv.
 
C

Charles Harrison Caudill

Ivo said:
Could someone please solve the following problem: <snip>

Here's your mistake. comp.lang.c is not here to solve your algorithmic
problems for you. This newsgroup is for the C language. To find a function
to do your file io, try comp.std.c.
 
S

Simon Biber

Ivo said:
Could someone please solve the following problem:

There is a given txt file like this (example):

00011100001000
00100011110100
00100000000010
00010000000001
00010000000010
00001000000100
00000111111000

We should read the txt file and...
The 1's make an outline (shape) - contour. This info should be placed
in
a matrix [rxc] elements. The Task is to fill this shape (to color) the
contour with 1's - inside. Such colored matrix should be printed.

you want something that detects the border of ones and does not change
them? Hint -- investigate recursive algorithms.

Here's some untested code to give you the gist:

void fill(int area[C][R], int x, int y, int fromcolour, int tocolour)
{
if(area[x][y] == fromcolour)
{
area[x][y] = tocolour;
fill(area, x + 1, y, fromcolour, tocolour);
fill(area, x - 1, y, fromcolour, tocolour);
fill(area, x, y + 1, fromcolour, tocolour);
fill(area, x, y - 1, fromcolour, tocolour);
}
}

With an initial call like
fill(area, 3, 1, 0, 1);
where the initial x,y point is inside the shape you want coloured.
 
J

Jack Klein

Here's your mistake. comp.lang.c is not here to solve your algorithmic
problems for you. This newsgroup is for the C language. To find a function
to do your file io, try comp.std.c.

Don't redirect people to newsgroups when you do not know their topic.
This post would be totally off-topic in comp.std.c.

[posted & mailed]

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
C

Charles Harrison Caudill

Jack Klein said:
On 3 Dec 2003 03:15:07 GMT, Charles Harrison Caudill
Don't redirect people to newsgroups when you do not know their topic.

Then comp.std.c should post a topic and/or a suggestion should not be taken as
law.
This post would be totally off-topic in comp.std.c.

agreed, see above.

[posted & mailed]
 
P

Peter Shaggy Haywood

Groovy hepcat Simon Biber was jivin' on Wed, 3 Dec 2003 14:52:59 +1100
in comp.lang.c.
Re: Task Problem!!!!'s a cool scene! Dig it!
Ivo said:
Could someone please solve the following problem:

There is a given txt file like this (example):

00011100001000
00100011110100
00100000000010
00010000000001
00010000000010
00001000000100
00000111111000

We should read the txt file and...
The 1's make an outline (shape) - contour. This info should be placed
in
a matrix [rxc] elements. The Task is to fill this shape (to color) the
contour with 1's - inside. Such colored matrix should be printed.

you want something that detects the border of ones and does not change
them? Hint -- investigate recursive algorithms.

Nonsense. This is an iterative problem, not a recursive one.
Here's some untested code to give you the gist:

void fill(int area[C][R], int x, int y, int fromcolour, int tocolour)
{
if(area[x][y] == fromcolour)
{
area[x][y] = tocolour;
fill(area, x + 1, y, fromcolour, tocolour);
fill(area, x - 1, y, fromcolour, tocolour);
fill(area, x, y + 1, fromcolour, tocolour);
fill(area, x, y - 1, fromcolour, tocolour);
}
}

I suggest you test it. You may experience a stack overflow problem
(on stack based implementations), especially with a large region to be
filled. This recursive solution is very bad. A better way of doing
what you suggest is iterative, or at least a mix of iteration and
recursion, like the following:

#include <stdio.h>

static void xfill(unsigned char *rgn, size_t w, size_t h, size_t x,
size_t y, int clr, int bg_clr)
{
long start, end;

/* don't go off edge of region */
if(x >= w || y >= h)
return;

/* don't go over different colour */
if(rgn[y * w + x] != bg_clr)
return;

for(end = x; end < w && rgn[y * w + end] == bg_clr; end++)
rgn[y * w + end] = clr;
for(start = x - 1; start >= 0 && rgn[y * w + start] == bg_clr;
start--)
rgn[y * w + start] = clr;

/* recurse for < y */
if(y > 0)
for(start++, x = start; start < end; start++)
xfill(rgn, w, h, start, y - 1, clr, bg_clr);

/* recurse for > y */
if(y < h - 1)
for(start = x; start < end; start++)
xfill(rgn, w, h, start, y + 1, clr, bg_clr);
}

void fill(unsigned char *rgn, size_t w, size_t h, size_t x, size_t y,
int clr)
{
unsigned char bg_clr = rgn[y * w + x];

/* don't bother to fill same colour */
if(clr != bg_clr)
xfill(rgn, w, h, x, y, clr, bg_clr);
}

#define MAP_WIDTH 15
#define MAP_HEIGHT (sizeof map / sizeof *map)

#define X_Fill_COORD 3
#define Y_FILL_COORD 1

#define FILL_CHAR '2'

int main(void)
{
char map[][MAP_WIDTH] =
{
"00011100001000",
"00100011110100",
"00100000000010",
"00010000000001",
"00010000000010",
"00001000000100",
"00000111111000"
};
int i;

puts("Before the fill:");
for(i = 0; i < MAP_HEIGHT; i++)
{
puts(map);
}

fill((char *)map, MAP_WIDTH, MAP_HEIGHT, X_Fill_COORD, Y_FILL_COORD,
FILL_CHAR);

puts("\nAfter the fill:");
for(i = 0; i < MAP_HEIGHT; i++)
{
puts(map);
}

return 0;
}

However, that may be overkill for the OP's purposes. As TroLoo
suggested, all he has to do is scan each line until he finds the
outline, then store the fill character until he again encounters the
outline, then continue scanning the rest of the line. This is a
completely iterative solution to the problem, and wouldn't require the
OP to first find a set of coordinates inside the outline (as the
solutions you and I posted do).

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,109
Messages
2,570,671
Members
47,263
Latest member
SyreetaGru

Latest Threads

Top