救命!! [英] help!!

查看:74
本文介绍了救命!!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是c的新手,我对阵列有问题。我想把两个数组

加到这样的第三个数组中:

数组a:5 6 8 9 1 2 4 7 3 8

阵列b:5 2 1 2 4 6 8 5 4 3

总和:1 0 9 0 1 5 9 3 2 8 1

我试过循环但我已经失败。任何人都可以帮助我吗?

解决方案



" siya" < AY ********* @ gmail.com>在消息中写道

news:11 ********************* @ g47g2000cwa.googlegro ups.com ...

我是c的新手,我对数组有问题。我想将两个数组相加成第三个数组:
数组a:5 6 8 9 1 2 4 7 3 8
数组b:5 2 1 2 4 6 8 5 4 3
总和:1 0 9 0 1 5 9 3 2 8 1
我试过循环但是我失败了。有人可以帮帮我吗?




告诉我们你的尝试。准确地说出你想要的

程序,以及它的作用。如果您从编译器获得了

错误,也包含它们,并且

表示它们引用了哪些行。


- Mike


我试过这个:

#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(&q uot;%d",c [i]);

}

}

返回0;

}

首先打印一个[1],一个[2] ......打印一个[10]后,它将一个

加到a [10]的值中这样的索引:a [9] =

3,a [10] = 5,a [6] = ...

i想要添加两个数组总和大于10,写下随身携带

到剩下并休息到总和。

Mike Wahler写道:

" siya" < AY ********* @ gmail.com>在消息中写道
新闻:11 ********************* @ g47g2000cwa.googlegro ups.com ...

I 我是c的新手,我对阵列有问题。我想将两个数组相加成第三个数组:
数组a:5 6 8 9 1 2 4 7 3 8
数组b:5 2 1 2 4 6 8 5 4 3
总和:1 0 9 0 1 5 9 3 2 8 1
我试过循环但是我失败了。任何人都可以帮助我吗?



告诉我们您的尝试。准确地说出你想要的
程序,以及它的作用。如果编译器出现错误,也请将它们包括在内,并指出它们引用的行。

-Mike



"丝亚"写道:

我试过这个:
#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>


我认为如果使用函数进行添加,则不会产生混淆。这个

工作,使用\\注释但我的编译器接受它们。注意我在操作数上添加了一个

前导零以强制返回进位。


------------- --------------------------

#include< stdio.h>


#define N 11 //操作数中的位数


int add(int a,int b)

{

静态携带; //将初始化为0

int sum = a + b + carry;

if(sum> 9)

{

sum - = 10;

carry = 1;

}

else

carry = 0;

返还金额;

}

// =================

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]; //允许携带数字

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( );

}



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?

解决方案


"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();
}



这篇关于救命!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