• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 12:31
CEST 18:31
KST 01:31
  • 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
[ASL19] Finals Recap: Standing Tall9HomeStory Cup 27 - Info & Preview18Classic wins Code S Season 2 (2025)16Code S RO4 & Finals Preview: herO, Rogue, Classic, GuMiho0TL Team Map Contest #5: Presented by Monster Energy6
Community News
Weekly Cups (June 30 - July 6): Classic Doubles2[BSL20] Non-Korean Championship 4x BSL + 4x China8Flash Announces Hiatus From ASL66Weekly Cups (June 23-29): Reynor in world title form?14FEL Cracov 2025 (July 27) - $8000 live event22
StarCraft 2
General
The SCII GOAT: A statistical Evaluation The GOAT ranking of GOAT rankings Weekly Cups (June 23-29): Reynor in world title form? Weekly Cups (June 30 - July 6): Classic Doubles Program: SC2 / XSplit / OBS Scene Switcher
Tourneys
RSL: Revival, a new crowdfunded tournament series FEL Cracov 2025 (July 27) - $8000 live event Sparkling Tuna Cup - Weekly Open Tournament WardiTV Mondays Korean Starcraft League Week 77
Strategy
How did i lose this ZvP, whats the proper response Simple Questions Simple Answers
Custom Maps
[UMS] Zillion Zerglings
External Content
Mutation # 481 Fear and Lava Mutation # 480 Moths to the Flame Mutation # 479 Worn Out Welcome Mutation # 478 Instant Karma
Brood War
General
Flash Announces Hiatus From ASL SC uni coach streams logging into betting site BW General Discussion BGH Auto Balance -> http://bghmmr.eu/ ASL20 Preliminary Maps
Tourneys
[BSL20] Grand Finals - Sunday 20:00 CET CSL Xiamen International Invitational [BSL20] Non-Korean Championship 4x BSL + 4x China The Casual Games of the Week Thread
Strategy
Simple Questions, Simple Answers I am doing this better than progamers do.
Other Games
General Games
Nintendo Switch Thread Stormgate/Frost Giant Megathread Path of Exile What do you want from future RTS games? 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
Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread Vanilla Mini Mafia
Community
General
US Politics Mega-thread Russo-Ukrainian War Thread Stop Killing Games - European Citizens Initiative Summer Games Done Quick 2024! Summer Games Done Quick 2025!
Fan Clubs
SKT1 Classic Fan Club! Maru Fan Club
Media & Entertainment
Anime Discussion Thread [Manga] One Piece [\m/] Heavy Metal Thread
Sports
Formula 1 Discussion 2024 - 2025 Football Thread NBA General Discussion TeamLiquid Health and Fitness Initiative For 2023 NHL Playoffs 2024
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List
Blogs
Culture Clash in Video Games…
TrAiDoS
from making sc maps to makin…
Husyelt
Blog #2
tankgirl
StarCraft improvement
iopq
Trip to the Zoo
micronesia
Customize Sidebar...

Website Feedback

Closed Threads



Active: 636 users

The Big Programming Thread - Page 631

Forum Index > General Forum
Post a Reply
Prev 1 629 630 631 632 633 1031 Next
Thread Rules
1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution.
2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20)
3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible.
4. Use [code] tags to format code blocks.
solidbebe
Profile Blog Joined November 2010
Netherlands4921 Posts
Last Edited: 2015-05-13 21:05:32
May 13 2015 21:05 GMT
#12601
double post
That's the 2nd time in a week I've seen someone sig a quote from this GD and I have never witnessed a sig quote happen in my TL history ever before. -Najda
Ben...
Profile Joined January 2011
Canada3485 Posts
May 13 2015 21:12 GMT
#12602
On May 14 2015 05:49 killa_robot wrote:
You don't go to school to learn how to work though, you go to school to learn how to learn. One of the greatest disconnects we have right now is between schools and employers.

Yup. I've seen this topic discussed to death on a different forum and this is what it always comes down to. Is university supposed to be a place of learning or a place to prepare you for working? The business types seem to think it should prepare you for working in the field while the academic types feel it should teach you the underlying concepts and more of the "why's" than "hows" and that in knowing these concepts, you can be a more adaptable programmer since you understand things in general and can apply them well and learn quickly.
"Cliiiiiiiiiiiiiiiiide" -Tastosis
iaretehnoob
Profile Joined June 2004
Sweden741 Posts
May 13 2015 22:37 GMT
#12603
On May 14 2015 06:04 solidbebe wrote:
So Im very new to C++ and don't know anything about the language's intricacies yet. I've implemented this algorithm which computes the sum of values on the even levels of a tree. Given that I have to use lists (and not vectors or what have it), is this a good implementation?

