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? 21 1476
"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
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
"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();
}
thank you it works alot:)
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
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
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
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.
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
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
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.
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
"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?
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
"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.
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/>
"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.
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.
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
----------------------------
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
----------------------------
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |