Sum in the linked list

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 56
Reputation: complexcodes is an unknown quantity at this point 
Solved Threads: 1
complexcodes complexcodes is online now Online
Junior Poster in Training

Sum in the linked list

 
0
  #1
Apr 30th, 2008
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 44
Reputation: rje7 is an unknown quantity at this point 
Solved Threads: 3
rje7 rje7 is offline Offline
Light Poster

Re: Sum in the linked list

 
0
  #2
Apr 30th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 56
Reputation: complexcodes is an unknown quantity at this point 
Solved Threads: 1
complexcodes complexcodes is online now Online
Junior Poster in Training

Re: Sum in the linked list

 
0
  #3
Apr 30th, 2008
I came up with this codes here :-

  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!
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 44
Reputation: rje7 is an unknown quantity at this point 
Solved Threads: 3
rje7 rje7 is offline Offline
Light Poster

Re: Sum in the linked list

 
0
  #4
Apr 30th, 2008
Originally Posted by complexcodes View Post
I came up with this codes here :-

  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 56
Reputation: complexcodes is an unknown quantity at this point 
Solved Threads: 1
complexcodes complexcodes is online now Online
Junior Poster in Training

Re: Sum in the linked list

 
0
  #5
Apr 30th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 248
Reputation: hammerhead is an unknown quantity at this point 
Solved Threads: 24
hammerhead's Avatar
hammerhead hammerhead is offline Offline
Posting Whiz in Training

Re: Sum in the linked list

 
1
  #6
Apr 30th, 2008
Here are a few errors I can see
  1. int List::sumOfNodes();
remove the ';' at the end.

  1. while( currentPtr ! = null)

null to be written as NULL

  1. sum = sum + currentPtr;
currentPter is of type Listnode not int. You will have to do something like
  1. sum=sum+currentPtr->value;

also the function must return a value.
There are 10 types of people in the world, those who understand binary and those who don't.

All generalizations are wrong. Even this one.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 56
Reputation: complexcodes is an unknown quantity at this point 
Solved Threads: 1
complexcodes complexcodes is online now Online
Junior Poster in Training

Re: Sum in the linked list

 
0
  #7
Apr 30th, 2008
here is my updated one but still giving me an error :-
  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. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 44
Reputation: rje7 is an unknown quantity at this point 
Solved Threads: 3
rje7 rje7 is offline Offline
Light Poster

Re: Sum in the linked list

 
0
  #8
Apr 30th, 2008
Originally Posted by complexcodes View Post
here is my updated one but still giving me an error :-
  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

  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 56
Reputation: complexcodes is an unknown quantity at this point 
Solved Threads: 1
complexcodes complexcodes is online now Online
Junior Poster in Training

Re: Sum in the linked list

 
0
  #9
Apr 30th, 2008
I can use same idea to get the average as well?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 44
Reputation: rje7 is an unknown quantity at this point 
Solved Threads: 3
rje7 rje7 is offline Offline
Light Poster

Re: Sum in the linked list

 
0
  #10
Apr 30th, 2008
Originally Posted by complexcodes View Post
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;

Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC