• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 07:17
CET 13:17
KST 21:17
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
RSL Season 3 - RO16 Groups C & D Preview0RSL Season 3 - RO16 Groups A & B Preview2TL.net Map Contest #21: Winners12Intel X Team Liquid Seoul event: Showmatches and Meet the Pros10[ASL20] Finals Preview: Arrival13
Community News
[TLMC] Fall/Winter 2025 Ladder Map Rotation12Weekly Cups (Nov 3-9): Clem Conquers in Canada4SC: Evo Complete - Ranked Ladder OPEN ALPHA8StarCraft, SC2, HotS, WC3, Returning to Blizzcon!45$5,000+ WardiTV 2025 Championship7
StarCraft 2
General
Mech is the composition that needs teleportation t RotterdaM "Serral is the GOAT, and it's not close" RSL Season 3 - RO16 Groups C & D Preview [TLMC] Fall/Winter 2025 Ladder Map Rotation TL.net Map Contest #21: Winners
Tourneys
RSL Revival: Season 3 Sparkling Tuna Cup - Weekly Open Tournament Constellation Cup - Main Event - Stellar Fest Tenacious Turtle Tussle Master Swan Open (Global Bronze-Master 2)
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 499 Chilling Adaptation Mutation # 498 Wheel of Misfortune|Cradle of Death Mutation # 497 Battle Haredened Mutation # 496 Endless Infection
Brood War
General
FlaSh on: Biggest Problem With SnOw's Playstyle BW General Discussion What happened to TvZ on Retro? Brood War web app to calculate unit interactions [ASL20] Ask the mapmakers — Drop your questions
Tourneys
[Megathread] Daily Proleagues Small VOD Thread 2.0 [BSL21] RO32 Group D - Sunday 21:00 CET [BSL21] RO32 Group C - Saturday 21:00 CET
Strategy
PvZ map balance Current Meta Simple Questions, Simple Answers How to stay on top of macro?
Other Games
General Games
Path of Exile Stormgate/Frost Giant Megathread Nintendo Switch Thread Clair Obscur - Expedition 33 Beyond All Reason
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread SPIRED by.ASL Mafia {211640}
Community
General
Things Aren’t Peaceful in Palestine US Politics Mega-thread Russo-Ukrainian War Thread Artificial Intelligence Thread Canadian Politics Mega-thread
Fan Clubs
White-Ra Fan Club The herO Fan Club!
Media & Entertainment
[Manga] One Piece Anime Discussion Thread Movie Discussion! Korean Music Discussion Series you have seen recently...
Sports
2024 - 2026 Football Thread Formula 1 Discussion NBA General Discussion MLB/Baseball 2023 TeamLiquid Health and Fitness Initiative For 2023
World Cup 2022
Tech Support
SC2 Client Relocalization [Change SC2 Language] Linksys AE2500 USB WIFI keeps disconnecting Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List
Blogs
Dyadica Gospel – a Pulp No…
Hildegard
Coffee x Performance in Espo…
TrAiDoS
Saturation point
Uldridge
DnB/metal remix FFO Mick Go…
ImbaTosS
Reality "theory" prov…
perfectspheres
Customize Sidebar...

Website Feedback

Closed Threads



Active: 2253 users

c++ help?

Blogs > tossinYoSalad
Post a Reply
1 2 3 Next All
tossinYoSalad
Profile Blog Joined May 2009
United States215 Posts
Last Edited: 2009-10-14 02:15:20
October 14 2009 02:01 GMT
#1
Ok, so this project is due in 2 hours, and I've been working on it all day. It's pretty much done but it's just not working and I just can't stare at it any more. Can someone help?

The project is basically analyzing contiguous spaces in a two-dimensional array (find the blobs). A blob is 2 or more contiguous spaces in the array. I've figured out how to find the blobs using a stack, and I think I implemented it correctly but it's not working for some reason. The code hangs within the while loop so I'm assuming something in there is wrong. here is the code:

http://pastebin.com/d30e116f3

^^pastebin now.

I know its way over commented, I'm a total noob at c++ (mostly code in java,asm, and python) and it got confusing with braces and shit. I also know this could be done way more efficiently but I don't really care at this point, I just need it to work. The following DOES work: importing grid from text file, displaying the grid, and everything else. the above method hangs in the while loop and I can't figure out why. Help PLEASE!

b3h47pte
Profile Blog Joined May 2007
United States1317 Posts
Last Edited: 2009-10-14 02:05:19
October 14 2009 02:05 GMT
#2
Use http://pastebin.com/
it hurts my eyes looking at that...>>
fabiano
Profile Blog Joined August 2009
Brazil4644 Posts
October 14 2009 02:06 GMT
#3
with no identation its impossible to read anything
"When the geyser died, a probe came out" - SirJolt
tossinYoSalad
Profile Blog Joined May 2009
United States215 Posts
October 14 2009 02:06 GMT
#4
http://pastebin.com/m588911d8

sorry, wish tl had code tags lol.
Eti307
Profile Blog Joined March 2004
Canada3442 Posts
October 14 2009 02:08 GMT
#5
Wow that's way to messy, sorry I'm not touching that
was the //startif //endif comments really necessary? lol

Where exactly does it hang? use the debugger, might give you a clue
tossinYoSalad
Profile Blog Joined May 2009
United States215 Posts
October 14 2009 02:12 GMT
#6
On October 14 2009 11:08 Eti307 wrote:
Wow that's way to messy, sorry I'm not touching that
was the //startif //endif comments really necessary? lol

Where exactly does it hang? use the debugger, might give you a clue


the startif endif i put it so i could check and see if i was missing a bracket when i was fixing syntax errors. i can clean it real fast.
yenta
Profile Blog Joined April 2006
Poland1142 Posts
October 14 2009 02:12 GMT
#7
What are the INPUTs and OUTPUTs of this function supposed to be?
Trutacz Practice Discord - https://discord.gg/PWF7Pv
Eti307
Profile Blog Joined March 2004
Canada3442 Posts
October 14 2009 02:16 GMT
#8
On October 14 2009 11:12 tossinYoSalad wrote:
Show nested quote +
On October 14 2009 11:08 Eti307 wrote:
Wow that's way to messy, sorry I'm not touching that
was the //startif //endif comments really necessary? lol

Where exactly does it hang? use the debugger, might give you a clue


the startif endif i put it so i could check and see if i was missing a bracket when i was fixing syntax errors. i can clean it real fast.


instead of doing that just type in both brackets each and everytime you are using something with brackets. Prevents those type of errors
tossinYoSalad
Profile Blog Joined May 2009
United States215 Posts
Last Edited: 2009-10-14 02:21:40
October 14 2009 02:16 GMT
#9
On October 14 2009 11:12 yenta wrote:
What are the INPUTs and OUTPUTs of this function supposed to be?


all it does is count the number of blobs and keep track of the recursion depth. numBlobs is incremented immediately after the while loop and recDepth is incremented at the end of the function.

there is no input it acts on a grid held in the class (just a two-dim array of ints) its called gridCopy. theGrid is the same thing only its an arr of chars (this teacher is psycho, i had to do it this way)
i know its a horrible implementation believe me.
tossinYoSalad
Profile Blog Joined May 2009
United States215 Posts
October 14 2009 02:17 GMT
#10
On October 14 2009 11:16 Eti307 wrote:
Show nested quote +
On October 14 2009 11:12 tossinYoSalad wrote:
On October 14 2009 11:08 Eti307 wrote:
Wow that's way to messy, sorry I'm not touching that
was the //startif //endif comments really necessary? lol

Where exactly does it hang? use the debugger, might give you a clue


the startif endif i put it so i could check and see if i was missing a bracket when i was fixing syntax errors. i can clean it real fast.


