Two Plus Two Poker Forums Programming homework and newbie help thread
 Register FAQ Search Today's Posts Mark Forums Read TwoPlusTwo.com

 Notices The Theory of Poker Applied to No-Limit now available For those of you here in Las Vegas, The Theory of Poker Applied to No-Limit by David Sklansky is now available at Gambler’s General Store/ GAMBLER'S BOOK CLUB in downtown Las Vegas. Their address is 727 S Main St, Las Vegas, NV 89101 and their phone number is (702) 382-9903. We also have this title available in several special poker book promotions directly from Two Plus Two Publishing. For more info or to ask questions check out this thread in the books and publications forum: David Sklansky Answers Questions About The Theory of Poker Applied to No Limit.

10-23-2014, 12:25 PM   #51
maxtower
Carpal \'Tunnel

Join Date: Sep 2005
Posts: 7,904
Re: Programming homework and newbie help thread

Quote:
 Originally Posted by tm33 Hi, after 3 days of trying really hard, I decided that i need help. Let's have a cube and two points A[x1,y1,z1], B[x2,y2,z2]. I need to calculate the shorthest path between these two points. The path has to be on surface of the cube. If the two points are on the same side, then it is easy. I managed to write a code for situation when they are on the opposite sides. It is somehow working altough it has probably some flaws. But I just can't figure out, what to do when they are on adjacent sides. Obviously I tried using Pythagorean equation in a lot of different ways, but there was always some hole in my code. If you could please just give me some advice. Thank you
The shortest distance between the two points will define the hypotenuse of the right triangle which contains the surface distance you are looking for by adding the A side and B side lengths. On the cube, I believe there will be two separate right triangles which can be formed by these two points. You can figure out each by taking the shortest distance to the common cube-edge from each point. Then compute the two A+B distances of the two triangles and report the shorter of the two.

Sorry if this explanation is bad, it's hard without drawing a picture.

 10-23-2014, 07:09 PM #52 KatoKrazy Pooh-Bah     Join Date: Jan 2009 Location: Orange County, CA Posts: 4,053 Re: Programming homework and newbie help thread I feel the worst I have ever felt after a test. Damn that was hard.
 10-24-2014, 12:02 AM #53 Anais Carpal \'Tunnel     Join Date: Mar 2006 Posts: 6,666 Re: Programming homework and newbie help thread not to turn into the wii u thread, but
10-24-2014, 02:28 PM   #54
well named
poorly undertitled

Join Date: Jun 2007
Location: esse est coesse
Posts: 75,980
Re: Programming homework and newbie help thread

Quote:
 Originally Posted by adios In C, how would you dynamically allocate 1K bytes and have them all be zero with one standard C library call?
calloc(1000, sizeof(char))

 10-25-2014, 09:11 PM #55 Barrin6 Carpal \'Tunnel     Join Date: Dec 2005 Location: beyond legal blindness Posts: 6,878 Re: Programming homework and newbie help thread Does anyone see a memory leak here? The method reads in a stream of integers and stops when an invalid character is inputted. I got the right size and array with the correct inputs. However the goal of the assignment is not only to get this right, but also not leak any memory which I am still somehow. Code: ```int* readData(istream& in, unsigned& size) { size = 0; int* index = new int [10]; int* beg = index; char ch; while (in.get(ch)) { if (isdigit(ch) || isspace(ch)) { //if size already a multiple of 10 then we need to increase the size //and copy the array till the end if (size % 10 == 0) { int *newarray = new int[size + 10]; int *beginofnewarray = newarray; for (size_t i = 0; i < size; i++ ) { *newarray = beg[i]; newarray++; } delete[] beg; //delete the old array beg = beginofnewarray;//set pointer back to the new array index = newarray;// set pointer past the end of the last value } in.unget(); int num; in >> num; if (in) { *index = num; index++; size++; } } } //create a new array with the exact size int* theArray = new int[size]; //copy values from old array to new for (size_t i = 0; i < size; i ++) { theArray[i] = beg[i]; } //delete the old array delete[] beg; beg = nullptr; index = nullptr; return theArray; }```
 10-25-2014, 11:10 PM #56 Barrin6 Carpal \'Tunnel     Join Date: Dec 2005 Location: beyond legal blindness Posts: 6,878 Re: Programming homework and newbie help thread Actually please disregard, it might be something with the code checker since it still says a memory leak even when I create and return a pointer
 10-26-2014, 05:31 PM #57 jmakin debauchery and general idiocy     Join Date: Jan 2008 Location: Streaming Posts: 28,507 Re: Programming homework and newbie help thread Maybe someone can help solidify my understanding of pointers in C++. In java, for instance, all variables are "pointers" (not really i know). When I say x = y, you're merely making x refer to y. In C++, that same statement actually creates a copy of variable y into variable x. So, my question is, in C++, how the **** do variables work? If I have a variable declared and initialized like - int x = 10; int * p = &x; *p = 4; // changes x to 4 (the value that p points to is changed to 4 is how i read that in my head) would *x be the same essentially as x or am I misunderstanding completely?
 10-26-2014, 05:40 PM #58 Anais Carpal \'Tunnel     Join Date: Mar 2006 Posts: 6,666 Re: Programming homework and newbie help thread Ask me again on Wednesday. We start pointers this week. Aorn, I have no idea what they are.
