• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

C++ Help?

Status
Not open for further replies.

RdN

Member
Hey, I forgot to come back here yesterday, I'm sorry.

I just want to say thanks very much for all the help, really saved my project.

You, gentleman, you rock.
 
I posted for help a while ago and didn't really articulate my thanks, so thanks again for all the help. Kudos to the cpp_is_king.

On to other things, I'm looking to get some private help off with some minor C++ work, if anyone has an hour or so a week, I will offer compensation. Please PM for more details. Thanks!
 

Exuro

Member
Hello everyone I was given this function which is suppose to be correct but it doesnt look right.

Code:
void FINANCE::SinglePaymentPresentWorth()
{
    double apr;
    
    FINANCE::EnterData();
    cout << "\nEnter Future Sum = ";
    cin >> FutureSum;
    apr = APR/100;
    array = new double [NumYears];
    // array holds the Present Sum per year
    for (int i = 0; i < NumYears; i++)
    {
        array[i] = (FutureSum / pow((1 + apr), i));
        cout << "array[" << i << "]= " << array[i] << endl;
    }    
} // end of function

This is a function that does single payment present worth as indicated, however doesn't this miss out on the last year? It would list the initial sum and then increment up to year 9 if i set it to 10 years no? This is suppose to be correct and I'm doing a few other functions where i need to match this but I'm using an online calculator that is giving me an extra result. Is this correct or do I need to change it?
 

AcciDante

Member
Exuro said:
Hello everyone I was given this function which is suppose to be correct but it doesnt look right.

This is a function that does single payment present worth as indicated, however doesn't this miss out on the last year? It would list the initial sum and then increment up to year 9 if i set it to 10 years no? This is suppose to be correct and I'm doing a few other functions where i need to match this but I'm using an online calculator that is giving me an extra result. Is this correct or do I need to change it?
I'm not super sure what you're asking, but yes, the last iteration of that loop is i = 9. It is looping ten times through, but the first one is i = 0, so when i = 9, it has looped 10 times. You need it to work like that to iterate through your array because the first index of an array is 0, so an array of size 10 has elements 0 - 9. To make it appear like it's 1 - 10, just add one to i in your cout that's in the loop.

Hopefully that was helpful...
 

Exuro

Member
My issue isn't how it loops, rather why he would set it to start at 0 years and end at 9 years for the equation. Like instead of having i in the equation, have it at i+1 so it starts at year 1 and ends at year 10. I guess this is more of a math question rather than a programming question.
 

usea

Member
Exuro said:
My issue isn't how it loops, rather why he would set it to start at 0 years and end at 9 years for the equation. Like instead of having i in the equation, have it at i+1 so it starts at year 1 and ends at year 10. I guess this is more of a math question rather than a programming question.
The only place i is used in as an array index. So starting at 1 would make no sense because C++ is 0-index.

edit: oh I'm wrong, also used as power function. I don't know the formula, so I'm no help.
 

Exuro

Member
i in the function is used as the year so in the formula the first iteration would have the year set to zero. I would think that that would need to be set to 1(would be i+1) and then the last iteration would be at the max year.
 
Exuro said:
My issue isn't how it loops, rather why he would set it to start at 0 years and end at 9 years for the equation. Like instead of having i in the equation, have it at i+1 so it starts at year 1 and ends at year 10. I guess this is more of a math question rather than a programming question.

Edit:

nvm, you are asking a math question. lol, sorry. hold on.
 

Exuro

Member
tycoonheart said:
You could just as easily have it start at 1 and do i <= numYears.

It will loop the same number of times.
True, it's just that the teacher gave us this and another function with the same issue to use as a template for 3 other functions and it being wrong is annoying/confusing me.
 
Yeah I think you're right. it needs to start at 1.

If that is the case you want to do:

for (int i = 1; i <= NumYears; i++)

and for arrays do array[i-1]

or leave it as is and do i+1 in the formula, this is probably easier.
 

Chris R

Member
tycoonheart said:
Yeah I think you're right. it needs to start at 1.

If that is the case you want to do:

for (int i = 1; i <= NumYears; i++)

and for arrays do array[i-1]

or leave it as is and do i+1 in the formula, this is probably easier.
I would NEVER do something like array[i-1]. That is just screaming for something to go very very VERY wrong. I'd leave it as is and do the (i+1) solution where needed instead.
 

usea

Member
rhfb said:
I would NEVER do something like array[i-1]. That is just screaming for something to go very very VERY wrong. I'd leave it as is and do the (i+1) solution where needed instead.
What? It's literally the same thing.

I mean, I agree with you. It's better to simply do i+1 in the formula and index naturally with unmodified i. But only because it's more readable for most people. And it only matters if this code was going to be reused and maintained (which I'm certain is not the case).

Nothing is going to magically go wrong because you subtract when indexing an array.
 
Code:
void Rational::outputDecimal()
{
	double temp = static_cast <double> (num)/den;

	cout << temp <<endl;
	
}

Simple question, I got this to work (taking a numerator and denominator of original type int) but I don't quite understand why it works the way it does? I was taking a value, in this case (1,2) as 1/2, and converting it and displaying it as a decimal.

Am I right to assume it takes the variable (int num) which is supplied in private class, and converting it to a type double (so 1 > 1.0), which then allows 1.0/2 to display as a type double?
 

Chris R

Member
usea said:
What? It's literally the same thing.

I mean, I agree with you. It's better to simply do i+1 in the formula and index naturally with unmodified i. But only because it's more readable for most people. And it only matters if this code was going to be reused and maintained (which I'm certain is not the case).

Nothing is going to magically go wrong because you subtract when indexing an array.
If you are working alone and remember that your index starts at 1 there then I guess you are right. I'm just used to coding with others, where doing something like that would be a pretty bad idea.
 

Margalis

Banned
Am I right to assume it takes the variable (int num) which is supplied in private class, and converting it to a type double (so 1 > 1.0), which then allows 1.0/2 to display as a type double?

There are rules for what the results of math operations are given the input types.

In most programming if you do math on two integers the result is an integer. If you want a floating point result you need to have one of the inputs be floating point.

You can't take the result of the operation and then cast it because by then it's too late, the result was already an int.
 

IceCold

Member
rhfb said:
If you are working alone and remember that your index starts at 1 there then I guess you are right. I'm just used to coding with others, where doing something like that would be a pretty bad idea.

I'm with you on this. It's a strange thing to do imo and prone to errors. I don't see why you wouldn't just start the loop with i = 0 until it's equal to the size - 1 of the array.
 

Haly

One day I realized that sadness is just another word for not enough coffee.
Code:
for (int i = 0; i < size; i++)
Easy to read, intuitive (for others).

Getting used to 0 indexing also helps if/when you do pointer arithmetic.
 

Exuro

Member
Okay I've got a real C++ question now. I'm going to be reading a file that takes in words per line, for example:
Code:
cat
racecar
A man, a plan, a canal: Panama.
this is not a palindrome

As you can guess I'll be checking to see if they're palindrome or not. I know how to do that part, my issue is getting the values from the file. Right now I'm using a string vector and getline to grab each word per line but then I'd need to grab each character in the vector which I'm not sure how to do plus that sounds like I'm doing an extra step. Whats the best/simple method to do this? I was also trying to grab each value with getchar, but I'm not sure how to detect new line and then start with a new vector/array for the next word. Ideas?
 
rhfb said:
If you are working alone and remember that your index starts at 1 there then I guess you are right. I'm just used to coding with others, where doing something like that would be a pretty bad idea.

It's possible you guys are talking about two different things. In principle, there's nothing wrong with doing index math inside of the array index operator. You can do array[i*i+2*n+3] if it makes sense and is clear in the context.

But what doesn't make sense is starting your array indexing at 1 and then always subtracting one. Yea, the code is correct and nothing will go wrong, but while you're at it you could also write infinite loops as:

Code:
while (true == false == true == false)
{
    //Do stuff
}

This code is also "correct" (mind fuck, but it's true), but it isn't the accepted way of writing an infinite loop, just like starting an array at 1 is not the accepted way of iterating over a collection of things.
 

W1SSY

Member
I am writing a binary tree operation that has to look and see if a value is in a tree yet for some reason I cannot get it to work. It has to be recursive and not use brute force so that is why I have the areEqual and areOrdered. The problem I am having is each time it compares to the first but if the value being compared is not at the root it returns false. IT is probably something eays but I cannot figure it out, does anyone have an idea of what is going on?

Code:
template <class DT, class RT, class TCompare>
Boolean Map3<DT, RT, TCompare>::inTree (
      preserves checked(TreeNodeClass*)& p,
	  preserves DT& d
   )
{
	Boolean found;
	if(p != NULL && !TCompare::areEqual(p->dItem, d)){
		if(TCompare::areOrdered (d, p->dItem)){
			inTree(p->leftSubtree, d);
		}
		else{
			inTree(p->rightSubtree, d);
		}
	}
	if(p != NULL)
		found = TCompare::areEqual(p->dItem, d);
		return(found);
} //inTree
 
W1SSY said:
I am writing a binary tree operation that has to look and see if a value is in a tree yet for some reason I cannot get it to work. It has to be recursive and not use brute force so that is why I have the areEqual and areOrdered. The problem I am having is each time it compares to the first but if the value being compared is not at the root it returns false. IT is probably something eays but I cannot figure it out, does anyone have an idea of what is going on?

Code:
template <class DT, class RT, class TCompare>
Boolean Map3<DT, RT, TCompare>::inTree (
      preserves checked(TreeNodeClass*)& p,
	  preserves DT& d
   )
{
	Boolean found;
	if(p != NULL && !TCompare::areEqual(p->dItem, d)){
		if(TCompare::areOrdered (d, p->dItem)){
			inTree(p->leftSubtree, d);
		}
		else{
			inTree(p->rightSubtree, d);
		}
	}
	if(p != NULL)
		found = TCompare::areEqual(p->dItem, d);
		return(found);
} //inTree

What language is this? It sure looks like C++, but I have no idea what "preserves" and "checked" mean?

Anyway, I think the issue is that you need to return the value returned by the recursive call. You are calling inTree(p->leftSubtree, d), and similarly for right, but what are you doing with the return value? Those two lines should also return the value.
 

W1SSY

Member
cpp_is_king said:
What language is this? It sure looks like C++, but I have no idea what "preserves" and "checked" mean?

Anyway, I think the issue is that you need to return the value returned by the recursive call. You are calling inTree(p->leftSubtree, d), and similarly for right, but what are you doing with the return value? Those two lines should also return the value.
It is c++ we are just using "preserves" and "checked" for my class so you know what is happening to the variables. Now that I look at it I see that I wasn't catching the return value from the recursive call. I knew it was going to be something so easy but I was just overlooking it. Thanks for the help, I should be able to get it going now.
 
Exuro said:
Okay I've got a real C++ question now. I'm going to be reading a file that takes in words per line, for example:
Code:
cat
racecar
A man, a plan, a canal: Panama.
this is not a palindrome

As you can guess I'll be checking to see if they're palindrome or not. I know how to do that part, my issue is getting the values from the file. Right now I'm using a string vector and getline to grab each word per line but then I'd need to grab each character in the vector which I'm not sure how to do plus that sounds like I'm doing an extra step. Whats the best/simple method to do this? I was also trying to grab each value with getchar, but I'm not sure how to detect new line and then start with a new vector/array for the next word. Ideas?

You can index into a string as if it was an array so the first character would be obtained by performing arrayName[0]. Then you have the vector which can also be thought as an array and can also be indexed in the same manner. Try to run this code to get a better idea.

Code:
int main()
{

	vector<string> test;

	string x;
	while (getline(cin, x))
	{
		test.push_back(x);
	}

	cout << test[1][1];

        return 0;
}

I have a piece of code that can check for palindromes in one line using the equal() function from the standard library, so this shouldn't be too hard to figure out.
 

Drkirby

Corporate Apologist
What would be an easy way to compare two strings, but with Wildcard support? The input is a text file, and it is supposed to search the text file for a string of characters, with room for one being different. Then report if and where it found the match.
 

Exuro

Member
Working on my palindrome program, pretty much experimenting with things and getting them to work. How would I make it so capital letters and lowercase letters were the same value?
 
What would be an easy way to compare two strings, but with Wildcard support? The input is a text file, and it is supposed to search the text file for a string of characters, with room for one being different. Then report if and where it found the match.

The easiest way is to use the grep utility that you can find by googling, and use the pattern .* to represent the "wildcard" portion. Examples:

foo.*bar - finds strings that start with foo and end with bar, with 0 or more characters in between

foo.bar - finds strings that start with foo and end with bar, with exactly 1 character in the middle.

foo.+bar - finds strings that start with foo and end with bar, with at least 1 character in between.

It sounds like you might want the second pattern. If you have any version of Visual Studio installed, you can hist Ctrl+Shift+F and it brings up the Find in files dialog where you can input the pattern and choose "Use Regular Expressions" in the advanced find options.


You can also find regular expression libraries available freely on the internet. For example, boost::regex, or if you don't mind using another language, System.Text.RegularExpression in .NET / C#.


If you really must handcode this in C++, then just do it the way that you are probably trying to avoid doing it. Analyze the pattern first for the presence of a wildcard character and split it up into 2 pieces, one before and one after the wildcard character. Search for the first piece, then starting from there search for the last piece.
 

Exuro

Member
You can index into a string as if it was an array so the first character would be obtained by performing arrayName[0]. Then you have the vector which can also be thought as an array and can also be indexed in the same manner. Try to run this code to get a better idea.

Code:
int main()
{

	vector<string> test;

	string x;
	while (getline(cin, x))
	{
		test.push_back(x);
	}

	cout << test[1][1];

        return 0;
}

I have a piece of code that can check for palindromes in one line using the equal() function from the standard library, so this shouldn't be too hard to figure out.
Alright so I messed around with this and it works great. I know what to do with the palindrome stuff using this.

There is a second part to my program however that I need to output that I'm having a hard time understanding. The end of the output needs to have one giant palindrome of the successfully tested palindromes in a linked list, so if the input like was
Code:
racecar
banana
Dad 
Dogma: I am God

Output:

raceDaDogmaIamGodadecar
I was just introduced to linked list and am having trouble following some online examples. All I really understand is that you have data and your data has a node that points to the next data until the end which is a null pointer. I'm having trouble understanding it in code. Any help would be appreciated.
 
Exuro said:
Alright so I messed around with this and it works great. I know what to do with the palindrome stuff using this.

There is a second part to my program however that I need to output that I'm having a hard time understanding. The end of the output needs to have one giant palindrome of the successfully tested palindromes in a linked list, so if the input like was
Code:
racecar
banana
Dad 
Dogma: I am God

Output:

raceDaDogmaIamGodadecar

Does each letter have to be a node, or is each word a node?

Exuro said:
I was just introduced to linked list and am having trouble following some online examples. All I really understand is that you have data and your data has a node that points to the next data until the end which is a null pointer. I'm having trouble understanding it in code. Any help would be appreciated.

Not quite. You have it the other way around. A linked list is made out of nodes. Each node holds an element and an address to the next node (depending on the type of linked list). The simplest form of linked list called a singly linked list where each node has a pointer to the next node in the list. For example:

Head-->["R" | --]-->["a" | --]-->["c" | --]-->...-->["r" | null ]

What is confusing you? If you want to implement the linked list you are probably going to need a Node class with an element of the type you want to hold, and a pointer to another Node.

For example:

Code:
class Node
{
public:
	Node *next;
	std::string data;
};

Then you will need another class to represent your LinkedList where an element is the head of the linked list.

Code:
class StringList
{
public:
	StringList();
	void push_back(std::string);
        ...


private:
	Node *head;
};

All your member functions in the LinkedList class will operate on the head element and subsequent elements.
 

Exuro

Member
They don't need to be characters I believe. It doesn't state the specifics on that. From what I'm understand I need to split the true palindromes in half as a string and print the first half and then the second half. The middle string would just be posted as the one listed doesn't repeat "I" twice while the others repeat their middles.

As for the linked list understanding, think the best way is to show you tutorial code and just help me understand that. I'm looking at the page below to understand it.

http://www.cprogramming.com/tutorial/lesson15.html

Code:
struct node {
  int x;
  node *next;
};

int main()
{
  node *root;       // This is the head, initial node that doesnt change
  node *conductor;  // This is the node that will be used to transverse the list

  root = new node;  // Not quite sure what new node means, other than being set to a new node.  I dont know a lot about new other than using it to initialize memory for arrays.
  root->next = 0;   //  This is pointing to the next node.  Why is it set to 0?
  root->x = 12;
  conductor = root; // The conductor points to the first node
  if ( conductor != 0 ) {
    while ( conductor->next != 0)
      conductor = conductor->next; // I'm not sure what this is doing
  }
  conductor->next = new node;  // address of new created node
  conductor = conductor->next; // Points to that node
  conductor->next = 0;         // set to 0 like above.  Not sure why they both have the same value
  conductor->x = 42;  // data in the node
}

The comments in that are of my own. It's probably something obvious that I'm not getting. Once i understand the basics better I'll throw them into classes and such.
 

Haly

One day I realized that sadness is just another word for not enough coffee.
root->next = 0; // This is pointing to the next node. Why is it set to 0?
This is saying that this node is the last node in the linked list, and that it does not link to a further node (links to null pointer).
if ( conductor != 0 ) {
while ( conductor->next != 0)
conductor = conductor->next; // I'm not sure what this is doing
}
As long as the conductor points to an actual node, it will move on to the next node of the linked list. This ties in with setting the root->next to 0. Basically, it'll keep iterating through the linked list until conductor becomes a node pointer that evaluates to 0. Not sure why they used 0 instead of NULL in this case, the latter is far more readable.
 

Exuro

Member
This is saying that this node is the last node in the linked list, and that it does not link to a further node (links to null pointer).

As long as the conductor points to an actual node, it will move on to the next node of the linked list. This ties in with setting the root->next to 0. Basically, it'll keep iterating through the linked list until conductor becomes a node pointer that evaluates to 0. Not sure why they used 0 instead of NULL in this case, the latter is far more readable.
So would that mean that the linked list ends at the head if the root node points to 0? Or is that to stop the head from being the transveral node? And then to make a dynamic linked list I would need to loop this code?
Code:
//loop
conductor->next = new node;  
conductor = conductor->next; 
conductor->x = "array here"
//end loop

And then have a separate function that makes the node point to null?

EDIT: just tried to do that, doing something wrong.
EDIT2: what does it mean when it says conductor != 0?
EDIT3: What would I change root->next to link to the conductor node?
 

Haly

One day I realized that sadness is just another word for not enough coffee.
So would that mean that the linked list ends at the head if the root node points to 0?
Yes, it means the linked list is only a head with no tail.
And then to make a dynamic linked list I would need to loop this code?

And then have a separate function that makes the node point to null?
Not sure about this, I'll leave the question to someone else.
 

poweld

Member
Okay so what do I set the root->next to, to go to the conductor node instead of using 0 to end it?

You don't want to change the root node's next value, with the exception of initializing it to 0.

The concept behind this code is:

1. Create the root node which initially points to a dead-end (0)
2. Create a conductor pointer which initially points to the root node
3. As you want to extend the linked list, allocate a new node into the conductor's next pointer, and shift the conductor pointer to this location

Step 3 can be looped as many times as you'd like to create a longer linked list, but steps 1 and 2 should only take place one time. You most likely do not want to change the pointer to the root node, nor it's next pointer value.
 

Exuro

Member
You don't want to change the root node's next value, with the exception of initializing it to 0.

The concept behind this code is:

1. Create the root node which initially points to a dead-end (0)
2. Create a conductor pointer which initially points to the root node
3. As you want to extend the linked list, allocate a new node into the conductor's next pointer, and shift the conductor pointer to this location

Step 3 can be looped as many times as you'd like to create a longer linked list, but steps 1 and 2 should only take place one time. You most likely do not want to change the pointer to the root node, nor it's next pointer value.
Thank you, this sounds much easier than trying to comprehend 500 lines of code that half of the tutorials I've seen have. So bear with me, here's my attempt at this. I made a simple 5 element array to store it in. Let me know whats wrong/right. Is this right at all or am I still off?

Code:
#include <iostream>
using namespace std;

struct node {
  int x;
  node *next;
};

int main()
{
  int array[5] = {1, 2, 3, 4, 5};
  node *root;       
  node *conductor;  
  int z;

  root = new node;  
  root->next = 0;   
  root->x = array[0];
  conductor = root;

  cout<< "The head is: " << root->x << endl;

  for(z = 1; z < 5; z++)
	{
    	conductor->next = new node;
    	conductor = conductor->next;
    	conductor->x = array[z];

	if (z != 4)
		cout << "Node " << z << ": " << conductor->x << endl;
	else
	{
		conductor->next = NULL;
		cout << "The tail is: " << conductor ->x << endl;
	}
  	}
}
 

usea

Member
I made a simple 5 element array to store it in. Let me know whats wrong/right. Is this right at all or am I still off?
What's the point of the array in this? It unnecessarily complicates it. You should replace array[z] with z+1. And replace array[0] with 1.

Other than that, it looks OK to me. I would add functions for making a new node, printing the contents of the list, etc. It would really make everything organized and easy to read.
 

poweld

Member
What's the point of the array in this? It unnecessarily complicates it. You should replace array[z] with z+1. And replace array[0] with 1.

Other than that, it looks OK to me. I would add functions for making a new node, printing the contents of the list, etc. It would really make everything organized and easy to read.

Ah, forgot to hit post on my response last night. Pretty much what I had to say, though. The code could be cleaner, and that array is a little silly (and prone to error), but otherwise this should be fine.
 

IceCold

Member
Hey guys, I have two questions:


1) Let's say I want to pass a certain Object as a parameter to a function. I want this object to be set by reference and not by value. In the signature of my function should I pass the object as :

Code:
void function(Object *object) {}

//or  

void function(Object &object) {}


I feel like both these are pretty equivalent. In the second case, the only difference is that I would need to create a pointer that points to that address.

2) Let's say for the first example, in the implementation of the second function I decide to create a pointer that refers to that object.

Like this:

Code:
void function(Object &object) {

Object *p = &object;

//do stuff with p
}

Now lets say that the memory allocated to object is being referred to by many pointers throughout my code. At the end of this particular function I have no use for the pointer p anymore so how am I supposed to get rid of it? Am I supposed delete p or simply set the p to null? Also what happens to a pointer that points to null? Is it garbage collected automatically like in Java?

Code:
void function(Object &object) {

Object *p = &object;

//do stuff with p

delete p; //or p = NULL ?
}

Being a Java programmer I have to say that pointers are one the most annoying concepts of C++ for me.
 

Spoo

Member
1) They have differences, but for the most part are the same. I'd recommend just reading up on those, as usually it's dictated by what you'll need the function to do. I think the reference is more or less a C++ way of being stricter about what the function is able to do with the argument once it's been passed.

