3 Replies - 364 Views - Last Post: 25 November 2012 - 04:18 AM Rate Topic: -----

#1 user12345  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 25-November 12

Problem in finding the sum of integer digits

Posted 25 November 2012 - 02:16 AM

I have tried to get the sum of integer digits but i only get the sum of 12345 right. any other integer i enter, it gives the wrong sum. Please tell me where i am going wrong

my code is

#include <iostream>
#include <stdlib.h>
using namespace std;
void sumdigits(int );
void sumdigits(int )
{
	int a;
	int b[5];
	int sum=0;
	cout<<"Fill array with integer digits"<<endl;
	cin>>b[5];
	for ( a = 0 ; a < 6 ; a++)
{
  //Enter the integer values one by one to store in array
 sum = sum + b [5]; 
}
	cout<<"The sum of the digits is: "<<sum<<endl;
}
void main ()
{
int integer;
cout<<"Enter integer: ";
cin >> integer;
sumdigits(integer);

}


This post has been edited by JackOfAllTrades: 25 November 2012 - 03:54 AM
Reason for edit:: Fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Problem in finding the sum of integer digits

#2 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 837
  • View blog
  • Posts: 2,460
  • Joined: 20-March 10

Re: Problem in finding the sum of integer digits

Posted 25 November 2012 - 02:38 AM

hi,

this line

sum = sum + b [5];
should be

sum = sum + b [a]; 


Please use code tags [ code] [ \code]

Snoopy
Was This Post Helpful? 1
  • +
  • -

#3 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: Problem in finding the sum of integer digits

Posted 25 November 2012 - 02:39 AM

Well! Lots of things to point out...
First, why you are passing an integer value to the sumdigits() which is never used in it...

Your array contains 5 elements, and you are counting 6, a should go till 4, so a < 5:
for ( a = 0 ; a < 6 ; a++)



You are just adding some garbage value here, instead of the array index:
sum = sum + b [5];

The final index of the array is 4, 0-4, in order to add all the elements use the array index, b[a]....

Hope this Helps!

PS: And one thing real serious, cin>>b[5];...
You are thinking that this read 5 inputs in the array, but it doesn't...

You are accessing a location that is out of the array elements, a location out of bounds...

Even if you do this a[4], you will only read in the 5th location of the array, and not the whole...

So, just use a loop from 0-4 and take input on each index...

Edit: You have to read more about arrays here and here...

This post has been edited by AKMafia001: 25 November 2012 - 02:55 AM

Was This Post Helpful? 1
  • +
  • -

#4 user12345  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 25-November 12

Re: Problem in finding the sum of integer digits

Posted 25 November 2012 - 04:18 AM

Oh i got it now. Thanks a lot.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1