+ Show Spoiler +

void visitTreeEvenLevels(std::list<Tree> treelist, int i, float& total){
if (treelist.size() == 0){
return;
}
std::list<Tree>::const_iterator ptr = treelist.begin();
std::list<Tree> cont;
for (; ptr != treelist.end(); ptr++){
std::list<Tree> temp = ptr->children;
std::list<Tree>::const_iterator ptr2 = temp.begin();
for (; ptr2 != temp.end(); ptr2++){
cont.push_front(*ptr2);
if (i % 2 == 0){
total += ptr2->value;
}
}
}
++i;
visitTreeEvenLevels(cont, i, total);
}


Just looking for criticism from experienced C/C++ people


Try changing your function signature to something like
void visitTreeEvenLevels(std::list<Tree>::const_iterator treelist, int i, float& total)

and doing it without creating temporary lists. Other than that it seems fine, but maybe clean up the naming a bit, stuff like ptr and ptr2 or using i for anything but a simple loop counter.
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
Last Edited: 2015-05-14 00:34:35
May 14 2015 00:09 GMT
#12604
On May 14 2015 07:37 iaretehnoob wrote:
Show nested quote +
On May 14 2015 06:04 solidbebe wrote:
So Im very new to C++ and don't know anything about the language's intricacies yet. I've implemented this algorithm which computes the sum of values on the even levels of a tree. Given that I have to use lists (and not vectors or what have it), is this a good implementation?

+ Show Spoiler +

void visitTreeEvenLevels(std::list<Tree> treelist, int i, float& total){
if (treelist.size() == 0){
return;
}
std::list<Tree>::const_iterator ptr = treelist.begin();
std::list<Tree> cont;
for (; ptr != treelist.end(); ptr++){
std::list<Tree> temp = ptr.children;
std::list<Tree>::const_iterator ptr2 = temp.begin();
for (; ptr2 != temp.end(); ptr2++){
cont.push_front(*ptr2);
if (i % 2 == 0){
total += ptr2->value;
}
}
}
++i;
visitTreeEvenLevels(cont, i, total);
}


Just looking for criticism from experienced C/C++ people


Try changing your function signature to something like
void visitTreeEvenLevels(std::list<Tree>::const_iterator treelist, int i, float& total)

and doing it without creating temporary lists. Other than that it seems fine, but maybe clean up the naming a bit, stuff like ptr and ptr2 or using i for anything but a simple loop counter.


I hope it works as I can't test it but here is some slight improvement:


void visitTreeEvenLevels(std::list<Tree> treelist, int& i, float& total)
{
if (treelist.empty()) {
return;
}

std::list<Tree> cont;

for (const Tree& parent : treelist)
{
for (const Tree& child : entry.children)
{
cont.push_front(child);
if (i % 2 == 0){
total += child.value;
}
}
}
++i;
visitTreeEvenLevels(cont, i, total);
}


1. empty() is better than size() == 0
2. for-each loop seems nicer if your compiler supports C++11
3. Edit: I've changed method's signature to be int& instead of int. You can either ++i directly or ++i before the recursive method, then pass i. Both should work. This should avoid unnecessary copies of 'i'. I guess the latter is more readable and doesn't rely on the reader to know that pre-increment returns a reference.

Edit: You can also avoid copies as suggested by iaretehnoob.
Cyx.
Profile Joined November 2010
Canada806 Posts
Last Edited: 2015-05-14 00:37:46
May 14 2015 00:36 GMT
#12605
On May 14 2015 06:04 solidbebe wrote:
So Im very new to C++ and don't know anything about the language's intricacies yet. I've implemented this algorithm which computes the sum of values on the even levels of a tree. Given that I have to use lists (and not vectors or what have it), is this a good implementation?

+ Show Spoiler +

void visitTreeEvenLevels(std::list<Tree> treelist, int i, float& total){
if (treelist.size() == 0){
return;
}
std::list<Tree>::const_iterator ptr = treelist.begin();
std::list<Tree> cont;
for (; ptr != treelist.end(); ptr++){
std::list<Tree> temp = ptr->children;
std::list<Tree>::const_iterator ptr2 = temp.begin();
for (; ptr2 != temp.end(); ptr2++){
cont.push_front(*ptr2);
if (i % 2 == 0){
total += ptr2->value;
}
}
}
++i;
visitTreeEvenLevels(cont, i, total);
}

Just looking for criticism from experienced C/C++ people


Looks okay, I would really prefer range-based for loops if you can use them:

for (auto ptr : treelist)


And
treelist.size() == 0
is called
treelist.empty()


I'm not sure about that 'i' you're using to track your depth, either - would it be maybe clearer/easier to just skip a level instead of checking which level you're on every time? Something along the lines of
//visitTreeEvenLevels(cont, i, total)
for (auto it : cont) {
visitTreeEvenLevels(it->children, total);
}


Dunno though.
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
May 14 2015 00:40 GMT
#12606
On May 14 2015 09:36 Cyx. wrote:
Show nested quote +
On May 14 2015 06:04 solidbebe wrote:
So Im very new to C++ and don't know anything about the language's intricacies yet. I've implemented this algorithm which computes the sum of values on the even levels of a tree. Given that I have to use lists (and not vectors or what have it), is this a good implementation?

+ Show Spoiler +

void visitTreeEvenLevels(std::list<Tree> treelist, int i, float& total){
if (treelist.size() == 0){
return;
}
std::list<Tree>::const_iterator ptr = treelist.begin();
std::list<Tree> cont;
for (; ptr != treelist.end(); ptr++){
std::list<Tree> temp = ptr->children;
std::list<Tree>::const_iterator ptr2 = temp.begin();
for (; ptr2 != temp.end(); ptr2++){
cont.push_front(*ptr2);
if (i % 2 == 0){
total += ptr2->value;
}
}
}
++i;
visitTreeEvenLevels(cont, i, total);
}

Just looking for criticism from experienced C/C++ people


Looks okay, I would really prefer range-based for loops if you can use them:

for (auto ptr : treelist)


And
treelist.size() == 0
is called
treelist.empty()


I'm not sure about that 'i' you're using to track your depth, either - would it be maybe clearer/easier to just skip a level instead of checking which level you're on every time? Something along the lines of
//visitTreeEvenLevels(cont, i, total)
for (auto it : cont) {
visitTreeEvenLevels(it->children, total);
}


Dunno though.


auto is kind of bad because it's not explicit, but you may also call the copy constructor. Try auto& instead of auto.
Khalum
Profile Joined September 2010
Austria831 Posts
May 14 2015 00:56 GMT
#12607
On May 14 2015 07:37 iaretehnoob wrote:
Show nested quote +
On May 14 2015 06:04 solidbebe wrote:
So Im very new to C++ and don't know anything about the language's intricacies yet. I've implemented this algorithm which computes the sum of values on the even levels of a tree. Given that I have to use lists (and not vectors or what have it), is this a good implementation?

+ Show Spoiler +

void visitTreeEvenLevels(std::list<Tree> treelist, int i, float& total){
if (treelist.size() == 0){
return;
}
std::list<Tree>::const_iterator ptr = treelist.begin();
std::list<Tree> cont;
for (; ptr != treelist.end(); ptr++){
std::list<Tree> temp = ptr->children;
std::list<Tree>::const_iterator ptr2 = temp.begin();
for (; ptr2 != temp.end(); ptr2++){
cont.push_front(*ptr2);
if (i % 2 == 0){
total += ptr2->value;
}
}
}
++i;
visitTreeEvenLevels(cont, i, total);
}


Just looking for criticism from experienced C/C++ people


Try changing your function signature to something like
void visitTreeEvenLevels(std::list<Tree>::const_iterator treelist, int i, float& total)

and doing it without creating temporary lists. Other than that it seems fine, but maybe clean up the naming a bit, stuff like ptr and ptr2 or using i for anything but a simple loop counter.


You can go one step further and pass the iterator as a reference:
void visitTreeEvenLevels(std::list<Tree>::const_iterator& treelist, int i, float& total)

