943,812 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3021
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 30th, 2008
0

Sum in the linked list

Expand Post »
Can anyone please help me to find the sum in the linked list. I have written codes to insert and remove numbers from lists but I have no idea how to calculate the sum.

C++ Syntax (Toggle Plain Text)
  1. void List::insert( const double &value )
  2. {
  3. ListNode *newPtr = getNewNode( value );
  4.  
  5. if ( isEmpty() )
  6. firstPtr = lastPtr = newPtr;
  7. else
  8. {
  9. newPtr->nextPtr = firstPtr;
  10. firstPtr = newPtr;
  11. }
  12. }
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
complexcodes is offline Offline
57 posts
since Mar 2008
Apr 30th, 2008
0

Re: Sum in the linked list

iterate through the list until the end of the list.
declare a variable called SUM then add the numbers from each node during the iteration to the variable sum.
Reputation Points: 12
Solved Threads: 9
Junior Poster in Training
rje7 is offline Offline
83 posts
since Mar 2008
Apr 30th, 2008
0

Re: Sum in the linked list

I came up with this codes here :-

C++ Syntax (Toggle Plain Text)
  1. int List::sumOfNodes();
  2. {
  3. int sum = 0;
  4. Listnode *currentPtr = firstPtr;
  5. while( currentPtr ! = null)
  6. {
  7. sum = sum + currentPtr;
  8. currentPtr = currentPtr->nextPtr;
  9. }
  10. cout<<"The sum is "<<sum<<endl;
  11. }

comipler is showing me errors!
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
complexcodes is offline Offline
57 posts
since Mar 2008
Apr 30th, 2008
0

Re: Sum in the linked list

I came up with this codes here :-

C++ Syntax (Toggle Plain Text)
  1. int List::sumOfNodes();
  2. {
  3. int sum = 0;
  4. Listnode *currentPtr = firstPtr;
  5. while( currentPtr ! = null)
  6. {
  7. sum = sum + currentPtr;
  8. currentPtr = currentPtr->nextPtr;
  9. }
  10. cout<<"The sum is "<<sum<<endl;
  11. }

comipler is showing me errors!
copy paste the full code.. lets have a look.
and paste the error messages too if possible.
Reputation Points: 12
Solved Threads: 9
Junior Poster in Training
rje7 is offline Offline
83 posts
since Mar 2008
Apr 30th, 2008
0

Re: Sum in the linked list

well that was my function and I got this error message

1>c:\users\owner\desktop\new folder\list.h(173) : error C2761: 'int List::sumOfNodes(void)' : member function redeclaration not allowed
1>c:\users\owner\desktop\new folder\list.h(174) : error C2447: '{' : missing function header (old-style formal list?)


I called this method in driver :_

listObject.sumOfNodes();

listObject is an object of type List.

am I doing something wrong in function?
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
complexcodes is offline Offline
57 posts
since Mar 2008
Apr 30th, 2008
1

Re: Sum in the linked list

Here are a few errors I can see
C++ Syntax (Toggle Plain Text)
  1. int List::sumOfNodes();
remove the ';' at the end.

C++ Syntax (Toggle Plain Text)
  1. while( currentPtr ! = null)

null to be written as NULL

C++ Syntax (Toggle Plain Text)
  1. sum = sum + currentPtr;
currentPter is of type Listnode not int. You will have to do something like
C++ Syntax (Toggle Plain Text)
  1. sum=sum+currentPtr->value;

also the function must return a value.
Reputation Points: 46
Solved Threads: 24
Posting Whiz in Training
hammerhead is offline Offline
248 posts
since May 2006
Apr 30th, 2008
0

Re: Sum in the linked list

here is my updated one but still giving me an error :-
C++ Syntax (Toggle Plain Text)
  1. int List::sumOfNodes()
  2. {
  3. int sum = 0;
  4. ListNode *currentPtr = firstPtr;
  5. while( currentPtr != NULL)
  6. {
  7. sum = sum + currentPtr;
  8. currentPtr = currentPtr->nextPtr;
  9. }
  10. return sum;
  11. cout<<"The sum is "<<sum<<endl;
  12. }
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
complexcodes is offline Offline
57 posts
since Mar 2008
Apr 30th, 2008
0

Re: Sum in the linked list

here is my updated one but still giving me an error :-
C++ Syntax (Toggle Plain Text)
  1. int List::sumOfNodes()
  2. {
  3. int sum = 0;
  4. ListNode *currentPtr = firstPtr;
  5. while( currentPtr != NULL)
  6. {
  7. sum = sum + currentPtr;
  8. currentPtr = currentPtr->nextPtr;
  9. }
  10. return sum;
  11. cout<<"The sum is "<<sum<<endl;
  12. }

sum = sum + currentPtr;
should be

C++ Syntax (Toggle Plain Text)
  1.  
  2. sum=sum+currentPtr->value; //value=the variable you are using to store the numbers.
sum and currentPtr are of different types.
sum is int and currentPtr->value is an int. but currentPtr is of type ListNode.


after the return statement is encountered the codes after that are inaccessible.
swap the return and cout statements.
Last edited by rje7; Apr 30th, 2008 at 4:01 am.
Reputation Points: 12
Solved Threads: 9
Junior Poster in Training
rje7 is offline Offline
83 posts
since Mar 2008
Apr 30th, 2008
0

Re: Sum in the linked list

I can use same idea to get the average as well?
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
complexcodes is offline Offline
57 posts
since Mar 2008
Apr 30th, 2008
0

Re: Sum in the linked list

I can use same idea to get the average as well?
hmm..
use another variable called int COUNT to count the number of iterations through the list. that will give the number of nodes.

then float avg=sum/COUNT;

Reputation Points: 12
Solved Threads: 9
Junior Poster in Training
rje7 is offline Offline
83 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: About Network programing?
Next Thread in C++ Forum Timeline: how do I?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC