C programming Help needed

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

C programming Help needed

Post by hfakar on Tue May 13, 2014 6:47 pm
([msg=80673]see C programming Help needed[/msg])

Hey there! I have an assignment. I m actually not bad at my programming classes but icouldnt figure out how to construct the algorithm at that assignment. Any help will be appreciated.Thats my assignment:

This is a simplified version of color filling problem in computer graphics will be coded in C programming
language. Assume that you are given with a matrix of type integer with initial values presenting an
image (matrix of pixels). Figure 1 shows an image of size 8x8 including a number of closed shapes. Note
that the four side of the image should be considered as surrounded with (imaginary) borders so that the
respective spaces can be accepted as closed spaces. The real borders of the shapes are represented by 1
and paintable cells are initially represented by 0 in a matrix. The coordinates of the top left cell of an
image is 1,1. The four neighbor check will be applied in filling, i.e., only the left, the right, the up and the
down cells will be checked for the borders as given in the example. (The other approach is eight
neighbor check in which one has to check for eight neighboring cells for the borders.)

User is expected to enter a couple of cell coordinates and a value (character) to fill. If the coordinates
are not pointing a border cell, the shape that the cell is in will be filled with the character entered by the
user. The shapes should be re-colorable. After each input triple form the user an updated image should
be printed on screen. User enters 0 0 as coordinates to terminate the program.

My implementation should use ROW and COL as constants to define the size of the
matrix. The submitted codes will be checked by replacing ROW and COL as well as the mat. Please stick to the given
names/identifiers as well (Fig 2.).
You may do an assumption if you need/if it is required as long as it does not contradict with the given
information.
Important note: I am not expected to use recursive calls.



Image

Image

Image
hfakar
New User
New User
 
Posts: 7
Joined: Fri May 09, 2014 6:14 pm
Blog: View Blog (0)


Re: C programming Help needed

Post by Goatboy on Tue May 13, 2014 7:29 pm
([msg=80677]see Re: C programming Help needed[/msg])

So you want us to do your homework for you basically.

And why is this posted in HTS missions? I don't remember ever getting points for doing another person's assignment.

Unless you can post proof that you have at least attempted this, I doubt anyone is going to jump the gun to help you.
Assume that everything I say is or could be a lie.
User avatar
Goatboy
Expert
Expert
 
Posts: 2864
Joined: Mon Jul 07, 2008 9:35 pm
Blog: View Blog (0)


Re: C programming Help needed

Post by limdis on Tue May 13, 2014 7:36 pm
([msg=80679]see Re: C programming Help needed[/msg])

Goatboy wrote:And why is this posted in HTS missions?

I was wrapped up in another thread. Relocated.
"The quieter you become, the more you are able to hear..."
"Drink all the booze, hack all the things."
User avatar
limdis
Moderator
Moderator
 
Posts: 1657
Joined: Mon Jun 28, 2010 5:45 pm
Blog: View Blog (0)


Re: C programming Help needed

Post by hfakar on Tue May 13, 2014 8:40 pm
([msg=80686]see Re: C programming Help needed[/msg])

Im really sorry that i posted this post in the wrong topic.I didnt see the hts mission title. I thought it is discussion section. Now i learned my lesson. thank you for that.

Ofcourse i attempted to write this program. I dont want anyone to do my assignment. As ı said i couldnt find a correct algorithm because im not allowed to use recursive functions. Prohibiting the recursive function is the evilest thing. When i check right, left or up and down i can paint only them. What i want to say is if it is like when user types: 1 2 9
initial matrix
1 0 0 0 0
1 1 0 0 0
1 0 1 1 1
1 0 0 0 0
that matrix should turn into this matrix
1 9 9 9 9
1 1 9 9 9
1 0 1 1 1
1 0 0 0 0
but i can only do this without recursive function
1 9 9 0 0
1 1 0 0 0
1 0 1 1 1
1 0 0 0 0
Last edited by hfakar on Thu May 15, 2014 11:10 am, edited 1 time in total.
Secure? Come onnn...
hfakar
New User
New User
 
Posts: 7
Joined: Fri May 09, 2014 6:14 pm
Blog: View Blog (0)


Re: C programming Help needed

Post by Goatboy on Tue May 13, 2014 11:57 pm
([msg=80689]see Re: C programming Help needed[/msg])

And why can't you just use a while loop?
Assume that everything I say is or could be a lie.
User avatar
Goatboy
Expert
Expert
 
Posts: 2864
Joined: Mon Jul 07, 2008 9:35 pm
Blog: View Blog (0)


Re: C programming Help needed

Post by pretentious on Wed May 14, 2014 6:47 am
([msg=80692]see Re: C programming Help needed[/msg])

hfakar wrote:Prohibiting the recursive function is the evilest thing

Not to derail the topic but I totally disagree, I think using the stack for arithmatic is a terrible thing to teach.
Now I'm not thinking before I type but i dare you to plug a number like 1,000,000 into a fibbonachi recursive function.
Goatboy wrote:Oh, that's simple. All you need to do is dedicate many years of your life to studying security.

IF you feel like exchanging ASCII arrays, let me know ;)
Can you say brainwashing It's a non stop disco
User avatar
pretentious
Addict
Addict
 
Posts: 1203
Joined: Wed Mar 03, 2010 12:48 am
Blog: View Blog (0)


Re: C programming Help needed

Post by tremor77 on Wed May 14, 2014 12:34 pm
([msg=80696]see Re: C programming Help needed[/msg])