2) Once you use delete, anything that was pointing to that space effectively becomes garbage. So if you have pointers still referencing that memory, and say, try to use them to access the now deleted object, you'll have problems. To make it clear, setting p to null without first deleting the object will cause a leak.

Some programmers will set p to null after they delete it. Some do not.

Also remember that pointers point and sit on the stack, and the objects they point to sit on the heap. So, say, assigning p to a new object, and then having p set to null, you'll lose the reference to that object on the heap and "leak" -- so no memory management unless you are using auto_ptr or something like that.

Hopefully I'm right about this stuff. Been a little since I've used C++.
 

IceCold

Member
Sure, I'll have a leak if that was the only pointer pointing to my object but in my case I have many.

Let's say I have this:

Code:
Object *aObject = new Object();

Object *p = object;

// do stuff using p

p = NULL;

//do stuff with object

After I set p to NULL I can still manipulate the object using the pointer aObject.

By the way if 'a' is pointer, doing *p = a is equivalent to doing *p = &a, right?
 

Spoo

Member
Sure, I'll have a leak if that was the only pointer pointing to my object but in my case I have many.

Let's say I have this:

Code:
Object *aObject = new Object();

Object *p = object;

// do stuff using p

p = NULL;

//do stuff with object

After I set p to NULL I can still manipulate the object using the pointer aObject.

