• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 14:12
CEST 20:12
KST 03:12
  • 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
Tournament Spotlight: FEL Cracow 20259Power Rank - Esports World Cup 202577RSL Season 1 - Final Week9[ASL19] Finals Recap: Standing Tall15HomeStory Cup 27 - Info & Preview18
Community News
Google Play ASL (Season 20) Announced25BSL Team Wars - Bonyth, Dewalt, Hawk & Sziky teams10Weekly Cups (July 14-20): Final Check-up0Esports World Cup 2025 - Brackets Revealed19Weekly Cups (July 7-13): Classic continues to roll8
StarCraft 2
General
#1: Maru - Greatest Players of All Time Tournament Spotlight: FEL Cracow 2025 Power Rank - Esports World Cup 2025 I offer completely free coaching services What tournaments are world championships?
Tourneys
FEL Cracov 2025 (July 27) - $10,000 live event Esports World Cup 2025 $25,000 Streamerzone StarCraft Pro Series announced $5,000 WardiTV Summer Championship 2025 WardiTV Mondays
Strategy
How did i lose this ZvP, whats the proper response
Custom Maps
External Content
Mutation #239 Bad Weather Mutation # 483 Kill Bot Wars Mutation # 482 Wheel of Misfortune Mutation # 481 Fear and Lava
Brood War
General
Google Play ASL (Season 20) Announced [Update] ShieldBattery: 2025 Redesign Dewalt's Show Matches in China BGH Auto Balance -> http://bghmmr.eu/ BW General Discussion
Tourneys
[Megathread] Daily Proleagues [BSL20] Non-Korean Championship 4x BSL + 4x China CSL Xiamen International Invitational [CSLPRO] It's CSLAN Season! - Last Chance
Strategy
Simple Questions, Simple Answers [G] Mineral Boosting Does 1 second matter in StarCraft?
Other Games
General Games
Stormgate/Frost Giant Megathread Nintendo Switch Thread Total Annihilation Server - TAForever [MMORPG] Tree of Savior (Successor of Ragnarok) Path of Exile
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
Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread Vanilla Mini Mafia
Community
General
US Politics Mega-thread UK Politics Mega-thread Stop Killing Games - European Citizens Initiative Things Aren’t Peaceful in Palestine Russo-Ukrainian War Thread
Fan Clubs
INnoVation Fan Club SKT1 Classic Fan Club!
Media & Entertainment
Anime Discussion Thread [\m/] Heavy Metal Thread Movie Discussion! [Manga] One Piece Korean Music Discussion
Sports
Formula 1 Discussion 2024 - 2025 Football Thread TeamLiquid Health and Fitness Initiative For 2023 NBA General Discussion
World Cup 2022
Tech Support
Installation of Windows 10 suck at "just a moment" Computer Build, Upgrade & Buying Resource Thread
TL Community
TeamLiquid Team Shirt On Sale The Automated Ban List
Blogs
Ping To Win? Pings And Their…
TrAiDoS
momentary artworks from des…
tankgirl
from making sc maps to makin…
Husyelt
StarCraft improvement
iopq
Socialism Anyone?
GreenHorizons
Eight Anniversary as a TL…
Mizenhauer
Customize Sidebar...

Website Feedback

Closed Threads



Active: 746 users

c++ help? - Page 2

Blogs > tossinYoSalad
Post a Reply
Prev 1 2 3 Next All
prOxi.swAMi
Profile Blog Joined November 2004
Australia3091 Posts
October 14 2009 03:48 GMT
#21
Wow, that's messy :S needs mass refactoring.
A tip for writing procedurally complex code like that is to employ comment-first coding which allows you to understand exactly what it is you need to do before you even write it. This code was obviously written "as you went along".

Start fresh, write a bunch of pseudocode in comments, then all you should have to do is fill in the logic under the comments.
Oh no
tossinYoSalad
Profile Blog Joined May 2009
United States215 Posts
October 14 2009 03:53 GMT
#22
On October 14 2009 12:48 prOxi.swAMi wrote:
Wow, that's messy :S needs mass refactoring.
A tip for writing procedurally complex code like that is to employ comment-first coding which allows you to understand exactly what it is you need to do before you even write it. This code was obviously written "as you went along".