Goatboy wrote:And why can't you just use a while loop?


By it's nature a loop will be a recursive function. The object of the lesson is to develop an algorithm that can solve the matrix.

My best shot at using a loop without being recursive would be something like this.. (psuedo-code <- not C lol)

Code: Select all
function check(usercoord)
{
for each coord in coords
{
   if coord = (usercoord) && (value == 0) then setval(coord, newval);
   if coord = (validNeighbor(usercoord)) && (value == 0) then setval(coord, newval);
}
}

function validNeighbor(usercoord)
{
  blocks = [north, south, east, west];
 
  for each block in blocks
    {
        if (block(value) == 0) then { setval(block, newval);  check(block); }
    }
}


Now this depends on your instructors interpretation of recursive function for all instances the above is still recursive while, check doesn't explicitly call itself, validNeighbor calls check. So you can see the issue at hand, avoiding recursion is not so easy..

Now as for the lesson itself, I don't think they the intention is to teach one way or the other.. but to show you where the best approach is put to the best use. Totally agree with pretentious

pretentious wrote:
hfakar wrote:Prohibiting the recursive function is the evilest thing
<br>Not to derail the topic but I totally disagree, I think using the stack for arithmatic is a terrible thing to teach.<br>Now I'm not thinking before I type but i dare you to plug a number like 1,000,000 into a fibbonachi recursive function.


Now I only spent about 5 minutes on this.. and my answer would probably fail. This is a fantastic assignment, forcing you to think about how you might approach something without resorting to the easy fallbacks.
User avatar
tremor77
Addict
Addict
 
Posts: 1098
Joined: Wed Mar 31, 2010 12:00 pm
Location: New York
Blog: View Blog (0)


Re: C programming Help needed

Post by hfakar on Wed May 14, 2014 7:16 pm
([msg=80700]see Re: C programming Help needed[/msg])

Thank you for your interest. Your algorithm is actually good, that helped me about thinking the process.But it would be considered as recursive i guess. I was thinking that even without recursive function it would not be that much dificult but now i understood that is very dificult task and i came up with no solution. I just couldnt. Is there any other way to do this? My code is this and it doesnt completely work.


Code: Select all
#include<stdio.h>
#include <time.h>
#include<stdlib.h>
#define ROW 8
#define COL 8
int a[COL][ROW];
int column, row, value;
void check(int column, int row, int value);
void matrix_printer(void);
int main(void){
   srand(time(NULL));
   int i, j, counter;//i,j and counter are all counters with different name
   
   printf("Initial Matrix\n\n");
   for (i = 0; i < COL; i++){//initial matrix
      for (j = 0; j < ROW; j++){
         a[i][j] = rand() %2;
         printf("%d  ", a[i][j]);
      }
      printf("\n");
   }
   for (i = 1; i < 20; i++){
      printf("Enter a coordinate and a value to point with order ==> column, row, value:");
      scanf("%d%d%d", &row, &column, &value);
      if (row == 0 || column == 0){
         printf("Terminated.\n");
         printf("The final matrix\n\n");
         matrix_printer();
         break;
      }
      else
      {
      if (column <= COL && row <= ROW){
         if (a[column-1][row-1] != 1){
            a[column - 1][row - 1] = value;
            check(column, row,value );
         }
         else{
            printf("It's border cell, please enter another coordinate\n");
         }

      }
      else{
         printf("Matrix size should fit to the size of %dx%d\n", COL, ROW);
      }
      matrix_printer();
   }
   }

   
   system("pause");
}
void check(column, row, value){
   int north = a[column - 1][row - 2];
   int south = a[column - 1][row];
   int west = a[column - 2][row - 1];
   int east = a[column][row - 1];
   if (north != 1){
      a[column - 1][row - 2] = value;
   }
   if (south == 0){
      a[column - 1][row] = value;
   }

   if (west != 1){
      a[column - 2][row - 1] = value;

   }
   if (east != 1){
      a[column][row - 1] = value;
   }
}
void matrix_printer(void){
   int counter, j;
   for (counter = 0; counter < COL; counter++){
   for (j = 0; j < ROW; j++){

         printf("%d  ", a[counter][j]);
      }
      printf("\n");
   }

}
Secure? Come onnn...
hfakar
New User
New User
 
Posts: 7
Joined: Fri May 09, 2014 6:14 pm
Blog: View Blog (0)


Re: C programming Help needed

Post by Goatboy on Wed May 14, 2014 11:12 pm
([msg=80703]see Re: C programming Help needed[/msg])

If your instructor thinks a while loop counts as recursion then you need to clamp his nuts in a vise and tell him he can only open it without using repetition.
Assume that everything I say is or could be a lie.
User avatar
Goatboy
Expert
Expert
 
Posts: 2864
Joined: Mon Jul 07, 2008 9:35 pm
Blog: View Blog (0)


Re: C programming Help needed

Post by hfakar on Thu May 15, 2014 9:19 am
([msg=80707]see Re: C programming Help needed[/msg])

No i didnt say that to you. I didnt even understand how can i use while loop here.Wouldnt it paint every 0 in the matrix?
I remembered something yesterday. In last class he said Reversal Polish notation will help your assignment. Whıch includes stack in it. How do i use stack here?
Secure? Come onnn...
hfakar
New User
New User
 
Posts: 7
Joined: Fri May 09, 2014 6:14 pm
Blog: View Blog (0)


Next

Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests