473,561 Members | 3,852 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help!!

I'm new to c and i've a problem with arrays. I want to sum two arrays
into a third array like this:
array a: 5 6 8 9 1 2 4 7 3 8
array b: 5 2 1 2 4 6 8 5 4 3
sum: 1 0 9 0 1 5 9 3 2 8 1
I tried loop but i've failed. Can anyone help me?

Feb 19 '06 #1
21 1492

"siya" <ay*********@gm ail.com> wrote in message
news:11******** *************@g 47g2000cwa.goog legroups.com...
I'm new to c and i've a problem with arrays. I want to sum two arrays
into a third array like this:
array a: 5 6 8 9 1 2 4 7 3 8
array b: 5 2 1 2 4 6 8 5 4 3
sum: 1 0 9 0 1 5 9 3 2 8 1
I tried loop but i've failed. Can anyone help me?


Show us what you tried. Tell exactly what you wanted the
program to do, and what it does instead. If you got
errors from the compiler, include them as well, and
indicate to which lines they refer.

-Mike
Feb 19 '06 #2
I've tried this:
#include <stdio.h>
main(){
int i,a[10],b[10],c[10];
int temp;int carry;
for(i=1;i<=10;i ++){
printf("a(%d)=" ,i);
scanf("%d",&a[i]);
}
printf("\n");
for(i=1;i<=10;i ++){
printf("b(%d)=" ,i);
scanf("%d",&b[i]);
}
printf("\n");
temp = a[i] + b[i];
for(i=1;i<=10;i ++){
if ( temp >=10 ){
carry = (a[i] + b[i] + carry) / 10;
temp = temp %10;
c[i] = temp;
printf("c(%d)=% d\n",i,c[i]);
}
else{
c[i] = a[i] + b[i];
printf("%d", c[i]);
}
}
return 0;
}
It first prints a[1],a[2]... after printing a[10] it writes the one
added to value of a[10] as index of a like this: a[9] =
3,a[10]=5,a[6]=...
i want to add two arrays if the sum is bigger than 10, write the carry
to left and rest to sum.
Mike Wahler wrote:
"siya" <ay*********@gm ail.com> wrote in message
news:11******** *************@g 47g2000cwa.goog legroups.com...
I'm new to c and i've a problem with arrays. I want to sum two arrays
into a third array like this:
array a: 5 6 8 9 1 2 4 7 3 8
array b: 5 2 1 2 4 6 8 5 4 3
sum: 1 0 9 0 1 5 9 3 2 8 1
I tried loop but i've failed. Can anyone help me?


Show us what you tried. Tell exactly what you wanted the
program to do, and what it does instead. If you got
errors from the compiler, include them as well, and
indicate to which lines they refer.

-Mike


Feb 19 '06 #3
"siya" writes:
I've tried this:
#include <stdio.h>
main(){
int i,a[10],b[10],c[10];
int temp;int carry;
for(i=1;i<=10;i ++){
printf("a(%d)=" ,i);
scanf("%d",&a[i]);
}

<snip>

I think it is less confusing if you use a function to do the addition. This
works, uses \\ comments but my compiler accepts them. Note I added a
leading zero to the operands to force the carry to be returned.

---------------------------------------
#include <stdio.h>

#define N 11 // number of digits in operand

int add(int a, int b)
{
static carry; // will be initialized to 0
int sum = a + b + carry;
if(sum>9)
{
sum -=10;
carry = 1;
}
else
carry = 0;
return sum;
}
//=============== ==
int main()
{
int a[N] = {0,5,6,8,9,1,2, 4,7,3,8};
int b[N] = {0,5,2,1,2,4,6, 8,5,4,3};
int sum[N+1]; // allow for carry digit
int i;

for(i=N-1; i>=0; i--)
sum[i] = add(a[i], b[i]);

for(i=0; i<N; i++)
printf("%d", sum[i]);
printf("%\nDone \n");
getchar();
}


Feb 19 '06 #4
thank you it works alot:)

Feb 19 '06 #5
siya wrote:

I'm new to c and i've a problem with arrays. I want to sum two arrays
into a third array like this:
array a: 5 6 8 9 1 2 4 7 3 8
array b: 5 2 1 2 4 6 8 5 4 3
sum: 1 0 9 0 1 5 9 3 2 8 1
I tried loop but i've failed. Can anyone help me?


/* BEGIN new.c output */

array a: 5689124738
array b: 5212468543
sum: 10901593281

/* END new.c output */

/* BEGIN new.c */

#include <stdio.h>
#include <string.h>

char *str_rev(char *s);

int main(void)
{
char a[] = "5689124738 ";
char b[] = "5212468543 ";
char sum[1 + sizeof a];
unsigned carry;
size_t index;

str_rev(a);
str_rev(b);
for (index = carry = 0; index != sizeof a - 1; ++index) {
carry += a[index] - '0' + b[index] - '0';
sum[index] = (char)('0' + carry % 10);
carry = carry > 9;
}
strcpy(sum + index, carry != 0 ? "1" : "");
str_rev(a);
str_rev(b);
str_rev(sum);
puts("\n/* BEGIN new.c output */\n");
printf("array a: %11s\n", a);
printf("array b: %11s\n", b);
printf("sum: %11s\n", sum);
puts("\n/* END new.c output */");
return 0;
}

char *str_rev(char *s)
{
char *t, swap;
char *const p = s;

if (*s != '\0') {
t = s + strlen(s + 1);
while (t > s) {
swap = *t;
*t-- = *s;
*s++ = swap;
}
}
return p;
}

/* END new.c */
--
pete
Feb 19 '06 #6
siya wrote:
I'm new to c and i've a problem with arrays. I want to sum two arrays
into a third array like this:
array a: 5 6 8 9 1 2 4 7 3 8
array b: 5 2 1 2 4 6 8 5 4 3
sum: 1 0 9 0 1 5 9 3 2 8 1
I tried loop but i've failed. Can anyone help me?


How about

for(i=0; i<n; i++)
sum[i]=array_a[i]+array_b[i];

(untested)
Best regards / Med venlig hilsen
Martin Jørgense

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Feb 19 '06 #7
pete wrote:
char a[] = "5689124738 ";
char b[] = "5212468543 "; unsigned carry; carry += a[index] - '0' + b[index] - '0';


That should be
carry = carry + a[index] - '0' + b[index] - '0';
instead.

If b[index] equals (INT_MAX - 2)
and a[index] is greater than ('2'),
then
carry += a[index] - '0' + b[index] - '0';
is undefined.

--
pete
Feb 19 '06 #8
Martin Jørgensen <un*********@sp am.jay.net> writes:
siya wrote:
I'm new to c and i've a problem with arrays. I want to sum two arrays
into a third array like this:
array a: 5 6 8 9 1 2 4 7 3 8
array b: 5 2 1 2 4 6 8 5 4 3
sum: 1 0 9 0 1 5 9 3 2 8 1
I tried loop but i've failed. Can anyone help me?


How about

for(i=0; i<n; i++)
sum[i]=array_a[i]+array_b[i];

(untested)


That's inconsistent with the OP's specified output. Though he doesn't
say so, he seems to be treating the arrays as arrays of digits
representing large integer values; thus, looking at just the last two
digits, (3, 8) + (4, 3) yields (8, 1), not (7, 11).

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 19 '06 #9
pete wrote:
pete wrote:
char a[] = "5689124738 ";
char b[] = "5212468543 ";
unsigned carry;

carry += a[index] - '0' + b[index] - '0';


That should be
carry = carry + a[index] - '0' + b[index] - '0';
instead.

If b[index] equals (INT_MAX - 2)
and a[index] is greater than ('2'),
then
carry += a[index] - '0' + b[index] - '0';
is undefined.


That's an interesting non-equivalence! I would have chosen the more
pedestrian rewrite:
carry += (a[index]-'0') + (b[index]-'0');

--
Thad
Feb 19 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
6503
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help Workshop program: hcw.exe that's included with Visual Basic. This exact same file compiled perfectly with no notes, warnings or errors prior to...
9
4390
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with the microsoft HTML workshop utility, lets call it c:\path\help.chm. My question is how do you launch it from the GUI? What logic do I put behind...
6
4314
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing result in any way. Who can help me, I thank you very very much. list.cpp(main program)...
3
3333
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With numarray, help gives unhelpful responses:
7
5350
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting list. But there isn't any information available from clicking on many of the available topics (mostly methods but some properties are also unavailable). This same problem occurs with many, if not...
5
3253
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to access this help file during his development time using "F1" help within the VB IDE. Is this expectation achievable In trying to test my help file in the IDE, I have a solution with 2 projects:...
8
3210
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both including search the internet for help, but the help is worthless. Any ideas?
10
3342
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably the worst I ever seen. I almost cannot find anything I need, including things I
1
6115
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve default property of object Label. Click for more:...
0
2861
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application calls MS Excel, so the scenario is that I am supposed to see the Excel Menu bar, FILE EDIT VIEW INSERT ... HELP. I am able to see the menu bar, but in...
0
7639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7562
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7855
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8074
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7608
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6198
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5465
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5181
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3594
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.