Start fresh, write a bunch of pseudocode in comments, then all you should have to do is fill in the logic under the comments.


uhh were you looking at the most recent version? cuz its very simple now lol. and.. this is due in 10 mins so i dont really have time to do it. its logically correct, ive had 4 people look at it and agree, but for some reason its endlessly looping between 4 method calls and.. well idk. ill just submit it as is and hope for the best. oddly enough correctness is NOT a huge part of our project grades.

thanks for help everyone, especially yenta.
SonuvBob
Profile Blog Joined October 2006
Aiur21549 Posts
Last Edited: 2009-10-14 03:59:16
October 14 2009 03:56 GMT
#23
Did you move gridCopy[curr_x][curr_y] = 0; to before the recursive calls?

On October 14 2009 12:53 tossinYoSalad wrote:
oddly enough correctness is NOT a huge part of our project grades.

Yeah, I'd take clean code that doesn't quite work over that mess you started with any day. Adding anything to that would be a nightmare, but fixing the newer version should be easy.
Administrator
tossinYoSalad
Profile Blog Joined May 2009
United States215 Posts
Last Edited: 2009-10-14 04:00:06
October 14 2009 03:58 GMT
#24
On October 14 2009 12:56 SonuvBob wrote:
Did you move gridCopy[curr_x][curr_y] = 0; to before the recursive calls?


yes i did



edit: haha well thats what i get when i wait till last day to do programming assignments. this is like the 10th class ive taken involving programming and this is the only assignment ive EVER had problems with.
yenta
Profile Blog Joined April 2006
Poland1142 Posts
October 14 2009 04:00 GMT
#25
On October 14 2009 12:35 yenta wrote:
Show nested quote +
On October 14 2009 11:34 tossinYoSalad wrote:
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


quoting myself incase you missed the solution i posted :p
Trutacz Practice Discord - https://discord.gg/PWF7Pv
tossinYoSalad
Profile Blog Joined May 2009
United States215 Posts
October 14 2009 04:07 GMT
#26
On October 14 2009 13:00 yenta wrote:
Show nested quote +
On October 14 2009 12:35 yenta wrote:
On October 14 2009 11:34 tossinYoSalad wrote:
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


quoting myself incase you missed the solution i posted :p



holy shit i didnt see that before lol. thanks man but its too late now. you can see my final solution at http://pastebin.com/d3918f10e.

i modified your solution slightly to simply have every call of processWork mark off an entire blob and then every iteration of the loop in processGrid increment the number of blobs.
ph33r
Profile Joined June 2007
Canada58 Posts
October 14 2009 04:10 GMT
#27
line 13:
you have an assignment instead of condition
tossinYoSalad
Profile Blog Joined May 2009
United States215 Posts
October 14 2009 04:14 GMT
#28
[image loading]


hey if anyone is curious.. i fixed it, 10 minutes after project was due. sonovbob found the broken code like 10 posts back i just missed it lol. it was the comparison == , and i was using =. fucking stupid c++ compiler should catch that lol.

anyway... thats what it does.
AeTheReal
Profile Joined June 2009
United States108 Posts
October 14 2009 05:16 GMT
#29
On October 14 2009 13:14 tossinYoSalad wrote:
sonovbob found the broken code like 10 posts back i just missed it lol.

... did he now?
Slithe
Profile Blog Joined February 2007
United States985 Posts
October 14 2009 05:19 GMT
#30
The compiler's not going to catch it because it's a legal operation, not an error. When you do an assignment, the assigned variable is the return value.
imDerek
Profile Blog Joined August 2007
United States1944 Posts
Last Edited: 2009-10-14 06:06:30
October 14 2009 06:00 GMT
#31
Least favorite progamers: Leta, Zero, Mind, Shine, free, really <-- newly added
GogoKodo
Profile Blog Joined April 2003
Canada1785 Posts
October 14 2009 06:07 GMT
#32
On October 14 2009 13:14 tossinYoSalad wrote:

hey if anyone is curious.. i fixed it, 10 minutes after project was due. sonovbob found the broken code like 10 posts back i just missed it lol. it was the comparison == , and i was using =. fucking stupid c++ compiler should catch that lol.