instead of doing that just type in both brackets each and everytime you are using something with brackets. Prevents those type of errors


ya i do, i also needed to put a a few lines in specific places at the end and there were 5 braces in a row at the end lol.
yenta
Profile Blog Joined April 2006
Poland1142 Posts
October 14 2009 02:29 GMT
#11
On October 14 2009 11:16 tossinYoSalad wrote:
Show nested quote +
On October 14 2009 11:12 yenta wrote:
What are the INPUTs and OUTPUTs of this function supposed to be?


all it does is count the number of blobs and keep track of the recursion depth. numBlobs is incremented immediately after the while loop and recDepth is incremented at the end of the function.

i know its a horrible implementation believe me.


Rewrite it like this:

RECURSIVE CALLER
Initialize count
Initialize array of array with flags (w/e you want)
FOR( 0...x )
FOR( 0..y )
if point @ x,y is unchecked & x.y is a space, call recursive worker
//FOR
//FOR
return w/e

RECURSIVE WORKER ( root, current_x, current_y, pointer to 2D array, ref to count )
base case:
return if out of bounds
return if space you're looking has been marked 'looked at'
recursive case:
count adj spaces by calling self with: ( x-1,y ), ( x+1, y), (x, y+1), (x, y-1) + diagonals if they count.
if adj spaces > 0 & root == true, increment counter //endif
mark current space as read
END
Trutacz Practice Discord - https://discord.gg/PWF7Pv
tossinYoSalad
Profile Blog Joined May 2009
United States215 Posts
October 14 2009 02:34 GMT
#12
On October 14 2009 11:29 yenta wrote:
Show nested quote +
On October 14 2009 11:16 tossinYoSalad wrote:
On October 14 2009 11:12 yenta wrote:
What are the INPUTs and OUTPUTs of this function supposed to be?


all it does is count the number of blobs and keep track of the recursion depth. numBlobs is incremented immediately after the while loop and recDepth is incremented at the end of the function.

i know its a horrible implementation believe me.


Rewrite it like this:

RECURSIVE CALLER
Initialize count
Initialize array of array with flags (w/e you want)
FOR( 0...x )
FOR( 0..y )
if point @ x,y is unchecked & x.y is a space, call recursive worker
//FOR
//FOR
return w/e

RECURSIVE WORKER ( root, current_x, current_y, pointer to 2D array, ref to count )
base case:
return if out of bounds
return if space you're looking has been marked 'looked at'
recursive case:
count adj spaces by calling self with: ( x-1,y ), ( x+1, y), (x, y+1), (x, y-1) + diagonals if they count.
if adj spaces > 0 & root == true, increment counter //endif
mark current space as read
END


thanks alot, what do you mean by root though? and because the 2d array is a class var i dont need to pass it though do i?
Slithe
Profile Blog Joined February 2007
United States985 Posts
Last Edited: 2009-10-14 02:39:04
October 14 2009 02:38 GMT
#13
I only very briefly skimmed it so I may be way off, but it looks like your bounds checking is wrong?

Your Code:
if (rowPtr-1 > 0)
if(rowPtr+1 < theGrid.getRows())

I'm assuming you're trying to make sure that 0 <= rowPtr < theGrid.getRows(). If that's the case, then shouldn't it be:

if (rowPtr+1 > 0)
if(rowPtr < theGrid.getRows())
tossinYoSalad
Profile Blog Joined May 2009
United States215 Posts
Last Edited: 2009-10-14 02:50:38
October 14 2009 02:42 GMT
#14
On October 14 2009 11:38 Slithe wrote:
I only very briefly skimmed it so I may be way off, but it looks like your bounds checking is wrong?

Your Code:
if (rowPtr-1 > 0)
if(rowPtr+1 < theGrid.getRows())

I'm assuming you're trying to make sure that 0 <= rowPtr < theGrid.getRows(). If that's the case, then shouldn't it be:

if (rowPtr+1 > 0)
if(rowPtr < theGrid.getRows())