RoyGBiv_13
Profile Blog Joined August 2010
United States1275 Posts
Last Edited: 2015-05-14 05:04:29
May 14 2015 05:00 GMT
#12608
On May 14 2015 09:40 darkness wrote:
Show nested quote +
On May 14 2015 09:36 Cyx. wrote:
On May 14 2015 06:04 solidbebe wrote:
So Im very new to C++ and don't know anything about the language's intricacies yet. I've implemented this algorithm which computes the sum of values on the even levels of a tree. Given that I have to use lists (and not vectors or what have it), is this a good implementation?

+ Show Spoiler +

void visitTreeEvenLevels(std::list<Tree> treelist, int i, float& total){
if (treelist.size() == 0){
return;
}
std::list<Tree>::const_iterator ptr = treelist.begin();
std::list<Tree> cont;
for (; ptr != treelist.end(); ptr++){
std::list<Tree> temp = ptr->children;
std::list<Tree>::const_iterator ptr2 = temp.begin();
for (; ptr2 != temp.end(); ptr2++){
cont.push_front(*ptr2);
if (i % 2 == 0){
total += ptr2->value;
}
}
}
++i;
visitTreeEvenLevels(cont, i, total);
}

Just looking for criticism from experienced C/C++ people


Looks okay, I would really prefer range-based for loops if you can use them:

for (auto ptr : treelist)


And
treelist.size() == 0
is called
treelist.empty()


I'm not sure about that 'i' you're using to track your depth, either - would it be maybe clearer/easier to just skip a level instead of checking which level you're on every time? Something along the lines of
//visitTreeEvenLevels(cont, i, total)
for (auto it : cont) {
visitTreeEvenLevels(it->children, total);
}


Dunno though.


auto is kind of bad because it's not explicit, but you may also call the copy constructor. Try auto& instead of auto.


auto isn't bad, though it is not explicit, which can be bad.

For the pros, It allows the compiler to take a lot of leniency on the data type when trying to optimize, it reduces work in refactoring types through multiple abstraction layers, and it still provides static type checking.

For the cons, it's not explicit and it even though you probably know the type at the time of you originally write the declaration of the variable, you may forget what it should be and misuse the variable later.

For all the benefits without the drawbacks, if you have a long function or a variable with complicated context, on the same line as the declaration or in the function comment block, mention the expected type and usage of those variables, even if you end up being wrong about the type the compiler uses. Later in the function, use assertions to force the type the what you expect, i.e, if "auto x" is an unsigned integer under MAX_X, Assert(x>0 && x < MAX_X);
+ Show Spoiler +
you ARE using assertions, right?


I personally disapprove of ever using auto for floats or doubles. If you're using a float or a double, you should be extra careful with that type.
Any sufficiently advanced technology is indistinguishable from magic
netherh
Profile Blog Joined November 2011
United Kingdom333 Posts
Last Edited: 2015-05-14 06:16:09
May 14 2015 06:09 GMT
#12609
On May 14 2015 14:00 RoyGBiv_13 wrote:
I personally disapprove of ever using auto for floats or doubles. If you're using a float or a double, you should be extra careful with that type.


Why?

I've been following the "almost always auto" advice for a while now, and the only time I've had any trouble is with a library that uses expression templates.
Mstring
Profile Joined September 2011
Australia510 Posts
May 14 2015 07:44 GMT
#12610
On May 14 2015 06:04 solidbebe wrote:
So Im very new to C++ and don't know anything about the language's intricacies yet. I've implemented this algorithm which computes the sum of values on the even levels of a tree. Given that I have to use lists (and not vectors or what have it), is this a good implementation?

+ Show Spoiler +

void visitTreeEvenLevels(std::list<Tree> treelist, int i, float& total){
if (treelist.size() == 0){
return;
}
std::list<Tree>::const_iterator ptr = treelist.begin();
std::list<Tree> cont;
for (; ptr != treelist.end(); ptr++){
std::list<Tree> temp = ptr->children;
std::list<Tree>::const_iterator ptr2 = temp.begin();
for (; ptr2 != temp.end(); ptr2++){
cont.push_front(*ptr2);
if (i % 2 == 0){
total += ptr2->value;
}
}
}
++i;
visitTreeEvenLevels(cont, i, total);
}


Just looking for criticism from experienced C/C++ people


Keep in mind exactly what happens when you say:

std::list<Tree> temp = ptr.children;

This will duplicate the entire children list, including all the children's children, and their children, etc.. Since you simply want to iterate over the whole thing and sum some values, what reason do you have for wanting to make a copy of anything? Your 'cont' list behaves similarly. Finally you're passing the list by value into the function, resulting in even more copies every time your function is called.

