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
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.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?
The only place i is used in as an array index. So starting at 1 would make no sense because C++ is 0-index.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.
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.
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.tycoonheart said:You could just as easily have it start at 1 and do i <= numYears.
It will loop the same number of times.
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.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.
What? It's literally the same thing.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.
void Rational::outputDecimal()
{
double temp = static_cast <double> (num)/den;
cout << temp <<endl;
}
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.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.
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?
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.
cat
racecar
A man, a plan, a canal: Panama.
this is not a palindrome
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.
while (true == false == true == false)
{
//Do stuff
}
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
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.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.
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?
int main()
{
vector<string> test;
string x;
while (getline(cin, x))
{
test.push_back(x);
}
cout << test[1][1];
return 0;
}
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.
Alright so I messed around with this and it works great. I know what to do with the palindrome stuff using this.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.
racecar
banana
Dad
Dogma: I am God
Output:
raceDaDogmaIamGodadecar
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
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.
class Node
{
public:
Node *next;
std::string data;
};
class StringList
{
public:
StringList();
void push_back(std::string);
...
private:
Node *head;
};
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
}
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).root->next = 0; // This is pointing to the next node. Why is it set to 0?
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.if ( conductor != 0 ) {
while ( conductor->next != 0)
conductor = conductor->next; // I'm not sure what this is doing
}
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?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.
//loop
conductor->next = new node;
conductor = conductor->next;
conductor->x = "array here"
//end loop
Yes, it means the linked list is only a head with no tail.So would that mean that the linked list ends at the head if the root node points to 0?
Not sure about this, I'll leave the question to someone else.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?
Okay so what do I set the root->next to, to go to the conductor node instead of using 0 to end it?
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?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.
#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;
}
}
}
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.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.
void function(Object *object) {}
//or
void function(Object &object) {}
void function(Object &object) {
Object *p = &object;
//do stuff with p
}
void function(Object &object) {
Object *p = &object;
//do stuff with p
delete p; //or p = NULL ?
}
Object *aObject = new Object();
Object *p = object;
// do stuff using p
p = NULL;
//do stuff with object
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?
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.
By the way if 'a' is pointer, doing *p = a is equivalent to doing *p = &a, right?
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.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.
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...
XNA. There must be at least one tutorial in the internet that shows you how to draw a sphere in XNA.
#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;
}
#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);
}
};
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);
}
}
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);
}
}
city closestCity = citytree.nearestCity (51, 151);