By the way if 'a' is pointer, doing *p = a is equivalent to doing *p = &a, right?

p = null just sets the stack variable to null. It does not delete the object. Say, in Java, what it does, is it counts the references to an object, and when the last reference goes out of scope (or you signify you're done with it by calling finalize) the garbage collector, at its own will, cleans up.

If, however, you call delete, the heap based object is killed (collected by YOU), and thus, any and all references to it are now defunct. So, setting to null is different from calling delete -- you can have as many references to the heap-based object as you like, but once you call delete she's gone, cap'n. So I'm just trying to be clear on that point.

Regarding this: By the way if 'a' is pointer, doing *p = a is equivalent to doing *p = &a, right?

It's easy to see what you're doing if you read it in English. * = the value at. So, the first line says "The value at p is assigned a" -- second line is, "the value at p is assigned the reference of a."
 

IceCold

Member
Yeah I understand the difference between delete and setting a pointer to null. I just thought that in C++ you had to clean up pointers that you don't need anymore.
 

Spoo

Member
Yeah I understand the difference between delete and setting a pointer to null. I just thought that in C++ you had to clean up pointers that you don't need anymore.

Well, if things are set to null, then it's easy to check them before you attempt deletion so you avoid, say, trying to delete things twice. So it's not a bad idea, even if it's not necessary all the time. It's either staring at null or at garbage, I guess.
 

maharg

idspispopd
By the way if 'a' is pointer, doing *p = a is equivalent to doing *p = &a, right?

Spoo didn't answer this directly, but for clarity's sake, no they're not at all the same thing. There is never any kind of automatic conversion between a pointer and a value in C/C++. And (p = a) is also not equivalent to (*p = *a), in case that's what you meant. The former is a reference assignment and the latter is a value assignment to a reference's target.
 

Slavik81

Member
Hey guys, I have two questions:


1) Let's say I want to pass a certain Object as a parameter to a function. I want this object to be set by reference and not by value. In the signature of my function should I pass the object as :

