471,585 Members | 1,234 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 1359

"siya" <ay*********@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.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*********@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.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*********@spam.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_Keith) 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
osmium wrote:
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();
}
siya wrote: thank you it works alot:)


The best thing you could do is to understand WHY it works and write you
own version. The code given has some subtle problems. One example: the
declaration of carry is compatible with C89, not C99, yet the comments
are supported in C99, not C89. The biggest problem, for me, is the
implied usage and restrictions of add. It works for this test program,
but how should it be used in general? A well-written problem requires
more than giving a correct answer for one test case.

--
Thad
Feb 19 '06 #11
On 2006-02-19, Keith Thompson <ks***@mib.org> wrote:
Martin Jørgensen <un*********@spam.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).


Well, there's another step, which you do after you've done that.
Feb 19 '06 #12
Keith Thompson wrote:
-snip-
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).


Oh, I didn't notice that...
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Feb 19 '06 #13
"Thad Smith" wrote:
The code given has some subtle problems. One example: the declaration of
carry is compatible with C89, not C99, yet the comments are supported in
C99, not C89.


Well, are you going to tell me *why* this is incompatible with C99 or are
you going to let your comment lay there like a stinking turd?
Feb 19 '06 #14
On 2006-02-19, siya <ay*********@gmail.com> 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?


The answer is totally dependant on what is in your arrays.

Are they strings of ascii characters? Arrays of integers?

If they are ascii character arrays then one way might be to just convert
them to doubles (atoi), add them and then sprintf them back into your
destination array.

--
Remove evomer to reply
Feb 19 '06 #15
"osmium" <r1********@comcast.net> writes:
"Thad Smith" wrote:
The code given has some subtle problems. One example: the declaration of
carry is compatible with C89, not C99, yet the comments are supported in
C99, not C89.


Well, are you going to tell me *why* this is incompatible with C99 or are
you going to let your comment lay there like a stinking turd?


The declaration of carry is:

static carry; // will be initialized to 0

C99 doesn't have implicit int. (It's horrid style even in C89.)

--
Keith Thompson (The_Other_Keith) 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 #16
osmium wrote:
"Thad Smith" wrote:
The code given has some subtle problems. One example: the
declaration of carry is compatible with C89, not C99, yet the
comments are supported in C99, not C89.


Well, are you going to tell me *why* this is incompatible with
C99 or are you going to let your comment lay there like a
stinking turd?


Because the declaration of carry uses implicit int. Sloppy.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>

Feb 19 '06 #17
"Keith Thompson" writes:
"osmium" <r1********@comcast.net> writes:

"Thad Smith" wrote:
The code given has some subtle problems. One example: the declaration
of
carry is compatible with C89, not C99, yet the comments are supported in
C99, not C89.


Well, are you going to tell me *why* this is incompatible with C99 or are
you going to let your comment lay there like a stinking turd?


The declaration of carry is:

static carry; // will be initialized to 0

C99 doesn't have implicit int. (It's horrid style even in C89.)


I didn't even know that would work! Simply a typing mistake on my part. Or
something. My compiler didn't whimper., even a little bit, so I was unaware
of what I had typed.
Feb 19 '06 #18
On 2006-02-19, Richard G. Riley <rg***********@gmail.com> wrote:
On 2006-02-19, siya <ay*********@gmail.com> 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?


The answer is totally dependant on what is in your arrays.

Are they strings of ascii characters? Arrays of integers?

If they are ascii character arrays then one way might be to just convert
them to doubles (atoi), add them and then sprintf them back into your
destination array.


This method doesn't scale as well as the other method, whose logical
conclusion is arbitrary-precision arithmetic.
Feb 20 '06 #19
I'm new to c too! This's complex code!

#include "stdio.h"
#define N 10
sum(int a[N],int b[N]) /*------------Sum function-----------*/
{
int i,s=0;
for(i=0;i<N;i++)
{
s=a[i]+b[i];
printf(" %3d",s);
}
}
main() /*---------------Input Output
function-----------*/
{
int a[N],b[N];
int i;
printf("input number:\n");
for(i=0;i<N;i++)
{
printf("Input a[%d],b[%d]:",i+1,i+1);
scanf("%d,%d",&a[i],&b[i]);
}
printf("Input:\n");
for(i=0;i<N;i++)
{
printf(" %3d",a[i]);
}
printf("\n");
for(i=0;i<N;i++)
{
printf(" %3d",b[i]);
}
printf("\n");
sum(a,b);
getch();
}

----------------------------
??(Senai)
dx********@126.com
----------------------------
Feb 20 '06 #20
if(make new array)this code:

#include "stdio.h"
#define N 8
sum(int a[N],int b[N])
{
int i,s[N];
for(i=0;i<N;i++)
{
s[i]=a[i]+b[i];
}
for(i=0;i<N;i++)
{
printf(" %5d",s[i]);
}
}
main()
{
int a[N],b[N];
int i;
printf("\nPlease input integer:\n");
for(i=0;i<N;i++)
{
printf("Input A[%d],B[%d]:",i+1,i+1);
scanf("%d,%d",&a[i],&b[i]); /*-----input
format:"5,5<cr>"------*/
}
printf("\nArray[A]:");
for(i=0;i<N;i++)
{
printf(" %5d",a[i]);
}
printf("\nArray[b]:");
for(i=0;i<N;i++)
{
printf(" %5d",b[i]);
}
printf("\nArray[S]:");
sum(a,b);
getch();
}

----------------------------
Senai
dx********@126.com
----------------------------
Feb 20 '06 #21
You're right. I'm trying to evaluate this code,it helped me alot in
noticing readability. Thanks everyone spending time on my problem.
Thad Smith wrote:

The best thing you could do is to understand WHY it works and write you
own version. The code given has some subtle problems. One example: the
declaration of carry is compatible with C89, not C99, yet the comments
are supported in C99, not C89. The biggest problem, for me, is the
implied usage and restrictions of add. It works for this test program,
but how should it be used in general? A well-written problem requires
more than giving a correct answer for one test case.

--
Thad


Feb 20 '06 #22

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Tom | last post: by
6 posts views Thread by wukexin | last post: by
3 posts views Thread by Colin J. Williams | last post: by
7 posts views Thread by Corepaul | last post: by
5 posts views Thread by Steve | last post: by
8 posts views Thread by Mark | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by Anwar ali | last post: by

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.