Two Plus Two Publishing LLC Two Plus Two Publishing LLC
 

Go Back   Two Plus Two Poker Forums > >

Notices

Programming Discussions about computer programming

Reply
 
Thread Tools Display Modes
Old 10-23-2014, 12:25 PM   #51
maxtower
Carpal \'Tunnel
 
maxtower's Avatar
 
Join Date: Sep 2005
Posts: 7,706
Re: Programming homework and newbie help thread

Quote:
Originally Posted by tm33 View Post
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.
maxtower is offline   Reply With Quote
Old 10-23-2014, 07:09 PM   #52
KatoKrazy
veteran
 
KatoKrazy's Avatar
 
Join Date: Jan 2009
Location: Roch, MN
Posts: 3,082
Re: Programming homework and newbie help thread

I feel the worst I have ever felt after a test. Damn that was hard.
KatoKrazy is offline   Reply With Quote
Old 10-24-2014, 12:02 AM   #53
Anais
Carpal \'Tunnel
 
Anais's Avatar
 
Join Date: Mar 2006
Posts: 6,666
Re: Programming homework and newbie help thread

not to turn into the wii u thread, but
Anais is offline   Reply With Quote
Old 10-24-2014, 02:28 PM   #54
well named
poorly undertitled
 
well named's Avatar
 
Join Date: Jun 2007
Location: esse est coesse
Posts: 73,199
Re: Programming homework and newbie help thread

Quote:
Originally Posted by adios View Post
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))
well named is offline   Reply With Quote
Old 10-25-2014, 09:11 PM   #55
Barrin6
Carpal \'Tunnel
 
Barrin6's Avatar
 
Join Date: Dec 2005
Location: beyond legal blindness
Posts: 6,632
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;
}
Barrin6 is offline   Reply With Quote
Old 10-25-2014, 11:10 PM   #56
Barrin6
Carpal \'Tunnel
 
Barrin6's Avatar
 
Join Date: Dec 2005
Location: beyond legal blindness
Posts: 6,632
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
Barrin6 is offline   Reply With Quote
Old 10-26-2014, 05:31 PM   #57
jmakin
 
jmakin's Avatar
 
Join Date: Jan 2008
Location: Streaming
Posts: 24,880
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?
jmakin is offline   Reply With Quote
Old 10-26-2014, 05:40 PM   #58
Anais
Carpal \'Tunnel
 
Anais's Avatar
 
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.
Anais is offline   Reply With Quote
Old 10-26-2014, 06:41 PM   #59
muttiah
Carpal \'Tunnel
 
muttiah's Avatar
 
Join Date: Aug 2004
Posts: 23,082
Re: Programming homework and newbie help thread

Quote:
Originally Posted by jmakin View Post
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.
muttiah is offline   Reply With Quote
Old 10-26-2014, 06:44 PM   #60
jmakin
 
jmakin's Avatar
 
Join Date: Jan 2008
Location: Streaming
Posts: 24,880
Re: Programming homework and newbie help thread

Ok, got it
jmakin is offline   Reply With Quote
Old 10-26-2014, 08:32 PM   #61
jjshabado
Carpal Tunnel
 
jjshabado's Avatar
 
Join Date: Jul 2006
Posts: 21,665
Re: Programming homework and newbie help thread

Quote:
Originally Posted by muttiah View Post
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.
jjshabado is offline   Reply With Quote
Old 10-26-2014, 10:10 PM   #62
jmakin
 
jmakin's Avatar
 
Join Date: Jan 2008
Location: Streaming
Posts: 24,880
Re: Programming homework and newbie help thread

I understand that the value and the address itself are different, that wasnt really my q
jmakin is offline   Reply With Quote
Old 10-26-2014, 10:31 PM   #63
well named
poorly undertitled
 
well named's Avatar
 
Join Date: Jun 2007
Location: esse est coesse
Posts: 73,199
Re: Programming homework and newbie help thread

Quote:
Originally Posted by jmakin View Post
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
well named is offline   Reply With Quote
Old 10-26-2014, 11:13 PM   #64
jmakin
 