Code:
void function(Object *object) {}

//or  

void function(Object &object) {}


I feel like both these are pretty equivalent. In the second case, the only difference is that I would need to create a pointer that points to that address.
The main difference is that a pointer can be NULL while a reference cannot be. This places an onus on the caller to ensure they pass valid data into the function.

I'd say to use a pointer if it's acceptable for the object being passed in to be null. Otherwise, just use a reference.
 

(._.)

Banned
Anybody know how I could go about making a piece of procedural geometry in C# ?

something like a sphere or a cube maybe?

I'm lost...
 

(._.)

Banned
XNA. There must be at least one tutorial in the internet that shows you how to draw a sphere in XNA.

Yeah, I was thinking I would do it in XNA.

Did a google search and there are some tutorials and help, thanks breh

will post again if I need help ~.~
 

Heysoos

Member
So I have a small problem I can't seem to fix. I think it's probably something stupid on my part but I just can't figure it out, any help would be appreciated.

Code:
#include "quadTree.cpp"

int main()
{

    quadTree citytree;

    citytree.insert ( 100, 100, "Chicago");
    citytree.insert ( 25, 125, "McAllen");
    citytree.insert ( 50, 150, "Los Angeles");
    citytree.insert ( 75, 130, "Detroit");
    citytree.insert ( 150,  50, "New York");
    citytree.insert ( 10,  40, "Austin");

	citytree.display();
	cout<< "These cities are in range: " <<endl;
	citytree.inRange(25, 50, 75);

    city closestCity = citytree.nearestCity (63, 84);
    cout << "The closest city is " << closestCity.name << endl;
    return 0;
}
Code:
#include <iostream>
#include <string>
#include <cmath>
using namespace std;


