By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,433 Members | 1,702 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,433 IT Pros & Developers. It's quick & easy.

Drive me crazy...About plus calculation by array

P: n/a
#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;
}
Nov 16 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Where on earth does it error??
Nov 16 '08 #2

P: n/a
upyzl wrote:
#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
es*****@ieee-dot-org.invalid
Nov 16 '08 #3

P: n/a
On 11月16日, 下午10时37分, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
upyzl wrote:
#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)
Nov 17 '08 #4

P: n/a
On Mon, 17 Nov 2008 04:52:41 -0800 (PST), upyzl <zj******@163.com>
wrote:
>On 11??16??, ????10??37??, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
>upyzl wrote:
#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
Nov 18 '08 #5

P: n/a
Barry Schwarz wrote:
upyzl <zj******@163.comwrote:
>Eric Sosman <esos...@ieee-dot-org.invalidwrote:
.... snip ...
>>
>> (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 ^^^^'
>>
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.
Nov 19 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.