Right now you're mixing your data structure (linked list) with what you intend to use it for. Consider splitting your logic into.
Right now you're mixing your data structure (linked list) with what you intend to use it for. Consider splitting your logic into: Your I/O code. The linked list implementation.
A function that takes a linked list, and computes the average.
I'm a bit unsure what you mean by splitting the logic... – Brandon Dec 11 '10 at 7:54 Right now, whenever you add a node you are also adding the value of that node to the total (in move_current_to_end by proxy of add_node). Instead, add_node should only add the node to the list - the actual total/length calculation should be done after everything has been added to the list. See Jonathan's answer for accomplishing the latter part.
– Paul Dec 11 '10 at 7:58 hm... I can't use a for loop in this assignment though... I have to work with what I've got there... EDIT: didn't realize that enter submitted, anyways, I need to have the thing calculate the total and number of nodes while it's traversing. – Brandon Dec 11 '10 at 8:10 Then use recursion instead. – Paul Dec 11 '10 at 8:13 I don't know what you mean by recursion.
– Brandon Dec 11 '10 at 8:26.
You've got a lot of other stuff there and you didn't say what your code does, but I'd do something like this (untested): int count = 0; int total = 0; for (ptr = head_ptr; ptr! = NULL; ptr = ptr->next) { total += ptr->number; count++; }.
I forgot to mention that I need to "traverse" the list in order to total the numbers entered and count the number of nodes in the list. Thus, my big dilemma... – Brandon Dec 11 '10 at 7:50 1 I don't understand. My code does traverse the list, it does count the number of nodes, and it also adds the sum -?
– Jonathan Wood Dec 11 '10 at 8:09 but I can't use a for loop, I have to use what I've got – Brandon Dec 11 '10 at 8:12 According to you, what you've got isn't working. So I assumed you needed to write it differently. Your version is overly complex.
Unfortunately, so is your description. As I solve one problem, you raise another. – Jonathan Wood Dec 11 '10 at 8:14 I just have to have it figure out the total and and # of nodes using those functions.
– Brandon Dec 11 '10 at 8:16.
I know this won't help you with your homework, but here is a C++ STL program that satisfies your requirements: As many inputs as the user desires Numbers are stored in a linked list Numbers are added up Calculates and displays average Good luck with your class. #include #include #include #include #include int main() { std::list l; std::copy(std::istream_iterator(std::cin), std::istream_iterator(), std::insert_iterator(l, l.begin())); size_t size = l.size(); if(size) std::cout.
Apologies: would have attached a comment to ask this introductory question. But apparently you need a higher rep than I currently have to do so. @Brandon.
Can I get you to clearly state that it is these functions: int get_number_data(int &number) void add_node(int &number) void move_current_to_end() void display_avg() and only these that you are allowed to use? (And I quote you: "I just have to have it figure out the total and and # of nodes using those functions" If so. Why?
Have they been specified by your lecturer?
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.