Drive me crazy...About plus calculation by array

U

upyzl

#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 = 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;
}
 
E

Eric Sosman

upyzl said:
#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.)
 
M

Martien Verbruggen

If this is homework, please don't just copy and hand it in. It's not
that hard to work through this program step by step, either by hand, or
by using a debugger, to find out what it's doing, and where it does
things wrong. Also, think about the order in which you store digits, and
the order in which you normally perform addition. Once you've done that,
please read on.










#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 = a % 10;
i++;
a /= 10;
}
while ( b != 0 ) {
y[j] = b % 10;
j++;
b /= 10;
}


You store the numbers input in 'reverse' order in the arrays, i.e. most
significant digit last.
max = i;
if ( j > i ) {
max = j;
}

for ( ; max >= 0; max--) {
z[max] = x[max] + y[max];

So, here you process from most significant digit to least significant
digit, which is the wrong order if you want to carry like you do here:
if ( z[max] >= 10 ) {
z[max+1]++;

You've already been at max + 1, so you're never going to see what you
carried. Even if you did, you wouldn't be using it in the above.
z[max] -= 10;
}

Either reverse the x and y numbers , or process them the other way
around, and reverse the result. You also need to make sure that if the
last addition carries, that you need to make your 'max' one larger.
Either way, you need to also fix your addition algorithm. Maybe
something like this:

for (i = 0; i < max; i++) {
z = z + x + y;
if ( z >= 10 ) {
z[i + 1] = 1;
z -= 10;
if (i == max - 1)
max++;
}
}

while (max--)
printf("%d", z[max]);
puts("");

Martien
 
U

upyzl

upyzl said:
#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.)

I mean, I just want to do a plus calculation, but numbers' digits are
over 10(such as 20)
 
U

upyzl

If this is homework, please don't just copy and hand it in. It's not
that hard to work through this program step by step, either by hand, or
by using a debugger, to find out what it's doing, and where it does
things wrong. Also, think about the order in which you store digits, and
the order in which you normally perform addition. Once you've done that,
please read on.

#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 = a % 10;
i++;
a /= 10;
}
while ( b != 0 ) {
y[j] = b % 10;
j++;
b /= 10;
}


You store the numbers input in 'reverse' order in the arrays, i.e. most
significant digit last.
max = i;
if ( j > i ) {
max = j;
}
for ( ; max >= 0; max--) {
z[max] = x[max] + y[max];

So, here you process from most significant digit to least significant
digit, which is the wrong order if you want to carry like you do here:
if ( z[max] >= 10 ) {
z[max+1]++;

You've already been at max + 1, so you're never going to see what you
carried. Even if you did, you wouldn't be using it in the above.
z[max] -= 10;
}

Either reverse the x and y numbers , or process them the other way
around, and reverse the result. You also need to make sure that if the
last addition carries, that you need to make your 'max' one larger.
Either way, you need to also fix your addition algorithm. Maybe
something like this:

for (i = 0; i < max; i++) {
z = z + x + y;
if ( z >= 10 ) {
z[i + 1] = 1;
z -= 10;
if (i == max - 1)
max++;
}
}

while (max--)
printf("%d", z[max]);
puts("");

Martien

--
|
Martien Verbruggen | Blessed are the Fundamentalists, for they
| shall inhibit the earth.
|- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

- ÏÔʾÒýÓõÄÎÄ×Ö -


In fact, this is only a small part of my homework.
But I can deal with others myself.
This part should be written to a function of that homework.
 
B

Barry Schwarz

upyzl said:
#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.)

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.
 
C

CBFalconer

Barry said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top