uhh you were sorta right, but you DID point out an error it needs to be if(rowPtr-1 >= 0) otherwise itll never enter those code segments on the leftmost or rightmost bounds.

but yes it needs to be within the bounds of [0,theGrid.getRows()], but because it can only exceed the left bound on a -1, and it can only exceed the right bound on a +1 i ommited those calls.

fixed it to that and still didnt work. I think its a stack error in the original code, but now im rewriting with yenta's suggestion, only thing i dont get is how to actually count the blobs using that method, i can get it to pass over all the blocks, but no way to tell when the end of a blob has been reached.
FaCE_1
Profile Blog Joined December 2006
Canada6178 Posts
October 14 2009 03:02 GMT
#15
due in 2 hours.. if it was due in 2 days, i could help
n_n
tossinYoSalad
Profile Blog Joined May 2009
United States215 Posts
October 14 2009 03:09 GMT
#16
http://pastebin.com/d3e835a75

its been updated. its almost done, can anyone help finish it?
AeTheReal
Profile Joined June 2009
United States108 Posts
October 14 2009 03:22 GMT
#17
Well, that's a major change from when I started looking at it...

As far as I can tell, the previous version of the code did not work because you never modified the value of a cell when you added it to the stack in the while loop. And since you keep on adding more items to the stack which each added more since nothing was being ruled out on subsequent loops, the while loop never ends. Also, you are probably supposed to check your bounds to be "var >= 0" instead of "var > 0" unless you don't need to check the 0 index for some reason.

In your new code your upper bound should be comparing ">=" theGrid.getRows() instead. Same for the .getCols() one.

else if(gridCopy[curr_x][curr_y] == 0){return;}

Use double equal signs for comparisons. Common mistake.

gridCopy[curr_x][curr_y] = 0;

That should go before you make the recursive calls or you'll get the same error.


Hope this helps.
SonuvBob
Profile Blog Joined October 2006
Aiur21550 Posts
Last Edited: 2009-10-14 03:43:16
October 14 2009 03:29 GMT
#18
No idea what this thing is supposed to do, but your counter doesn't increment.

edit: oh, didn't read the whole OP I guess
Administrator
AeTheReal
Profile Joined June 2009
United States108 Posts
October 14 2009 03:33 GMT
#19
I'm pretty sure that's supposed to count the number of blobs. Unfortunately, the code doesn't ever determine whether each location is part of a blob or not.
yenta
Profile Blog Joined April 2006
Poland1142 Posts
October 14 2009 03:35 GMT
#20
On October 14 2009 11:34 tossinYoSalad wrote:
Show nested quote +
On October 14 2009 11:29 yenta wrote:
On October 14 2009 11:16 tossinYoSalad wrote:
On October 14 2009 11:12 yenta wrote:
What are the INPUTs and OUTPUTs of this function supposed to be?


all it does is count the number of blobs and keep track of the recursion depth. numBlobs is incremented immediately after the while loop and recDepth is incremented at the end of the function.

i know its a horrible implementation believe me.


Rewrite it like this:

RECURSIVE CALLER
Initialize count
Initialize array of array with flags (w/e you want)
FOR( 0...x )
FOR( 0..y )
if point @ x,y is unchecked & x.y is a space, call recursive worker
//FOR
//FOR
return w/e

RECURSIVE WORKER ( root, current_x, current_y, pointer to 2D array, ref to count )
base case:
return if out of bounds
return if space you're looking has been marked 'looked at'
recursive case:
count adj spaces by calling self with: ( x-1,y ), ( x+1, y), (x, y+1), (x, y-1) + diagonals if they count.
if adj spaces > 0 & root == true, increment counter //endif
mark current space as read
END


thanks alot, what do you mean by root though? and because the 2d array is a class var i dont need to pass it though do i?