class city
{
public:
	string name;
	double x;
	double y;
};

class node
{
public:
	city data;
	node *nw;
	node *ne;
	node *sw;
	node *se;

	node(city c)
	{
		data = c;
		nw = NULL;
		ne = NULL;
		sw = NULL;
		se = NULL;
	}
};
class quadTree
{
private:
	node * root;
	
	double distance(double x1, double y1, double x2, double y2)
	{
		return sqrt( (x1-x2)*(x1-x2)*1.0 + (y1-y2)*(y1-y2)*1.0 );
	}
	void recursiveInsert( city c, node * & r)
	{
		if( r == NULL )
			r = new node(c);
		else
		{
			if( c.x < r->data.x && c.y < r->data.y )
				recursiveInsert(c, r->sw);
			else if( c.x < r->data.x && c.y > r->data.y)
				recursiveInsert(c, r->nw);
			else if( c.x > r->data.x && c.y > r->data.y)
				recursiveInsert(c, r->ne);
			else
				recursiveInsert(c, r->se);
		}
	}
	
	void recDisplay(node *r)
	{
		if( r == NULL )
		{
		
		}
		else
		{
			recDisplay(r->sw);
			recDisplay(r->se);
			cout << r->data.name << endl;
			recDisplay(r->nw);
			recDisplay(r->ne);
		}

	}	