anyway... thats what it does.

It's not a compile error so it shouldn't catch that. Doing assignment within a conditional is a pretty normal thing to do.
If you want some help from the compiler I believe there are certain warning flags you can set that will give a warning if you do not parenthesize the assignment within the conditional.

if( something = newSomething)
...
Could be set up to give you a compiler warning which would then go away if it was changed to
if( (something = newSomething) )
...

Can't remember the exact flag you need but I'm sure you can google it easily enough.
twitter: @terrancem
tossinYoSalad
Profile Blog Joined May 2009
United States215 Posts
Last Edited: 2009-10-14 06:32:55
October 14 2009 06:31 GMT
#33
On October 14 2009 14:16 AeTheReal wrote:
Show nested quote +
On October 14 2009 13:14 tossinYoSalad wrote:
sonovbob found the broken code like 10 posts back i just missed it lol.

... did he now?



yes.. yes.. he did.


as to the assignment within a conditional, yeah i know its a legal operation i was just mad that i didnt catch it lol. I normally don't mess that up so I don't think to check it.

also, when would you need to do an assignment within a conditional? i cant really find a use for that..
SonuvBob
Profile Blog Joined October 2006
Aiur21549 Posts
October 14 2009 06:32 GMT
#34
On October 14 2009 14:16 AeTheReal wrote:
Show nested quote +
On October 14 2009 13:14 tossinYoSalad wrote:
sonovbob found the broken code like 10 posts back i just missed it lol.

... did he now?

I must've used my admin powers to post it as someone else.
Administrator
EtherealDeath
Profile Blog Joined July 2007
United States8366 Posts
October 14 2009 06:40 GMT
#35
On October 14 2009 13:14 tossinYoSalad wrote:
[image loading]


hey if anyone is curious.. i fixed it, 10 minutes after project was due. sonovbob found the broken code like 10 posts back i just missed it lol. it was the comparison == , and i was using =. fucking stupid c++ compiler should catch that lol.

anyway... thats what it does.



Recursion depth 534 o.O
Didn't read the code, though that seems like a lot of recursion. Prolly would've done it by recursively calling hasBlobProperty() and having the outer layer go through every array position that hasn't already been marked as having a part of a blob there, and the second layer would keep track of the # of blobs. But apparently your method works so it's all good =)
Amnesia
Profile Blog Joined September 2009
United States3818 Posts
October 14 2009 06:41 GMT
#36
Jesus christ, you guys are so awesome. I wish I knew a programming language. Just looking at the way you talk makes my head hurt.

Is C++ hard to learn?
EtherealDeath
Profile Blog Joined July 2007
United States8366 Posts
Last Edited: 2009-10-14 06:45:47
October 14 2009 06:43 GMT
#37
nah C++ is pretty easy, although nowadays I suggest you learn Java or C#

EDIT: Although, if I recall correctly C# can execute up to 40 times slower than C++, and the JVM means Java is slower as well (but you would learn the details yourself in a Java course). Of course, efficiency usually won't matter too much... though I think most games are not made with Java/C# (WoW for instance is written in C++), but you would use a lot of C# and Java in other programming jobs.
GogoKodo
Profile Blog Joined April 2003
Canada1785 Posts
October 14 2009 06:52 GMT
#38
On October 14 2009 15:31 tossinYoSalad wrote:
Show nested quote +
On October 14 2009 14:16 AeTheReal wrote:
On October 14 2009 13:14 tossinYoSalad wrote:
sonovbob found the broken code like 10 posts back i just missed it lol.

... did he now?



yes.. yes.. he did.


as to the assignment within a conditional, yeah i know its a legal operation i was just mad that i didnt catch it lol. I normally don't mess that up so I don't think to check it.

also, when would you need to do an assignment within a conditional? i cant really find a use for that..


