d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Make Me A Better Programmer - From Step 1
Prev1121314151656Next
Add Reply New Topic New Poll
Member
Posts: 4,541
Joined: Sep 15 2011
Gold: 10,391.00
Nov 18 2012 05:06pm
spend less time coding, and spend more time working things out on pen and paper. once you start thinking about your assignments as problems to be solved rather than code to be written, then you'll be in a better shape. the problem you're given is more like a math word problem than it is a coding problem -- the only difference is that instead of doing arithmetic and calculus, you're working with data structures.

especially at a beginner's level (but it's really true at all levels) -- the actual coding is a distraction from really just solving the problem. the sooner you can approach these problems in an abstract, language-agnostic way, the easier time you'll have in like... all of computer science.

This post was edited by irimi on Nov 18 2012 05:10pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 18 2012 05:13pm
yeah.....Right now I am having a conflict between what he 'asked' and what I 'THINK' he asked

again, his wording:

Quote
Your fourth project is to write a program to build and display a family tree. I want you to use a class to hold the needed data for each person and pointers to the people associated
with each person [wife and children].


The input for the people consists of a name (with a trailing distinguishing number; treat the number as a part of the name), a birthdate, a marital status (either ‘M’ or ‘S’), a wife name
and birthdate if the marital status is ‘M’, a variable number of children names, a possible death date (which will start with a digit, so you can distinguish it from a child name), and
a trailing ‘X’.

ex:

Steve1 12Nov1931 M Mary1 17Dec1833 Steve2 Harry1 Sue1 3Jan1959 X
Steve2 31Dec1957 S Xavier1 X


After the people, there will be the word ‘QUIT’ and a series of names (possibly a spouse name). For each name, I want you to print that person, in parentheses his/her birthdate),
if married the string ” x ” (with the spaces) his/her spouse and their birthdate, and on succeeding indented lines the children and their birthdates (one per line).

The basic structure is that of a tree; I want it done with classes that you write.



like even with the approach of IM GONNA DO THIS ON PEN N PAPER....I still am not quite sure why we need pointers and why he says the basic structure is that of a tree (we just finished a binary tree project and this is not similar at all IMO) and the biggest problem is figuring out how to deal with the inputs!!!
Member
Posts: 4,541
Joined: Sep 15 2011
Gold: 10,391.00
Nov 18 2012 05:18pm
there's nothing wrong or unclear about what he wrote though.

here's a hint: just grab a piece of pen and paper and draw out the representation of what he's asking you to do, reading the quoted above from top to bottom. forget about the fact that you're doing this in C++. just think, what would this conceptually look like on paper?

This post was edited by irimi on Nov 18 2012 05:19pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 18 2012 05:29pm


I think?
Member
Posts: 4,541
Joined: Sep 15 2011
Gold: 10,391.00
Nov 18 2012 05:30pm
alright, now for the million dollar question -- are those all the same class?
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 18 2012 05:34pm
Quote (irimi @ Nov 18 2012 06:30pm)
alright, now for the million dollar question -- are those all the same class?


yes. In general the class with have the fields:


// this is a linked list of type person, needed for the class I think


struct kids {
person *child
person *next
}

class person{

string name
string birthdate
char Marital_status
person *spouse
kids *children
string deathdate;
}

I think this is the best way to do it?

This post was edited by Eep on Nov 18 2012 05:34pm
Member
Posts: 4,541
Joined: Sep 15 2011
Gold: 10,391.00
Nov 18 2012 05:37pm
Quote (Eep @ Nov 18 2012 04:13pm)
I still am not quite sure why we need pointers and why he says the basic structure is that of a tree (we just finished a binary tree project and this is not similar at all IMO)


so do you know why now?
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 18 2012 05:42pm
Quote (irimi @ Nov 18 2012 06:37pm)
so do you know why now?


I mean yeah but one particular thing I was having issue with

like here is some pseudo shit I was writing down:

Quote
idea:

Create object

while (cin >> data):

head points to the created object:

is name empty? if so, fill it
is dob empty? if so, fill it
is marital empty? if so, fill it
if marital is S and the next input is a string that begins with a letter, then person->children = new(kids) and  if person->children.name is empty, fill it
if marital is S and the next input is a string that begins with an int, then if person.dod is empty, fill it.
if marital is M and person->spoouse == NULL,







something along those lines. I don't know how to....allocate memory for a new struct called children, then allocate memory for the childrens object called person, then access that objects data fields and set the data. It seems incredibly arduous to do all of this, I feel like I am making a misstep somewhere.

On top of that, I still have to find a way to search through this madness and I still have to deal with the weird inputs (as you can see above).
Member
Posts: 4,541
Joined: Sep 15 2011
Gold: 10,391.00
Nov 18 2012 06:18pm
if i'm not mistaken, you can just allocate memory for shit in C++ by calling "new".

also, it's not actually that arduous to do all those things if you do it intelligently.

something like

Code
steve1 = new Person("Steve1")
mary = new Person("Mary")
steve2 = new Person("Steve2")
steve.setspouse(mary)
steve.addchild(steve2)


the only part that looks remotely like a challenge is figuring out how to get to one of those people (i.e. you're done adding steve1, his spouse and his kids, now you need to setup steve2, his spouse and his kids). for that you need to either do some tree traversal, or if you want to cheat, just maintain a global list of pointers to all the people in existence, and use that to access the people directly.

start by writing out what that might look like in the absence of methods and functions. then start observing patterns and repeated lines of code that you could extract into a function and/or a loop (or recursion).

but once again, remember that the purpose of all these assignments isn't really about getting The Right Answer (there's no single Right Answer anyway). it's about developing your thought process to get to *an answer*. so if you're getting stuck, the best thing to do instead of just writing more code is to see if you can somehow modify/change your approach to make the problem easier. also, speaking from experience, a naive/brute force approach is usually a good starting point, because it sets the foundation for both understanding the problem more fully, and for iterating to a better solution.

in this particular case, a naive/brute force approach would be the one i laid out above --- write a solution that's tailored specifically to the example you're given, and then try to generalize your solution from there by adding or subtracting things from the example and seeing what part of your solution breaks down in what kinds of situations.

This post was edited by irimi on Nov 18 2012 06:24pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 20 2012 04:58am
Am I a bad CS student for still having trouble with this? Albeit I haven't done any actual coding yet, I just still feel lost.

I would LIKE to just brute force this assignment, but I need to figure out some c++ specific syntax in order to do that.
Go Back To Programming & Development Topic List
Prev1121314151656Next
Add Reply New Topic New Poll