10-26-2014, 06:41 PM   #59
muttiah
Carpal \'Tunnel

Join Date: Aug 2004
Posts: 23,458
Re: Programming homework and newbie help thread

Quote:
 Originally Posted by jmakin Maybe someone can help solidify my understanding of pointers in C++. In java, for instance, all variables are "pointers" (not really i know). When I say x = y, you're merely making x refer to y. In C++, that same statement actually creates a copy of variable y into variable x. So, my question is, in C++, how the **** do variables work? If I have a variable declared and initialized like - int x = 10; int * p = &x; *p = 4; // changes x to 4 (the value that p points to is changed to 4 is how i read that in my head) would *x be the same essentially as x or am I misunderstanding completely?
x is a variable. It has the value 4. *x is invalid syntax because x is not a pointer. You can't deference a non-pointer. *p is how you change the value at the address stored in p. Remember a pointer is simply an address of a memory location where the actual value 4 is stored.

 10-26-2014, 06:44 PM #60 jmakin debauchery and general idiocy     Join Date: Jan 2008 Location: Streaming Posts: 28,507 Re: Programming homework and newbie help thread Ok, got it
10-26-2014, 08:32 PM   #61
Carpal Tunnel

Join Date: Jul 2006
Posts: 22,554
Re: Programming homework and newbie help thread

Quote:
 Originally Posted by muttiah x is a variable. It has the value 4. *x is invalid syntax because x is not a pointer. You can't deference a non-pointer. *p is how you change the value at the address stored in p. Remember a pointer is simply an address of a memory location where the actual value 4 is stored.
This is the key, imo. Once you mentally separate the memory address from the value actually at that address these kinds of questions get much easier to understand.

 10-26-2014, 10:10 PM #62 jmakin debauchery and general idiocy     Join Date: Jan 2008 Location: Streaming Posts: 28,507 Re: Programming homework and newbie help thread I understand that the value and the address itself are different, that wasnt really my q
10-26-2014, 10:31 PM   #63
well named
poorly undertitled

Join Date: Jun 2007
Location: esse est coesse
Posts: 75,980
Re: Programming homework and newbie help thread

Quote:
 Originally Posted by jmakin Maybe someone can help solidify my understanding of pointers in C++. In java, for instance, all variables are "pointers" (not really i know). When I say x = y, you're merely making x refer to y. In C++, that same statement actually creates a copy of variable y into variable x. So, my question is, in C++, how the **** do variables work? If I have a variable declared and initialized like - int x = 10; int * p = &x; *p = 4; // changes x to 4 (the value that p points to is changed to 4 is how i read that in my head) would *x be the same essentially as x or am I misunderstanding completely?
To add to what was already said: when you declare "int x = 10", what happens is that the program makes room in the stack for an int, and the variable x is located at that memory address on the stack. *x, if the compiler would let you do it (to go beyond what muttiah said) is actually going to lead to some sort of memory access violation, because *x means to take the value of x and interpret it as a memory location (a pointer), and read from that location. But the location 10 is not very likely to be valid given how memory is laid out in most (all?) operating systems.

On the other hand, &x means to get the memory address where the value in the variable x is stored, which in this case is the location on the stack that was allocated when you declared the variable.

In the same way, when you declare "int *p", you are also created space on the stack, this time, rather than space for an integer, you are reserving enough space for a memory address, and declaring that the value stored at that address will be an int. Hence "pointer to int p".

int *p = &x

allocates enough space for a pointer, and in that space places, as a value, the memory address of the variable x

*p = 4;

"*p" means to take the value of p, which is the memory address &x, and use it as a memory address in the assignment. This is what it means for p to point at x. As long as p == &x, *p references the same memory location as &x

The thing that gets tricky is to remember that the value of the pointer, which is the address of the variable x (&x), is distinct from the location in memory of p itself, i.e &p. &p != &x, but *p == &x.

The name of a variable always references the "value" of the variable, but if the variable is of type "int *" then the value is itself a memory address, whereas with a type "int" the value is an integer. The memory address of a pointer is distinct from its value, which may point somewhere else.

x: the value of the variable
&x: the memory address where the variable lives
*x: the memory address designated by the data in the value of the variable.

*x is a special case only in that it won't lead to useful results if the type of x is not a pointer, but under the hood a memory address is just a number. It may be a 64bit number or 32bit number, but you can cast between the appropriate size unsigned integer and a memory address. Knowing that may help to understand what it means for the "value" of a pointer to be a memory address. It is just a number interpreted with a special meaning

 10-26-2014, 11:13 PM #64 jmakin debauchery and general idiocy     Join Date: Jan 2008 Location: Streaming Posts: 28,507 Re: Programming homework and newbie help thread Thank you, that is what i was trying to get at. I knew &p was the address of the pointer itself, and wondered if something similar existed with variables of primitives. That helps a lot, thank you
 10-27-2014, 12:37 AM #65 well named poorly undertitled     Join Date: Jun 2007 Location: esse est coesse Posts: 75,980 Re: Programming homework and newbie help thread Correction p == &x *p == x As far as actual usage. My description might confuse you on that. I was trying to emphasize using *p when you want to change the value at the address pointed to by both p and &x, but I wrote *p == &x which is incorrect as far as code. hopefully that doesn't confuse the issue too much more It can be useful to play with print statements like printf("%x", &x); printf("%x", p); To see memory addresses directly
 10-27-2014, 02:26 AM #66 Anais Carpal \'Tunnel     Join Date: Mar 2006 Posts: 6,666 Re: Programming homework and newbie help thread Just reminded me what I hate about learning html: the teacher is always talking about P this and P that. And I've usually just had way too much coffee before class.
 10-27-2014, 10:47 AM #68 KatoKrazy Pooh-Bah     Join Date: Jan 2009 Location: Orange County, CA Posts: 4,053 Re: Programming homework and newbie help thread Great post adios. It is very true if a student doesn't know how these languages work at a deeper level they will have trouble making the correct decision on what language to use for certain tasks. For example, Java is for the most part dynamically bound, which will inherently make it slower. As you said, scope and life are also very important. For example, if a student doesn't understand that the variable they just created will live on the stack and is going to be gone when the stack frame is popped off, their program might not work in the way they intended.
 10-27-2014, 02:57 PM #69 Anais Carpal \'Tunnel     Join Date: Mar 2006 Posts: 6,666 Re: Programming homework and newbie help thread Can't speak to that question yet adios, as I'm only halfway thru my first real programming language class (c++). We haven't covered the stack at all, really. About the only memory related thing we've discussed is how pass by reference variables point to the memory location of the variable, or some such. By scope I assume you mean scope of a variable? Definitely covered that early on. No clue what life refers to. And we haven't really touched on how processors work wrt what we're writing. I assume we will probably cover that sort of thing in a later class.
 10-27-2014, 03:25 PM #70 KatoKrazy Pooh-Bah     Join Date: Jan 2009 Location: Orange County, CA Posts: 4,053 Re: Programming homework and newbie help thread Anais, I really hope your teacher ends up covering where the variables you are creating will be, and what their scope is. He should be teaching you what it means when you make a pointer, declare a static variable, etc.
 10-27-2014, 03:52 PM #71 Anais Carpal \'Tunnel     Join Date: Mar 2006 Posts: 6,666 Re: Programming homework and newbie help thread I already said pointers is this week! Probably. We just had a mid term, dunno if we're going to do much on them this week or wait till next week. Either way, I'll try to read that chapter today/tomorrow.
 10-27-2014, 04:17 PM #72 KatoKrazy Pooh-Bah     Join Date: Jan 2009 Location: Orange County, CA Posts: 4,053 Re: Programming homework and newbie help thread Awesome. Our teacher did a really good job explaining and/or drawing out where things will reside at (static memory, stack, heap). It was really helpful when he would draw pictures of everything.
 10-28-2014, 05:17 PM #73 jmakin debauchery and general idiocy     Join Date: Jan 2008 Location: Streaming Posts: 28,507 Re: Programming homework and newbie help thread I can't figure out how to access a member to a pointer I have in a vector. my loop has this statement - cout << c->students.at(i) << endl; c is a struct that has a vector of student pointers. students is the vector of Student pointers. I know this statement prints the address of that pointer. but when I try to type - c->students.at(i)->name Eclipse tells me there's no such data field as "name." and i get an error, but it will still compile. my structs are defined like this: Code: ```struct Course; struct Student; struct Student { string name; vector courses; }; struct Course { string name; vector students; };``` and my enroll function appears to be working because I'm passing all those tests. What the heck is going on? Please help me, this is infuriating and i've tried seemingly everything.
 10-28-2014, 05:59 PM #74 Mr.mmmKay Drugs are bad...     Join Date: May 2011 Location: Treat your womenfolk with care Posts: 16,220 Re: Programming homework and newbie help thread The following code runs fine for me, are you trying to do something different? Code: ```#include #include #include struct Course; struct Student; struct Student { std::string name; std::vector courses; }; struct Course { std::string name; std::vector students; }; int main() { Student s; s.name = "Jmakin"; Course a; a.students.push_back(&s); Course *c = &a; std::cout << c->students.at(0)->name << '\n'; return 0; }```
 10-28-2014, 06:32 PM #75 jmakin debauchery and general idiocy     Join Date: Jan 2008 Location: Streaming Posts: 28,507 Re: Programming homework and newbie help thread I think it may be because i forgot to include std:: in my vector declaration. The problem appears to be my function that adds pointers to each respective vector. It's weird because one works fine and one doesn't.

 Thread Tools Display Modes Linear Mode

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home Links to Popular Forums     News, Views, and Gossip     Beginners Questions     Marketplace & Staking     Casino & Cardroom Poker     Internet Poker     NL Strategy Forums     Poker Goals & Challenges     Las Vegas Lifestyle     Sporting Events     Other Other Topics Two Plus Two     About the Forums     Two Plus Two Magazine Forum     The Best of Two Plus Two Marketplace & Staking     Commercial Marketplace     General Marketplace     Staking - Offering Stakes     Staking         Staking - Offering Stakes         Staking - Seeking Stakes         Staking - Selling Shares - Online         Staking - Selling Shares - Live         Staking Rails         Transaction Feedback & Disputes     Transaction Feedback & Disputes Coaching & Training     Coaching Advice     Cash Game Poker Coach Listings     Tournament/SNG Poker Coach Listings Poker News & Discussion     News, Views, and Gossip     Poker Goals & Challenges     Poker Beats, Brags, and Variance     That's What She Said!     Poker Legislation & PPA Discussion hosted by Rich Muny     Twitch - Watch and Discuss Live Online Poker     Televised Poker General Poker Strategy     Beginners Questions     Books and Publications     Poker Tells/Behavior, hosted by: Zachary Elwood     Poker Theory     Psychology No Limit Hold'em Strategy     Medium-High Stakes PL/NL     Micro-Small Stakes PL/NL     Medium-High Stakes Full Ring     Micro-Small Stakes Full Ring     Heads Up NL     Live Low-stakes NL Limit Texas Hold'em Strategy     Mid-High Stakes Limit     Micro-Small Stakes Limit Tournament Poker Strategy     STT Strategy     Heads Up SNG and Spin and Gos     Mid-High Stakes MTT     Small Stakes MTT     MTT Community     Tournament Events Other Poker Strategy     High Stakes PL Omaha     Small Stakes PL Omaha     Omaha/8     Stud     Draw and Other Poker Live Poker     Casino & Cardroom Poker         Venues & Communities         Regional Communities     Venues & Communities     Tournament Events         WPT.com     Home Poker     Cash Strategy     Tournament Strategy Internet Poker     Internet Poker         Global Poker         MPN – Microgaming Poker Network         BetOnline.ag Online Poker     Commercial Software     Software         Commercial Software         Free Software General Gambling     Backgammon Forum hosted by Bill Robertie.     Probability     Sports Betting     Other Gambling Games 2+2 Communities     Other Other Topics         OOTV         Game of Thrones     The Lounge: Discussion+Review     EDF     Las Vegas Lifestyle     BBV4Life         omg omg omg     House of Blogs Sports and Games     Sporting Events         Single-Team Season Threads         Fantasy Sports     Fantasy Sports         Sporting Events     Wrestling     Golf     Chess and Other Board Games     Video Games         League of Legends         Hearthstone     Puzzles and Other Games Other Topics     Politics and Society     Playground: Well Named Loves Social Science     History     Business, Finance, and Investing     Science, Math, and Philosophy     Religion, God, and Theology     Travel     Health and Fitness     Laughs or Links!     Computer Technical Help     Programming

All times are GMT -4. The time now is 10:10 PM.

 Contact Us - Two Plus Two Publishing LLC - Privacy Statement - Top