Passing references around makes avoiding this kind of thing easy:

void SumTreeEvenLevels(std::list<tree_node> &TreeList, int Level, float &Total)
{
for(std::list<tree_node>::iterator Node = TreeList.begin(); Node != TreeList.end(); Node++)
{
if(Level % 2 == 0)
Total += Node->Value;

SumTreeEventLevels(Node->Children, Level + 1, Total);
}
}
Blisse
Profile Blog Joined July 2010
Canada3710 Posts
Last Edited: 2015-05-14 08:19:36
May 14 2015 08:12 GMT
#12611
void SumTreeEvenLevels(
std::list<tree_node*>::const_iterator& current,
std::list<tree_node*>::const_iterator& end,
int level,
float& total
)
{
while (current != end)
{
if (level % 2)
{
total += (*current)->value;
}

SumTreeEvenLevels(
(*current)->children.begin(),
(*current)->children.end(),
level + 1,
total
);

++current;
}
}

float SumTreeEvenLevels(
tree_node& root
)
{
float value = root.value;
int level = 1;

SumTreeEvenLevels(
root.children.begin(),
root.children.end(),
level,
value
);

return value;
}


lalalala probably this would be satisfactory

someone should summon nunez
There is no one like you in the universe.
Khalum
Profile Joined September 2010
Austria831 Posts
Last Edited: 2015-05-14 09:29:21
May 14 2015 09:28 GMT
#12612
On May 14 2015 17:12 Blisse wrote:
[..]
if (level % 2)
[..]


Nope, that's true for all odd levels.


[edit] But you could start counting with 0...
netherh
Profile Blog Joined November 2011
United Kingdom333 Posts
Last Edited: 2015-05-14 10:40:53
May 14 2015 10:12 GMT
#12613
Old versions

+ Show Spoiler +



#include <vector>
#include <list>
#include <functional>

struct Tree
{
Tree(float value): Value(value) { }

float Value;

std::vector<Tree> Children;
};

float AccumulateEven(std::list<Tree> const& trees)
{
typedef std::function<void(Tree const& tree, float& result)> FunctionT;

auto even = FunctionT();
auto odd = FunctionT();

even = [&odd] (Tree const& tree, float& result)
{
result += tree.Value;

for (auto const& child : tree.Children)
odd(child, result);
};

odd = [&even] (Tree const& tree, float& result)
{
for (auto const& child : tree.Children)
even(child, result);
};

auto result = 0.f;

for (auto const& tree : trees)
even(tree, result);

return result;
}



Yay, lambdas!

EDIT: Actually, something like the following works quite nicely:


#include <vector>
#include <list>

struct Tree
{
Tree(float value): Value(value) { }

float Value;

std::vector<Tree> Children;
};

template<bool IsEven>
void AccumulateEven(Tree const& tree, float& result)
{
if (IsEven)
result += tree.Value;

for (auto const& child : tree.Children)
AccumulateEven<!IsEven>(child, result);
};

float AccumulateEven(std::list<Tree> const& trees)
{
auto result = 0.f;

for (auto const& tree : trees)
AccumulateEven<true>(tree, result);

return result;
}



I expect the if (IsEven) branch will get optimized out entirely for the AccumulateEven<false> instantiation.


EDIT: Wait... aren't we just doing this? (replace vector w/ list as appropriate):


#include <vector>

struct Node
{
Node(float value): Value(value) { }

float Value;

std::vector<Node> Children;
};

template<bool IsEven>
float AccumulateEven(Node const& node)
{
auto result = 0.f;

for (auto const& child : node.Children)
result += AccumulateEven<!IsEven>(child);

return IsEven ? result + node.Value : result;
};

Manit0u
Profile Blog Joined August 2004
Poland17243 Posts
Last Edited: 2015-05-14 11:15:03
May 14 2015 11:14 GMT
#12614
On May 14 2015 05:49 killa_robot wrote:
Show nested quote +
On May 13 2015 19:47 Manit0u wrote:
People who had Java during their CS courses, please tell me - did they teach you anything about stuff like Maven, Spring, Hibernate, JDBC? Because that are just few of the tools you'll be using out in the real world and knowing them well is pretty much a prerequisite for getting a job.


Nope. Used Hadoop, but apart from that didn't use any frameworks.