look at this:
http://pastebin.com/m7c4ab47d
Trutacz Practice Discord - https://discord.gg/PWF7Pv
1 2 3 Next All
Please log in or register to reply.
Live Events Refresh
Kung Fu Cup
12:00
2025 Monthly #3: Day 4
Cure vs Reynor
Classic vs herO
RotterdaM385
IndyStarCraft 179
IntoTheiNu 46
SteadfastSC36
Liquipedia
RSL Revival
10:00
Group C
SHIN vs ByuN
Crank 1192
Tasteless826
ComeBackTV 797
Rex125
3DClanTV 68
Liquipedia
CranKy Ducklings
10:00
Master Swan Open #98
CranKy Ducklings41
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Crank 1192
Tasteless 826
RotterdaM 385
IndyStarCraft 179
Reynor 138
Rex 125
SteadfastSC 36
Railgan 35
StarCraft: Brood War
Britney 34679
Rain 3837
Hyuk 1331
Horang2 1325
firebathero 1306
Jaedong 886
Shuttle 597
Mini 440
Stork 332
BeSt 259
[ Show more ]
Last 196
PianO 168
EffOrt 166
Leta 163
Hm[arnc] 156
Pusan 133
Shine 131
Hyun 87
Mong 86
sorry 67
Barracks 66
Shinee 63
ggaemo 52
JYJ47
JulyZerg 29
soO 27
Bale 19
Movie 18
Noble 13
HiyA 12
ajuk12(nOOB) 9
Dota 2
Gorgc4605
singsing2264
XaKoH 429
XcaliburYe243
Dendi126
Counter-Strike
fl0m3292
zeus396
Other Games
FrodaN4033
B2W.Neo1686
KnowMe207
Fuzer 163
Lowko108
Mew2King64
MindelVK11
Organizations
Dota 2
PGL Dota 2 - Main Stream7851
PGL Dota 2 - Secondary Stream1604
Other Games
gamesdonequick575
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 13 non-featured ]
StarCraft 2
• Berry_CruncH138
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• C_a_k_e 1006
League of Legends
• Stunt1248
Upcoming Events
IPSL
4h 43m
ZZZero vs rasowy
Napoleon vs KameZerg
OSC
6h 43m
BSL 21
7h 43m
Tarson vs Julia
Doodle vs OldBoy
eOnzErG vs WolFix
StRyKeR vs Aeternum
Sparkling Tuna Cup
21h 43m
RSL Revival
21h 43m
Reynor vs sOs
Maru vs Ryung
Kung Fu Cup
23h 43m
WardiTV Korean Royale
23h 43m
BSL 21
1d 7h
JDConan vs Semih
Dragon vs Dienmax
Tech vs NewOcean
TerrOr vs Artosis
IPSL
1d 7h
Dewalt vs WolFix
eOnzErG vs Bonyth
Replay Cast
1d 10h
[ Show More ]
Wardi Open
1d 23h
Monday Night Weeklies
2 days
WardiTV Korean Royale
2 days
BSL: GosuLeague
3 days
The PondCast
3 days
Replay Cast
4 days
RSL Revival
4 days
BSL: GosuLeague
5 days
RSL Revival
5 days
WardiTV Korean Royale
5 days
RSL Revival
6 days
WardiTV Korean Royale
6 days
Liquipedia Results

Completed

Proleague 2025-11-07
Stellar Fest: Constellation Cup
Eternal Conflict S1

Ongoing

C-Race Season 1
IPSL Winter 2025-26
KCM Race Survival 2025 Season 4
SOOP Univ League 2025
YSL S2
BSL Season 21
CSCL: Masked Kings S3
SLON Tour Season 2
RSL Revival: Season 3
META Madness #9
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025

Upcoming

BSL 21 Non-Korean Championship
Acropolis #4
IPSL Spring 2026
HSC XXVIII
RSL Offline Finals
WardiTV 2025
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter 2026: Closed Qualifier
eXTREMESLAND 2025
ESL Impact League Season 8
SL Budapest Major 2025
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2025 TLnet. All Rights Reserved.