|
-
September 26th, 2010, 04:51 PM
#16
Re: Call Array Member in function Help
 Originally Posted by M2W
The array of struct in my first post, and the linked-list in my previous post are intended to be a circular structure. That was my issue with the arrays. I needed to find a way to let the function know that it was working on the last element in the array, and then send the function call to the [0] element, and not to the next element (which would be out of bounds).
With the array, the end of the data structure can be determined from the index which is quite trivial. The nested loops shown in post #12 implement the circular behaviour more or less implicitly, despite the fact that the data structure itself is not circular.
Then I tried using a linked-list. And I figured I would make it circular by connecting the last pointer to the head pointer. The problem is, I don't know how to do this connection precisely. I know the next pointer should point to the top, but I don't know how to code that. It seems like I should just add a line or two to my code on the previous post and it will work.
You don't even have to insert a line. Just change this one during the creation of the initial node as shown in your post #13:
to
This means you begin with a kind-of degenerate circle containing a single node and then extend it node by node.
As the terminating condition of your iteration (or maybe recursion, but see below) isn't determined by the contents of your circular data structure at all, as I think I can conclude from your latest post, it isn't even a big pain not having an end marker in the data structure.
Finally, I want the thing to go into an endless loop, calling function_two on the next node, and then on the next one, etc. I kind of have to use recursion because that is the point of the problem assigned. The loop will end since I will put a counter that will go up with each function_two that is called and once it hits a limit it will exit the do-while.
And yet again I have to say that this is kind of the opposite of a typical recursion scenario. A termination condition based on a counter or even an external event makes this an almost certain candidate for a stack overflow.
But maybe this is kind of a homework assignment? Just recently, in another thread, I have seen a discussion about the fact that they more than merely sometimes are lacking a reasonable point...
-
September 26th, 2010, 05:01 PM
#17
Re: Call Array Member in function Help
 Originally Posted by M2W
Thanks Eri523. I'll work on your suggestions now, but will leave this post with little extra info.
The array of struct in my first post, and the linked-list in my previous post are intended to be a circular structure. That was my issue with the arrays. I needed to find a way to let the function know that it was working on the last element in the array, and then send the function call to the [0] element, and not to the next element (which would be out of bounds).
Then I tried using a linked-list. And I figured I would make it circular by connecting the last pointer to the head pointer. The problem is, I don't know how to do this connection precisely. I know the next pointer should point to the top, but I don't know how to code that. It seems like I should just add a line or two to my code on the previous post and it will work.
Finally, I want the thing to go into an endless loop, calling function_two on the next node, and then on the next one, etc. I kind of have to use recursion because that is the point of the problem assigned. The loop will end since I will put a counter that will go up with each function_two that is called and once it hits a limit it will exit the do-while.
I guess I'll worry about the memory leak once I get this thing running.
I'll go and see if I can implement what you tell me. Thanks a lot. Any more insights please post in the meantime.
To be quite frank, it looks like you are trying to take on much more than you can handle. Containers (like lists) make for good theoretical examples, but are very hard objects to code, especially if you want it done correctly.
I assume this is homework? This isn't lesson one is it? Your teacher probably gave a lesson on arrays and pointers, did you understand it? Same for function calls, by value and reference. I'd recommend you slow down and go back to those exercises...
The problem with coding with concepts you can't handle is that it always ends in "hey it compiles! and it looks like it works." Which is the worst thing you could do.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
September 26th, 2010, 07:30 PM
#18
Re: Call Array Member in function Help
Yeah tell me about it. I agree with your points. It's actually a graduate course. I was admitted to it since I had an upper degree in pure mathematics so I guess they expect me to know how to code miracles. (Miracles for me since I only have a basic knowledge of C++)
In any case, I'm armed with only a C++ book and an algorithms book. Both undergrad but they do help a lot. And I also have the internet, which comes with helping dudes I'm just trying to learn on my own, and pick up stuff as I go along. Next semester I'll definitely enroll on a programming class to catch up, but for now, I gotta make this happen.
As far as this coding exercise is concerned, what I ended up doing is using the linked-list and using pointers for the recursion. It didn't have to be a circular list (thanks Eri523) as it loops anyway after it hits the final node, which is what I wanted.
This is what I couldn't do before, but it works now:
Code:
struct Node
{
int number;
int data;
Node* link;
};
typedef Node* Node_Ptr;
void function_two(Node_Ptr some_pointer);
void insert_node(Node_Ptr &top, int num); //insert a new node, and modify its number member in Nodes
int big_counter = 10;
int counter = 0;
int main(){
Node_Ptr top;
top = new Node;
top->number = 2;
top->link = NULL;
insert_node(top, 1);
insert_node(top, 0);
do {
function_two(top);
} while (counter < big_counter);
char response;
cin >> response;
return 0;
}
void insert_node(Node_Ptr &top, int num)
{
Node_Ptr tmp_ptr;
tmp_ptr = new Node;
tmp_ptr->number = num;
tmp_ptr->link = top;
top = tmp_ptr;
}
The following is the definition for a recursive function_two():
Code:
void function_two(Node_Ptr some_pointer)
{
counter ++;
cout << "Counter = " << counter << endl;
if (!(some_pointer->link == NULL))
{
cout << "This is node: " << (some_pointer->number) << endl << endl;
function_two(some_pointer->link);
}
else
cout << "This is the last node: " << (some_pointer->number) << endl;
}
Now, so far I think it works, as I can output the list of struct Nodes, and I use a counter to see if they are indeed running orderly. A sample output for the above is:
Code:
This is node: 1
Counter = 1
This is node: 2
Counter = 2
This is the last node: 3
Counter = 3
This is node: 1
Counter = 4
This is node: 2
Counter = 5
... etc until some counter is reached
Anyway, you guys get the point.
If you see an obvious error, please let me know, if not, I'm sure ll be coming around with another problem soon enough.
Thank you very much for all the replies!
BTW, now if I could find an easy way to access any of the nodes' data members would be golden.
-
September 26th, 2010, 09:48 PM
#19
Re: Call Array Member in function Help
Congratulations you got it working! It's still a little weird though, but I think that's not your fault, it's rather inherent in that assignment.
 Originally Posted by M2W
