help!!

S

siya

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?
 
M

Mike Wahler

siya said:
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
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

siya said:
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=array_a+array_b;

(untested)


Best regards / Med venlig hilsen
Martin Jørgense
 
S

siya

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);
}
printf("\n");
for(i=1;i<=10;i++){
printf("b(%d)=",i);
scanf("%d",&b);
}
printf("\n");
temp = a + b;
for(i=1;i<=10;i++){
if ( temp >=10 ){
carry = (a + b + carry) / 10;
temp = temp %10;
c = temp;
printf("c(%d)=%d\n",i,c);
}
else{
c = a + b;
printf("%d", c);
}
}
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.
 
O

osmium

siya said:
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);
}

<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 = add(a, b);

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

pete

siya said:
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 */
 
P

pete

pete said:
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.
 
K

Keith Thompson

Martin Jørgensen said:
siya said:
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=array_a+array_b;

(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).
 
J

Jordan Abel

Martin Jørgensen said:
siya said:
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=array_a+array_b;

(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.
 
T

Thad Smith

pete said:
pete said:
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');
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

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
 
T

Thad Smith

osmium said:
> 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 = add(a, b);
>
> for(i=0; i<N; i++)
> printf("%d", sum);
> printf("%\nDone\n");
> getchar();
> }

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

osmium

Thad Smith said:
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?
 
R

Richard G. Riley

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

Keith Thompson

osmium said:
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.)
 
C

CBFalconer

osmium said:
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/>
 
O

osmium

Keith Thompson said:
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.
 
J

Jordan Abel

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

Senai

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+b;
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,&b);
}
printf("Input:\n");
for(i=0;i<N;i++)
{
printf(" %3d",a);
}
printf("\n");
for(i=0;i<N;i++)
{
printf(" %3d",b);
}
printf("\n");
sum(a,b);
getch();
}
 

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

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,944
Members
47,491
Latest member
mohitk

Latest Threads

Top