Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
March 31st, 2013, 03:25 PM
 fridgedigga
Registered User

Join Date: Mar 2013
Posts: 3
Time spent in forums: 1 h 10 m 31 sec
Reputation Power: 0
2D array elimination

Hey
I'm trying to working on a part of program that involves an 2D array with randomly arrange 4 letter (b, r, g, y - symbolizing color).
Kind of like this. The size can vary.
Code:
```gbbybbbb
ryybbrby
yrbrybyr
yrbybybb
ybybbybg
byygybby
bbggrbbg
brrbgbbr```

The program will prompt the user to enter coordinate thereby picking a letter. For example b, then all the bs that's connected to each other would be eliminated. Then columns will collapse, and shift over to the left.

I'm still pretty new to C so any help will be appreciated.
I'll try to post up pictures but I can't right now...

#2
March 31st, 2013, 03:36 PM
 fridgedigga
Registered User

Join Date: Mar 2013
Posts: 3
Time spent in forums: 1 h 10 m 31 sec
Reputation Power: 0
Code:
```
grygbbb
grbbbbb
grbbbby
grrbbgy
grrrbgy
grrrbby```

and the user enters the coordinates for the very top right b, then you get something like this (elimination of all the connected bs only vertically and horizontally. NOT diagonally connected)
Code:
```gryg
gr
gr    y
grr  gy
grrr gy
grrr  y```

then the columns will collapse so there's no letter that has no letter underneath it, as if by virtual gravity
Code:
```gr
gr
gry    y
grrg   y
grrr  gy
grrr  gy```

then the columns shift over to that left so that columns with no elements are eliminated
Code:
```gr
gr
gry  y
grrg y
grrrgy
grrrgy```

And the user continues to enter coordinates until no more letters can be eliminated.

#3
April 1st, 2013, 01:31 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,246
Time spent in forums: 2 Months 4 Weeks 1 Day 11 h 1 m 39 sec
Reputation Power: 1809

Because it looks like a homework dump and expectation of complete code on a plate in return, and that just isn't going to happen.

You showed three distinct steps, so how about 3 functions.
Code:
```#define SIZE 10

// eliminate the connected letters starting at x,y
void eliminate ( char grid[SIZE][SIZE], int x, int y );

// do the gravity bit
void collapse ( char grid[SIZE][SIZE] );

// do the shift bit
void shift ( char grid[SIZE][SIZE] );

// For good measure, and an aid to debugging
// just print the grid
void print ( char grid[SIZE][SIZE] );```
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

#4
April 1st, 2013, 03:39 AM
 fridgedigga
Registered User

Join Date: Mar 2013
Posts: 3
Time spent in forums: 1 h 10 m 31 sec
Reputation Power: 0
Quote:
 Originally Posted by salem So what's your question? Because it looks like a homework dump and expectation of complete code on a plate in return, and that just isn't going to happen. You showed three distinct steps, so how about 3 functions. Code: ```#define SIZE 10 // eliminate the connected letters starting at x,y void eliminate ( char grid[SIZE][SIZE], int x, int y ); // do the gravity bit void collapse ( char grid[SIZE][SIZE] ); // do the shift bit void shift ( char grid[SIZE][SIZE] ); // For good measure, and an aid to debugging // just print the grid void print ( char grid[SIZE][SIZE] );```

I don't expect a full code. That would be way too generous. Just any hints or tips.
A short pseudo code would be awesome.

I'm wondering whether the first function would be necessary though. I don't necessarily need a turn the characters into a space. So long as the 1st grid ends up looking like the bottom grid, it's all good.

#5
April 1st, 2013, 03:49 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,246
Time spent in forums: 2 Months 4 Weeks 1 Day 11 h 1 m 39 sec
Reputation Power: 1809
You need to post some actual code (or actual pseudocode), so we can see exactly what you're capable of, and thus tune the advice and help accordingly.

Simply spoon-feeding anything beforehand suffers from the goldilocks problem (I knew that already , just what I needed , I'm still confused )

But I'm guessing you'll need this to complete step 1.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > 2D array elimination