473,397 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Drive me crazy...About plus calculation by array

#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
5 1613
Where on earth does it error??
Nov 16 '08 #2
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: jenny | last post by:
Hi, I am trying to find a VB way that would create a folder on all existing drives - the folder name would be the same on each drive. ie c:\backup, d:\backup, etc. But the folders would only be...
17
by: HALLES | last post by:
HELLO ! I seek to use a form too fill a file without putting a server on my computer. I am no C# programmer, i can do some things in Javascript but IT CAN T WRITE REWRITE APPEND A FILE ON LOCAL...
24
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
34
by: kevin.watters | last post by:
Hi all, I have a need for a short program: Given a drive letter, it would recursively search through all directories, "generating" each filename that it encounters (need to pass each filename...
32
by: Chumbo | last post by:
If I have this(please bear with me - this is a case of a former java guy going back to C ;-) int main () { char *tokconvert(char*); char str = "dot.delimited.str"; char *result; result =...
8
by: Islam Elkhayat | last post by:
I need help plzzzz... In my windows application i fill listview from simple access database... here is the code //Creating colums InitializeComponent();...
16
by: Computer geek | last post by:
Hello, I am new to VB.NET and programming in general. I have taught myself a lot of the basics with vb.net but am still quite the novice. I am working on a little application now and I need some...
18
by: Lie | last post by:
I'm very surprised actually, to see that Python rejected the use of fractional/rational numbers. However, when I read the PEP, I know exactly why the proposal was rejected: People compared...
135
by: robinsiebler | last post by:
I've never had any call to use floating point numbers and now that I want to, I can't! *** Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) on win32. *** 0.29999999999999999 0.29999999999999999
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.