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.
#include<iostream>
#include<cstring>
usingnamespace 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).
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:
#include<iostream>
#include<cstring>
usingnamespace 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...
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?
#include<iostream>
#include<cstring>
usingnamespace std;
int main()
{
int number, digit[5], i=5, count=0;
longint 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
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..
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?
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...