jmakin's Avatar
 
Join Date: Jan 2008
Location: Streaming
Posts: 24,880
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
jmakin is offline   Reply With Quote
Old 10-27-2014, 12:37 AM   #65
well named
poorly undertitled
 
well named's Avatar
 
Join Date: Jun 2007
Location: esse est coesse
Posts: 73,199
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
well named is offline   Reply With Quote
Old 10-27-2014, 02:26 AM   #66
Anais
Carpal \'Tunnel
 
Anais's Avatar
 
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.
Anais is offline   Reply With Quote
Old 10-27-2014, 10:28 AM   #67
adios
Carpal \'Tunnel
 
Join Date: Sep 2002
Location: Russian Troll
Posts: 20,820
Re: Programming homework and newbie help thread

Caveat, probably a dumb question.

Just curious about this regarding the following question, why would anyone prefer C instead of Java? The standard answer is that C is closer to the macine, runs faster, etc. How do you think this works exactly? I mean what actually happens to get your C program to run and why is it faster? There are programming assignments that absolutely require C over Java and it really isn't all about execution speed either. In his detailed explanation well named mentioned the stack, how does that actually work in a processor? Why do some programming tasks demand that they be written in assembly language (most definitely true on Windows PCs too btw). A convoluted way to ask how much do understand about how a processor actually works?

Just asking because I have the view that it makes understanding C and C++ much easier. I also believe it can provide insight into other, more abstract programming paradigms.

BTW I hope your instructors are emphasizing scope rules in your C and C++ classes. If not they suck in my view.
adios is offline   Reply With Quote
Old 10-27-2014, 10:47 AM   #68
KatoKrazy
veteran
 
KatoKrazy's Avatar
 
Join Date: Jan 2009
Location: Roch, MN
Posts: 3,082
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.
KatoKrazy is offline   Reply With Quote
Old 10-27-2014, 02:57 PM   #69
Anais
Carpal \'Tunnel
 
Anais's Avatar
 
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.
Anais is offline   Reply With Quote
Old 10-27-2014, 03:25 PM   #70
KatoKrazy
veteran
 
KatoKrazy's Avatar
 
Join Date: Jan 2009
Location: Roch, MN
Posts: 3,082
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.
KatoKrazy is offline   Reply With Quote
Old 10-27-2014, 03:52 PM   #71
Anais
Carpal \'Tunnel
 
Anais's Avatar
 
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.
Anais is offline   Reply With Quote
Old 10-27-2014, 04:17 PM   #72
KatoKrazy
veteran
 
KatoKrazy's Avatar
 
Join Date: Jan 2009
Location: Roch, MN
Posts: 3,082
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.
KatoKrazy is offline   Reply With Quote
Old 10-28-2014, 05:17 PM   #73
jmakin
 
jmakin's Avatar
 
Join Date: Jan 2008
Location: Streaming
Posts: 24,880
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<Course*> courses;
};

struct Course
	{
		string name;
		vector<Student*> 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.
jmakin is offline   Reply With Quote
Old 10-28-2014, 05:59 PM   #74
Mr.mmmKay
Drugs are bad...
 
Mr.mmmKay's Avatar
 
Join Date: May 2011
Posts: 13,232
Re: Programming homework and newbie help thread

The following code runs fine for me, are you trying to do something different?
Code:
#include<string>
#include<vector>
#include<iostream>
struct Course;
struct Student;

struct Student
{
	std::string name;
	std::vector<Course*> courses;
};

struct Course
{
	std::string name;
	std::vector<Student*> 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;
}
Mr.mmmKay is online now   Reply With Quote
Old 10-28-2014, 06:32 PM   #75
jmakin
 
jmakin's Avatar
 
Join Date: Jan 2008
Location: Streaming
Posts: 24,880
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.
jmakin is offline   Reply With Quote

Reply
      

Thread Tools
Display Modes

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 Jump


All times are GMT -4. The time now is 04:49 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © 2008-2010, Two Plus Two Interactive
 
 
Poker Players - Streaming Live Online