I don't think it's ever a necessity (could be wrong), it's just a nice convenience for a lot of things.
twitter: @terrancem
Disregard
Profile Blog Joined March 2007
China10252 Posts
Last Edited: 2009-10-14 07:00:27
October 14 2009 06:59 GMT
#39
Damn, this looks a lot harder than Java. Does C++ use the same operators?
"If I had to take a drug in order to be free, I'm screwed. Freedom exists in the mind, otherwise it doesn't exist."
Slithe
Profile Blog Joined February 2007
United States985 Posts
October 14 2009 07:14 GMT
#40
The issue with c++ is that there are a lot of nuances to it that gives it a steeper learning curve. As a result, one is a lot more error-prone when writing c++ code.

Regarding the assignment inside a conditional, here's random example code where it might be used:

char[] str = "Hello TeamLiquid!"
char c;
for (int i = 0; (c=str[i]) != 0; i++) {
printf("Letter: %c", c);
}

From a stylistic point of view, I don't think it's usually a particularly good idea, but it would be inconsistent to treat the assignment differently just because it was placed inside a conditional statement.
Prev 1 2 3 Next All
Please log in or register to reply.
Live Events Refresh
BSL20 Non-Korean Champi…
18:00
RO8 Round Robin Group - Day 4
Bonyth vs Zhanhun
Dewalt vs Mihu
Hawk vs Sziky
Sziky vs QiaoGege
Mihu vs Hawk
Zhanhun vs Dewalt
Fengzi vs Bonyth
ZZZero.O57
LiquipediaDiscussion
FEL
09:00
Cracow 2025
Clem vs ReynorLIVE!
RotterdaM2779
ComeBackTV 2289
IndyStarCraft 720
WardiTV509
CranKy Ducklings211
3DClanTV 183
Rex137
EnkiAlexander 90
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
RotterdaM 2779
IndyStarCraft 720
Rex 137
Vindicta 52
MindelVK 5
StarCraft: Brood War
Britney 31867
Larva 684
Nal_rA 627
Shine 339
firebathero 256
BeSt 195
Soulkey 135
Dewaltoss 80
Hyun 65
ZZZero.O 57
[ Show more ]
yabsab 36
sSak 31
Free 27
Terrorterran 6
IntoTheRainbow 5
Dota 2
Gorgc6602
qojqva4114
Counter-Strike
fl0m3904
flusha136
Super Smash Bros
hungrybox422
Mew2King180
Heroes of the Storm
Khaldor653
Liquid`Hasu449
Other Games
B2W.Neo706
Hui .198
KnowMe152
QueenE94
Organizations
Other Games
gamesdonequick2218
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 20 non-featured ]
StarCraft 2
• StrangeGG 60
• Adnapsc2 14
• Legendk 7
• iHatsuTV 7
• LaughNgamezSOOP
• sooper7s
• AfreecaTV YouTube
• Migwel
• intothetv
• Kozan
• IndyKCrew
StarCraft: Brood War
• Azhi_Dahaki13
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• Nemesis2288
• WagamamaTV711
League of Legends
• Jankos1905
Other Games
• imaqtpie916
• Shiphtur160
Upcoming Events
Wardi Open
16h 48m
Sparkling Tuna Cup
1d 15h
WardiTV European League
1d 21h
Online Event
1d 23h
uThermal 2v2 Circuit
2 days
The PondCast
3 days
Replay Cast
4 days
Korean StarCraft League
5 days
CranKy Ducklings
5 days
Sparkling Tuna Cup
6 days
Liquipedia Results

Completed

CSLPRO Last Chance 2025
Esports World Cup 2025
Murky Cup #2

Ongoing

Copa Latinoamericana 4
Jiahua Invitational
BSL 20 Non-Korean Championship
BSL 20 Team Wars
FEL Cracov 2025
CC Div. A S7
Underdog Cup #2
IEM Cologne 2025
FISSURE Playground #1
BLAST.tv Austin Major 2025
ESL Impact League Season 7
IEM Dallas 2025
PGL Astana 2025
Asian Champions League '25

Upcoming

ASL Season 20: Qualifier #1
ASL Season 20: Qualifier #2
ASL Season 20
CSLPRO Chat StarLAN 3
BSL Season 21
RSL Revival: Season 2
Maestros of the Game
SEL Season 2 Championship
WardiTV Summer 2025
uThermal 2v2 Main Event
HCC Europe
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual
Esports World Cup 2025
BLAST Bounty Fall 2025
BLAST Bounty Fall Qual
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.