If you see an obvious error, please let me know [...].
Ok, you explicitly asked for it... 
You are still leaking memory. There's no single delete in the entire program.
BTW, now if I could find an easy way to access any of the nodes' data members would be golden.
Maybe your'e lucky you didn't try: They're all uninitializad! They do no harm in that condition as long as you don't access them, but it's bad style anyway. Aside from that: You would access them just like any other members, using the -> or . operator.
And I still think the ubiquotious (at least they were some years ago) factorial and Fibonacci sequences are still better examples of recursion. They don't use linked lists though...
Good luck in your further process of learning C++.
-
September 26th, 2010, 10:51 PM
#20
Re: Call Array Member in function Help
Thanks buddy.
This was only part of the problem. But it was the biggest one for me by far since I am really behind on my C++. I am now much further ahead in my project thanks to this thread. Good information. Now, my example has function_two, so it implies there must be a function_one somewhere. In fact there are 2 more such recursive functions (that are all working together so far thanks to my sketchy code ha ha.. Man, I should have joined this forum earlier).
There is also a pseudo-random number generator function (a lot easier to work through since that's my background).
Oh, and my question about accessing them goes more like this: How can I access, say Node 3 directly and change its data member? You know, like if you had an array, you would just say Array[3].data = 10 or something like that.
Thanks!
BTW, Where would I stick the delete to free the memory? Right before my function_two ends or somewhere in main? I'm thinking the former.
BTW2, You are right about the weird nature of the solution. I think it's either the way academia works, or my inexperience in programming. Maybe both
Last edited by M2W; September 26th, 2010 at 10:55 PM.
-
September 26th, 2010, 11:32 PM
#21
Re: Call Array Member in function Help
 Originally Posted by M2W
There is also a pseudo-random number generator function (a lot easier to work through since that's my background).
Now these always look like kind of (black?) magic to me. I always was happy when I had ready-made ones at hand, and particularly appreciate the PRNG functionality offered by TR1.
Oh, and my question about accessing them goes more like this: How can I access, say Node 3 directly and change its data member? You know, like if you had an array, you would just say Array[3].data = 10 or something like that.
Well, the lack of random access capabilities is one of the key properties of a linked list. (This downside is compensated by certain advatages however.) If you only need direct access to a relatively small number of certain elements (and that preferably frequently), it might pay to put aside some separate direct pointers to these nodes. If you need direct access to a lot of nodes and/or not always the same ones... 
BTW, Where would I stick the delete to free the memory? Right before my function_two ends or somewhere in main? I'm thinking the former.
Definitely main() would be the right place to do that, in particular right after leaving the while loop. And that's for two reasons: 1) As main() is also responsible for allocation of the nodes (indirectly by calling insert_node()) it is the natural choice. 2) If you actually wanted to do that in function_two(), it would be unnecessairily complicated to determine when to do that, as function_two() gets called (and returns) over and over, either from main() or recursively. And of course it wouldn't be a single delete, it would be one corresponding to each call to new, i.e. node.
Last edited by Eri523; September 27th, 2010 at 09:22 AM.
-
September 27th, 2010, 07:22 AM
#22
Re: Call Array Member in function Help
Can you give me an example of how I would add a pointer to say Node 2, and how I would access it directly? If possible of course.
Thank you.
-
September 27th, 2010, 08:20 AM
#23
Re: Call Array Member in function Help
 Originally Posted by M2W
Can you give me an example of how I would add a pointer to say Node 2, and how I would access it directly? If possible of course.
Thank you.
As already stated, a linked list is not random access, which means you can't just do "get node 2". If you need node 2, then you should consider using another container.
Now, if you must use a linked list, and you must access node 2, Then start at node 0, get node 1, then get node 2.
Code:
int node_number= 0;
int desired_node_number = 2;
const node* current_node = head_node
while (node_number != desired_node_number &&
current_node.next_node != 0)
{
current_node = current_node.next_node;
++node_number;
}
if (node_number == desired_node_number)
{
//node number 2 is in current_node
}
else
{
//list is too small
}
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
September 27th, 2010, 08:47 AM
#24
Re: Call Array Member in function Help
I understand. Thanks for the info.
So if I want some specific node, I can just go in order (like iterating) until I get there and once there, modify the contents. It makes sense. And since I have a small number of Nodes (like 10 in my real code) what you suggest seems like the best (and only) way to do it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|