Count digits (1,3,5) and 2,4,6) in Array

Hi there,

question is to enter a six-digit number, which is then used to show the sum
of
the second, fourth and sixth digit & the first, third and fifth digit respectively.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<iostream>
#include<cstring>

using namespace std;

int main()
{

	int number, l, digit[10], i=0, count, sum1=0, sum2=0;

	cin>>number;

	count=0;

		while(number>0)
		{
		digit[i] = number%10;
		number=number/10;
		i++;
		count++;
		}

			for(i=0;i<count;i++)
			{
			cout<<digit[i]<<"\t";
			}
//Here is problem...
sum1=digit[1,3,5];
sum2=digit[2,4,6];

cout<<sum1<<" Sum of 1,3,5\n";
cout<<sum2<<" Sum of 2,4,6\n";

return 0;
}


It seems the numbers are not initialized properly. My question is: How to tell to calculate sum of the position in the I specify (format for this would be appreciated).

Regards
Sternenfisch
Your first problem is in the algorithm. You're storing the number's reverse in the array. For example, if I entered 123456, the number stored in the array is 654321. As 6 (number of digits) is even, the odd and even places get interchanged. Reverse the array to have the original number...

Secondly, what is line 28 and 29? As far as I know, that is not standard syntax. Use this:
1
2
sum1=digit[0]+digit[2]+digit[4];
sum2=digit[1]+digit[3]+digit[5];


Or you could use something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<iostream>
#include<cstring>

using namespace std;

int main()
{

	int number, digit[6], i=5, count=0, sum1=0, sum2=0;

	cin>>number;

	while(number>0)
	{
		digit[i] = number%10;
		number=number/10;
		i--;
		count++;

		if (i%2==0)
		sum1+=digit[i];
		else
		sum2+=digit[i];
	}

		for(i=0;i<count;i++)
		{
			cout<<digit[i]<<"\t";
		}


	cout<<sum1<<" Sum of 1,3,5\n";
	cout<<sum2<<" Sum of 2,4,6\n";

	return 0;
}


(Just a tad bit memory efficient...)

Also, I feel you should know, if you don't already, that an array's last index is one less than what you define, since the first index is 0. For example, an array as arr[3] has three elements namely arr[0], arr[1] and arr[2].. arr[3] is non-allocated memory...

Hope this helps... :)
Last edited on
Hi Caprico,

somehow that code is messing up the digit[i] upon entering the while loop. I've posted the cleaned up debugging effort (but kept the output on the relevant places).

Could the if(i%2==0) affect the [i] value in such a manner that is messes up the array?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<iostream>
#include<cstring>

using namespace std;

int main()
{

	int number, digit[5], i=5, count=0;
	long int sum1=0, sum2=0;
	cin>>number;



	while(number>0)
	{
		digit[i] = number%10;
		cout<<"Digit: "<<digit[i]<<" \n";
		//18: Digit still intact
		number=number/10;
		
		i--;
				
		count++;
			
			
		if(i%2==0)
		{
		//30: Digit messed up
		cout<<"Digit: "<<digit[i]<<"\n";
		sum1+=digit[i];
		
		}		
		else
		{
		sum2+=digit[i];
		
		}
		
	}

		for(i=0;i<count;i++)
		{
			cout<<digit[i]<<"\t";
		}


	cout<<sum1<<" Sum of 1,3,5\n";
	cout<<sum2<<" Sum of 2,4,6\n";

	return 0;
}


Thanks for telling about the array boundaries. I know that the compiler doesn't check which is the last element of an array. And also I know that machine index starts at 0 and counts from there on.

Sorry if don't immediately get what mean with:

array as arr[3] has three elements namely arr[0], arr[1] and arr[2].. arr[3] is non-allocated memory...

I thought we just gave the machine an array of arr[6], wouldn't that mean it just got 5 elements to fill them with derived integers?

And no, setting the arr[7] doesn't work. Still the digits gone bonkers :D

Regs
Last edited on
Found my mistake: Line 17 (in my post) is misplaced.. It should be after the else. Sorry! :) Its what was causing the digits to go "bonkers"..
Found your mistake as well: Line 9 of your code... it should be digit[6], not 5... Else you'll have an error/warning as "array index out of bounds" or something..

Okay, umm, to explain the array part.. if you declare an array as arr[6], it still has six elements.. arr[0] is the first, arr[1] is the second...... arr[5] is the sixth..

Is that simpler?
Working now, the i decrement was something that didn't really make sense to me in the first place. Now I know why, it was just not in the right place.

I understand the array part. To given an example = in the arr[6] example, we could put values for arr[0],arr[1],a[...] till arr[6] (all together seven elements in this array).
However trying to assign an additional value arr[7] or arr[i+1] after the i value has been incremented 7 times from 0 to 6 will cause an boundary error.

In complex programs however, that may not become immediately apparent, as boundary checking is not done automatically by the compiler. Thats our job, right?

Its working now, thanks.
Ahh, you still didn't get it completely right... arr[6] still has six elements only. arr[6] is non-allocated memory. You can't store anything in arr[6]... arr[5] is the last memory location of the array.
And yes, boundary checking is trusted upon the programmer...

Glad I could help... :)
Topic archived. No new replies allowed.