	void recinRange(double x, double  y, double range, node * r)
	{
		if( r != NULL )
		{
			if( distance(x, y, r->data.x, r->data.y) <= range )
				cout << r->data.name << endl;
			recinRange(x, y, range, r->ne);
			recinRange(x, y, range, r->nw);
			recinRange(x, y, range, r->se);
			recinRange(x, y, range, r->sw);
		}
	}
	
	void recNearest(double x, double y, node * r)
	{
		if( distance(x, y, r->data.x, r->data.y) == 0 )
		{
			cout<< r->data.name<<" : ( "<<r->data.x<<" , "<<r->data.y<<" )"<<endl;
		}
		else
		{
			recNearest(x,y,r->sw);
			recNearest(x,y,r->se);
			recNearest(x,y,r->nw);
			recNearest(x,y,r->ne);
		}
	}

public:

	quadTree()
	{
		root = NULL;
	}

	void insert(double x, double y, string name)
	{
		city c;
		c.x= x;
		c.y= y;
		c.name = name;
		recursiveInsert(c, root);
	}
	
	void display()
	{
		recDisplay(root);
	}
	
	void inRange(double x, double y, double range)
	{
		recinRange(x, y, range, root);

	}
	void nearestCity(double x, double y)
	{
		recNearest(x,y,root);
	}
};

My problem is this following segment:
city closestCity = citytree.nearestCity (63, 84);

The problem is that I'm trying to convert a 'city' to void when passing it to the nearestCity function which in turn passes it to the recursive function. I can't seem to figure out how to fix this. :/ I tried changing the 'void' to 'city' in the function but then it tells me it's supposed to return something, however, my function is not built to specifically return something. Any help would be appreciated. Also, I'm trying to keep
city closestCity = citytree.nearestCity (63, 84);
as it is because it's part of the problem.
 

Haly

One day I realized that sadness is just another word for not enough coffee.
First off, this function isn't doing what I think it should be doing:
Code:
city recNearest(double x, double y, node * r)
	{
		if( distance(x, y, r->data.x, r->data.y) == 0 )
		{
			cout<< r->data.name<<" : ( "<<r->data.x<<" , "<<r->data.y<<" )"<<endl;
		}
		else
		{
			recNearest(x,y,r->sw);
			recNearest(x,y,r->se);
			recNearest(x,y,r->nw);
			recNearest(x,y,r->ne);
		}
	}
One, it doesn't return a city object. If you discover that node r is the nearest city, then all you have to do is return r. Simple as that. But afterwards, you need a way to pass that city back up the chain of function calls. In pseudocode, it would look something like this.
Code:
city recNearest(double x, double y, node * r)
	{
		if(r is the city that is closest to x and y)
		{
			return r;
		}
		else
		{
			return recnearest(x, y, nextcity);
		}
	}

Two, it doesn't find the nearest city, but in fact, finds a city that is at the exact same xy coordinates as the parameters. So, for example, if you said
Code:
city closestCity = citytree.nearestCity (51, 151);
The nearest city to that should be Los Angeles, but your program would never catch that.

Three, it's not taking advantage of the way the tree is being set up. You have a city node, and it has four connections, NW, NE, SE, SW. Look at how the nodes are being inserted into the program. Right now, when your function gets some coordinates, it's checking every single city node that borders the root node.

You don't need to do that. In fact you should be able to pinpoint which direction the target coordinates are (because you do so during the insertion process), and then only check one new node. Then, you compare whether the new node is closer to the coordinates you're checking, or whether the original node is closer. If the new node is closer, you do another check because it might be connected to a city that's closer. If not, then you know that the current node is the closest city, and then you just return that.
 
Status
Not open for further replies.
Top Bottom