Connecting Tech Pros Worldwide Forums | Help | Site Map

Drive me crazy...About plus calculation by array

upyzl
Guest
 
Posts: n/a
#1: Nov 16 '08
#include <stdio.h>
#define digit 21

int main()
{
int a, b, max;
int i = 0;
int j = 0;
int x[digit - 1] = { 0 };
int y[digit - 1] = { 0 };
int z[digit] = { 0 };//result

printf("Input the first number:\n");
scanf("%d", &a);
printf("Input the second number:\n");
scanf("%d", &b);

while ( a != 0 ) {
x[i] = a % 10;
i++;
a /= 10;
}
while ( b != 0 ) {
y[j] = b % 10;
j++;
b /= 10;
}

max = i;
if ( j i ) {
max = j;
}

for ( ; max >= 0; max--) {
z[max] = x[max] + y[max];
if ( z[max] >= 10 ) {
z[max+1]++;
z[max] -= 10;
}
printf("%d", z[max]);
}
printf("\n");
return 0;
}

upyzl
Guest
 
Posts: n/a
#2: Nov 16 '08

re: Drive me crazy...About plus calculation by array


Where on earth does it error??
Eric Sosman
Guest
 
Posts: n/a
#3: Nov 16 '08

re: Drive me crazy...About plus calculation by array


upyzl wrote:
Quote:
#include <stdio.h>
[... code snipped; see up-thread ...]
It would be a good idea to explain what your code is
supposed to do (we can only see what it actually does), and
how what it actually does fails to meet your purposes. Do
you take a vow of silence before you visit your doctor?

Still, I can see at least one thing that looks wrong.
You can probably discover it for yourself by working through
a small example with pencil and paper: Write down the values
of the program's variables and start following through the
code step by step, as if you were the computer (this can be
a surprisingly effective way to discover mistakes). An
example that highlights the flaw I spotted is to try adding
181 and 19.

(A hint for future revisions: When adding the numbers,
you may find it easier to start at the ones' place and work
upward than to start at the topmost digit and work down.)

--
Eric Sosman
esosman@ieee-dot-org.invalid
upyzl
Guest
 
Posts: n/a
#4: Nov 17 '08

re: Drive me crazy...About plus calculation by array


On 11月16日, 下午10时37分, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
Quote:
upyzl wrote:
Quote:
#include <stdio.h>
[... code snipped; see up-thread ...]
>
It would be a good idea to explain what your code is
supposed to do (we can only see what it actually does), and
how what it actually does fails to meet your purposes. Do
you take a vow of silence before you visit your doctor?
>
Still, I can see at least one thing that looks wrong.
You can probably discover it for yourself by working through
a small example with pencil and paper: Write down the values
of the program's variables and start following through the
code step by step, as if you were the computer (this can be
a surprisingly effective way to discover mistakes). An
example that highlights the flaw I spotted is to try adding
181 and 19.
>
(A hint for future revisions: When adding the numbers,
you may find it easier to start at the ones' place and work
upward than to start at the topmost digit and work down.)
>
--
Eric Sosman
esos...@ieee-dot-org.invalid
I mean, I just want to do a plus calculation, but numbers' digits are
over 10(such as 20)
Barry Schwarz
Guest
 
Posts: n/a
#5: Nov 18 '08

re: Drive me crazy...About plus calculation by array


On Mon, 17 Nov 2008 04:52:41 -0800 (PST), upyzl <zj262144@163.com>
wrote:
Quote:
>On 11??16??, ????10??37??, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
Quote:
>upyzl wrote:
Quote:
#include <stdio.h>
[... code snipped; see up-thread ...]
>>
> It would be a good idea to explain what your code is
>supposed to do (we can only see what it actually does), and
>how what it actually does fails to meet your purposes. Do
>you take a vow of silence before you visit your doctor?
>>
> Still, I can see at least one thing that looks wrong.
>You can probably discover it for yourself by working through
>a small example with pencil and paper: Write down the values
>of the program's variables and start following through the
>code step by step, as if you were the computer (this can be
>a surprisingly effective way to discover mistakes). An
>example that highlights the flaw I spotted is to try adding
>181 and 19.
>>
> (A hint for future revisions: When adding the numbers,
>you may find it easier to start at the ones' place and work
>upward than to start at the topmost digit and work down.)
>>
>--
>Eric Sosman
>esos...@ieee-dot-org.invalid
>
>I mean, I just want to do a plus calculation, but numbers' digits are
>over 10(such as 20)
We understand that. Eric gave you a very good method to find the
error in your logic. If you do what he suggested, it should be
obvious where you need to adjust your code.

--
Remove del for email
CBFalconer
Guest
 
Posts: n/a
#6: Nov 19 '08

re: Drive me crazy...About plus calculation by array


Barry Schwarz wrote:
Quote:
upyzl <zj262144@163.comwrote:
Quote:
>Eric Sosman <esos...@ieee-dot-org.invalidwrote:
>>
.... snip ...
Quote:
Quote:
>>
Quote:
>> (A hint for future revisions: When adding the numbers,
>>you may find it easier to start at the ones' place and work
>>upward than to start at the topmost digit and work down.)
>>>
>>-- <
>>Eric Sosman <
>>esos...@ieee-dot-org.invalid <
^^^^^ above is a signature ^^^^'
Quote:
Quote:
>>
>I mean, I just want to do a plus calculation, but numbers' digits
>are over 10(such as 20)
>
We understand that. Eric gave you a very good method to find the
error in your logic. If you do what he suggested, it should be
obvious where you need to adjust your code.
And the simple technique of examining your reply before hitting
'send' will expose the fact that you have failed to purge
signatures. Those are everything following the "__ " alone marker.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Closed Thread