You don't go to school to learn how to work though, you go to school to learn how to learn. One of the greatest disconnects we have right now is between schools and employers.


Well, if you want to be an academic then I believe studying maths or philosophy would be more beneficial. CS should be pretty much purely technical (technical in learn how it's done in the real world sense) studies that prepare you for work in the industry.

Also, quite a large number of developers aren't even schooled in it. Here are some interesting resources:

http://stackoverflow.com/research/developer-survey-2015#profile-education
http://blog.codinghorror.com/why-cant-programmers-program/

There was also a great paper showing that people without CS education tend to be better programmers most of the time.
Time is precious. Waste it wisely.
spinesheath
Profile Blog Joined June 2009
Germany8679 Posts
Last Edited: 2015-05-14 11:48:14
May 14 2015 11:40 GMT
#12615
On May 14 2015 19:12 netherh wrote:
EDIT: Wait... aren't we just doing this? (replace vector w/ list as appropriate):


#include <vector>

struct Node
{
Node(float value): Value(value) { }

float Value;

std::vector<Node> Children;
};

template<bool IsEven>
float AccumulateEven(Node const& node)
{
auto result = 0.f;

for (auto const& child : node.Children)
result += AccumulateEven<!IsEven>(child);

return IsEven ? result + node.Value : result;
};


Templates, why are you so awesome?

On May 14 2015 20:14 Manit0u wrote:
There was also a great paper showing that people without CS education tend to be better programmers most of the time.

That sounds a lot like a newspaper headline that takes the result of the paper out of context.

Most likely the actual result is that self taught programmers are better than people who just did the mandatory stuff for their CS education. CS education doesn't make you a good programmer (at all), but if you are passionate about programming, you will be good regardless of having a CS education or not.
If you have a good reason to disagree with the above, please tell me. Thank you.
iaretehnoob
Profile Joined June 2004
Sweden741 Posts
May 14 2015 12:49 GMT
#12616
On May 14 2015 09:56 Khalum wrote:
Show nested quote +
On May 14 2015 07:37 iaretehnoob wrote:
On May 14 2015 06:04 solidbebe wrote:
So Im very new to C++ and don't know anything about the language's intricacies yet. I've implemented this algorithm which computes the sum of values on the even levels of a tree. Given that I have to use lists (and not vectors or what have it), is this a good implementation?

+ Show Spoiler +

void visitTreeEvenLevels(std::list<Tree> treelist, int i, float& total){
if (treelist.size() == 0){
return;
}
std::list<Tree>::const_iterator ptr = treelist.begin();
std::list<Tree> cont;
for (; ptr != treelist.end(); ptr++){
std::list<Tree> temp = ptr->children;
std::list<Tree>::const_iterator ptr2 = temp.begin();
for (; ptr2 != temp.end(); ptr2++){
cont.push_front(*ptr2);
if (i % 2 == 0){
total += ptr2->value;
}
}
}
++i;
visitTreeEvenLevels(cont, i, total);
}


Just looking for criticism from experienced C/C++ people


Try changing your function signature to something like
void visitTreeEvenLevels(std::list<Tree>::const_iterator treelist, int i, float& total)

and doing it without creating temporary lists. Other than that it seems fine, but maybe clean up the naming a bit, stuff like ptr and ptr2 or using i for anything but a simple loop counter.


You can go one step further and pass the iterator as a reference:
void visitTreeEvenLevels(std::list<Tree>::const_iterator& treelist, int i, float& total)


I think iterators are usually passed by value, which makes a lot of sense when you think of them as "fancy pointers".


On May 14 2015 19:12 netherh wrote:
EDIT: Wait... aren't we just doing this? (replace vector w/ list as appropriate):


#include <vector>

struct Node
{
Node(float value): Value(value) { }

float Value;

std::vector<Node> Children;
};

template<bool IsEven>
float AccumulateEven(Node const& node)
{
auto result = 0.f;

for (auto const& child : node.Children)
result += AccumulateEven<!IsEven>(child);

return IsEven ? result + node.Value : result;
};


Nice.
Prillan
Profile Joined August 2011
Sweden350 Posts
May 14 2015 15:17 GMT
#12617
On May 13 2015 22:30 Ropid wrote:
There's also the complete opposite, you know. I actually regret that the academic side of "computer science" wasn't there enough.

I've looked at this here today: http://ncatlab.org/nlab/show/polynomial functor

It is related to object-oriented programming. I don't understand.

You need to give room for something like this as well, or you won't find people that want to work on it in depth. Also, if people can't even vaguely get what the stuff is about, they won't really know if this is actually an interesting topic for them or not. You need to show the basics that would enable people to start approaching those kinds of topics. I felt that was missing.

Don't expect to understand anything written on ncatlab. I've studied mathematics for 4 years, with interest in algebra and category theory, and I still can't make sense of most of the stuff posted there.
TheBB's sidekick, aligulac.com | "Reality is frequently inaccurate." - Douglas Adams
Manit0u
Profile Blog Joined August 2004
Poland17243 Posts
May 14 2015 16:34 GMT
#12618
So, I've decided to learn me some Java. Decided to start out with something simple, like the FizzBuzz test that some people use during job interviews.


class FizzBuzz {
public static void main(String[] args) {
int i = 0;
boolean isDivisibleByThree = false;
boolean isDivisibleByFive = false;
String msg = "";

while (i++ < 100) {
isDivisibleByThree = ((i % 3) == 0);
isDivisibleByFive = ((i % 5) == 0);

if (isDivisibleByThree && isDivisibleByFive) {
msg = "FizzBuzz";
} else if (isDivisibleByThree) {
msg = "Fizz";
} else if (isDivisibleByFive) {
msg = "Buzz";
} else {
msg = Integer.toString(i);
}

System.out.println(msg);
}
}
}


Could someone point me into right direction so that I can move away from the imperative thinking and towards full OOP and the "Java-way" (java specific of course, I know a fair bit about OOP but very little about Java)? How would you write this simple app in the way that Java should be written?
Time is precious. Waste it wisely.
spinesheath
Profile Blog Joined June 2009
Germany8679 Posts
May 14 2015 17:01 GMT
#12619
If you're asking how to over-engineer this program, there is Enterprise FizzBuzz. There is no point in adding classes for a program as simple as this one.

Personally, I'd refactor your code to something like this, but that's hardly Java specific:

class FizzBuzz {
public static void main(String[] args) {
for (int = 0; i < 100; ++i) {
String msg = GetMessage(i);
System.out.println(msg);
}
}

private static String GetMessage(int i)
{
boolean isDivisibleByThree = ((i % 3) == 0);
boolean isDivisibleByFive = ((i % 5) == 0);

if (isDivisibleByThree && isDivisibleByFive) {
return "FizzBuzz";
} else if (isDivisibleByThree) {
return "Fizz";
} else if (isDivisibleByFive) {
return "Buzz";
} else {
return Integer.toString(i);
}
}
}


You shouldn't declare your variables at the top of the method in Java, declare them as close to their usage as possible instead; this is not C. Ideally you avoid initializing a variable to a dummy value by joining the declaration and first usage. Don't increment and compare in a while loop condition, do one thing at a time.
If you have a good reason to disagree with the above, please tell me. Thank you.
Ben...
Profile Joined January 2011
Canada3485 Posts
May 14 2015 19:11 GMT
#12620
On May 14 2015 20:14 Manit0u wrote:
Well, if you want to be an academic then I believe studying maths or philosophy would be more beneficial. CS should be pretty much purely technical (technical in learn how it's done in the real world sense) studies that prepare you for work in the industry.

There's technical/vocational schools for the purely technical side of things.

How university CS is right now makes sense. It's more in line with most other scientific university degree programs. You learn the underlying concepts and apply them in projects and assignments (just like how in physics or chemistry you learn things in lecture and apply them in labs). There seems to be a contingent of people who think that CS is purely software engineering and programming, and that is definitely not the case. Software engineering is just a small part of CS. You can go through an entire CS degree without ever stepping foot in a software engineering class (outside of the intro stuff you likely run into in second year). Likewise, there are CS classes where you don't program at all. From a CS perspective, programming is just a means of exploring and applying concepts. The end goal when programming during a CS degree is to show that you can apply the knowledge you learned, not produce some thing that you have to sell and maintain.

The whole "employers wanting people that know a lot of the theoretical concepts in CS but also all the latest and greatest tools" issue isn't with universities or technical schools, it's an issue with the employers. They want it all while not being willing to put up the resources to get it all. They want both someone who is both well versed from a theoretical perspective, but also somehow magically knows all of the particular technologies the company is using so they don't have to train them. They look to fresh graduates and expect them to know specific technologies that are not really something that are useful outside of a business environment.
"Cliiiiiiiiiiiiiiiiide" -Tastosis
Prev 1 629 630 631 632 633 1031 Next
Please log in or register to reply.
Live Events Refresh
RotterdaM Event
16:00
Rotti Stream Rumble 4k Edition
RotterdaM443
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
mouzHeroMarine 762
RotterdaM 443
Hui .338
MaxPax 330
StarCraft: Brood War
Bisu 1963
EffOrt 1327
Flash 1236
Jaedong 1157
Hyuk 792
Stork 374
actioN 369
Soma 289
Soulkey 243
firebathero 173
[ Show more ]
Snow 171
Mind 99
JulyZerg 66
sSak 64
Barracks 60
TY 58
Sharp 48
Terrorterran 45
PianO 43
JYJ43
Rock 32
Yoon 24
HiyA 21
yabsab 17
Aegong 17
soO 16
GoRush 9
Shine 8
IntoTheRainbow 7
Dota 2
Gorgc6615
qojqva3321
League of Legends
singsing2243
Dendi1253
Counter-Strike
fl0m1160
markeloff169
Super Smash Bros
Mew2King257
Other Games
hiko1581
Beastyqt844
ceh9337
Lowko301
crisheroes248
ArmadaUGS149
KnowMe138
Trikslyr62
PGG 1
Organizations
Other Games
gamesdonequick47269
StarCraft 2
angryscii 21
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 16 non-featured ]
StarCraft 2
• Reevou 6
• intothetv
• AfreecaTV YouTube
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• Michael_bg 2
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Nemesis6915
• Jankos912
• TFBlade845
Other Games
• Shiphtur333
Upcoming Events
Replay Cast
7h 29m
Sparkling Tuna Cup
17h 29m
WardiTV European League
23h 29m
MaNa vs sebesdes
Mixu vs Fjant
ByuN vs HeRoMaRinE
ShoWTimE vs goblin
Gerald vs Babymarine
Krystianer vs YoungYakov
PiGosaur Monday
1d 7h
The PondCast
1d 17h
WardiTV European League
1d 19h
Jumy vs NightPhoenix
Percival vs Nicoract
ArT vs HiGhDrA
MaxPax vs Harstem
Scarlett vs Shameless
SKillous vs uThermal
uThermal 2v2 Circuit
1d 23h
Replay Cast
2 days
RSL Revival
2 days
ByuN vs SHIN
Clem vs Reynor
Replay Cast
3 days
[ Show More ]
RSL Revival
3 days
Classic vs Cure
FEL
3 days
RSL Revival
4 days
FEL
4 days
FEL
4 days
BSL20 Non-Korean Champi…
5 days
Bonyth vs QiaoGege
Dewalt vs Fengzi
Hawk vs Zhanhun
Sziky vs Mihu
Mihu vs QiaoGege
Zhanhun vs Sziky
Fengzi vs Hawk
Sparkling Tuna Cup
5 days
RSL Revival
5 days
FEL
5 days
BSL20 Non-Korean Champi…
6 days
Bonyth vs Dewalt
QiaoGege vs Dewalt
Hawk vs Bonyth
Sziky vs Fengzi
Mihu vs Zhanhun
QiaoGege vs Zhanhun
Fengzi vs Mihu
Liquipedia Results

Completed

BSL Season 20
HSC XXVII
Heroes 10 EU

Ongoing

JPL Season 2
BSL 2v2 Season 3
Acropolis #3
KCM Race Survival 2025 Season 2
CSL 17: 2025 SUMMER
Copa Latinoamericana 4
Jiahua Invitational
Championship of Russia 2025
RSL Revival: Season 1
Murky Cup #2
BLAST.tv Austin Major 2025
ESL Impact League Season 7
IEM Dallas 2025
PGL Astana 2025
Asian Champions League '25
BLAST Rivals Spring 2025
MESA Nomadic Masters
CCT Season 2 Global Finals
IEM Melbourne 2025

Upcoming

2025 ACS Season 2: Qualifier
CSLPRO Last Chance 2025
CSL Xiamen Invitational
2025 ACS Season 2
CSLPRO Chat StarLAN 3
K-Championship
uThermal 2v2 Main Event
SEL Season 2 Championship
FEL Cracov 2025
Esports World Cup 2025
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
IEM Cologne 2025
FISSURE Playground #1
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.