这是更好的道路 [英] which is the better path

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

问题描述

问题:

编写一个名为sumarrays()的函数,它接受两个大小相同的数组

。该函数应该将数组中的每个元素添加到一起并将值放在thrid数组中。


哪个答案是更好的做法;


/*EX9.C*/

#include< stdio.h>

#define MAX 5


int array2 [MAX] = {1,2,3,4,5};

int array3 [MAX] = {6,7,8,9,10};

int sumarray(int array2 [],int array3 []);


int main(void)

{

sumarray(array2,array3);

返回0;

}


int sumarray(int array2 [] ,int array3 [])

{

int ctr;

int total [MAX];


for(ctr = 0; ctr< MAX; ctr ++)

{

total [ctr] = array2 [ctr] + array3 [ctr];

printf(总%d \ n,总计[ctr]);

}

返回总计[count];

}

____________________________________________

/ * EX9-1.C * /

#include< stdio.h>

#define SIZE 5

void sumarray(int [],int []);

int main(void)

{

int a [SIZE] = {1,2,3,4,5};

int b [SIZE] = {6,7,8,9,10};


sumarray(a,b);

返回0;

}


void sumarray(int first [],int second [])

{

int total [SIZE];

int ctr;


for(ctr = 0; ctr<尺寸; ctr ++)

{

total [ctr] = first [ctr] + second [ctr];

printf(" Total%d \\ \\ n,总计[ctr]);

}


}

Question:
Write a function named sumarrays() that accepts two arrays that are the same
size. The function should add each element in the array together and place
the values in the thrid array.

Which answer is the better practice;

/*EX9.C*/
#include<stdio.h>
#define MAX 5

int array2[MAX] = {1,2,3,4,5};
int array3[MAX] = {6,7,8,9,10};
int sumarray(int array2[], int array3[]);

int main(void)
{
sumarray(array2,array3);
return 0;
}

int sumarray(int array2[], int array3[])
{
int ctr;
int total[MAX];

for(ctr = 0; ctr < MAX; ctr++)
{
total[ctr] = array2[ctr] + array3[ctr];
printf("Total %d\n",total[ctr]);
}
return total[count];
}
____________________________________________
/* EX9-1.C*/
#include<stdio.h>
#define SIZE 5

void sumarray(int [], int []);
int main(void)
{
int a[SIZE] = {1,2,3,4,5};
int b[SIZE] = {6,7,8,9,10};

sumarray(a,b);
return 0;
}

void sumarray(int first[], int second[])
{
int total[SIZE];
int ctr;

for(ctr = 0; ctr < SIZE; ctr++)
{
total[ctr] = first[ctr] + second[ctr];
printf("Total %d\n",total[ctr]);
}

}

推荐答案

Darklight写道:
Darklight wrote:
问题:
编写一个名为sumarrays()的函数,它接受两个大小相同的数组。该函数应该将数组中的每个元素添加到一起,并将值放在thrid数组中。


哪个第三阵列?

哪个答案是更好的做法;

/*EX9.C*/
#include< stdio.h>
#define MAX 5
int array2 [MAX] = {1,2,3,4,5};
int array3 [MAX] = {6,7,8,9,10};


为什么这些数组是全局的?

int sumarray(int array2 [],int array3 []);


如果你把整个功能放在首位,你就不需要这个。

int main(无效)
{/ / sumarray (数组2,ARRAY3);


summarray()返回一个int,为什么要用它?

结果在哪里?

返回0 ;

int sumarray(int array2 [],int array3 [])
{
数组的大小应作为参数传递。为什么''array2''和

''array3''? ''array1''在哪里?

int ctr;
通常使用''i'表示循环索引。

int total [MAX];
(bis)数组的大小应该作为参数传递

for(ctr = 0; ctr< MAX; ctr ++)
{
total [ctr ] = array2 [ctr] + array3 [ctr];
printf(Total%d\ n,total [ctr]);


这个printf()在这做什么?

}
返回总计[count];


呃...你有没有试过*编译*这段代码?

}
____________________________________________
/ * EX9-1 .C * /
#include< stdio.h>
#define SIZE 5
void sumarray(int [],int []);
int main( void)
{
int [SIZE] = {1,2,3,4,5};
int b [SIZE] = {6,7,8,9,10} ;
好​​的,他们至少在正确的地方

sumarray(a,b);


结果在哪里?

返回0;
}

void sumarray(int first [],int第二个[])
{
数组大小应作为参数传递。 int total [SIZE];
int ctr;
与上面相同

for(ctr = 0; ctr< SIZE; ctr ++)
{
总[ctr] =第一[ctr] +秒[ctr ];
printf(Total%d\ n,total [ctr]);
与上述相同}

}
Question:
Write a function named sumarrays() that accepts two arrays that are the same
size. The function should add each element in the array together and place
the values in the thrid array.
Which third array ?
Which answer is the better practice;

/*EX9.C*/
#include<stdio.h>
#define MAX 5

int array2[MAX] = {1,2,3,4,5};
int array3[MAX] = {6,7,8,9,10};
Why are those arrays globals ?
int sumarray(int array2[], int array3[]);
You don''t need this if you put the whole function first.
int main(void)
{
sumarray(array2,array3);
summarray() returns an int, why do you do with it ?
And where is the result ?
return 0;
}

int sumarray(int array2[], int array3[])
{ The size of arrays shoud be passed as a param. And why ''array2'' and
''array3'' ? Where is ''array1'' ?
int ctr; One usually uses ''i'' for loop indices.
int total[MAX]; (bis) The size of arrays shoud be passed as a param
for(ctr = 0; ctr < MAX; ctr++)
{
total[ctr] = array2[ctr] + array3[ctr];
printf("Total %d\n",total[ctr]);
What is this printf() doing here ?
}
return total[count];
Er... did you ever try and *compile* this code ?
}
____________________________________________
/* EX9-1.C*/
#include<stdio.h>
#define SIZE 5

void sumarray(int [], int []);
int main(void)
{
int a[SIZE] = {1,2,3,4,5};
int b[SIZE] = {6,7,8,9,10}; Ok, they are at least at the right place
sumarray(a,b);
Where is the result ?
return 0;
}

void sumarray(int first[], int second[])
{ array size should be passed as a parameter. int total[SIZE];
int ctr; same as above
for(ctr = 0; ctr < SIZE; ctr++)
{
total[ctr] = first[ctr] + second[ctr];
printf("Total %d\n",total[ctr]); same as above }

}




两者都有些难看。再试一次。

提示:

- 将数组len和''result''数组作为params传递

sumarray()的proto应该是这样的:

int * sumarray(int first [],int second [],int total [],size_t len);


- 移动printf()out of sumarray()

- 使用''i''循环指示


HTH

Bruno



Both are somewhat ugly. Try again.
Tips :
- pass the array len and the ''result'' array as params
the proto for sumarray() should look like :
int * sumarray(int first[], int second[], int total[], size_t len);

- move the printf() out of sumarray()
- use ''i'' for the loop indice

HTH
Bruno


Darklight< ng ****** @ netscape.net>潦草地写道:
Darklight <ng******@netscape.net> scribbled the following:
问题:
编写一个名为sumarrays()的函数,它接受两个大小相同的数组。该函数应该将数组中的每个元素添加到一起,并将值放在thrid数组中。


这就是全部?这个函数不需要用第三个

数组做任何事情吗?

哪个答案是更好的做法;
/*EX9.C*/
#include< stdio.h>
#define MAX 5
int array2 [MAX] = {1,2,3,4,5 int array3 [MAX] = {6,7,8,9,10};
int sumarray(int array2 [],int array3 []);
int main(void)
{sumarray(array2,array3);
返回0;
}
int sumarray(int array2 [],int array3 [])
{
int ctr;
int total [MAX];
for(ctr = 0; ctr< MAX; ctr ++)
{
total [ctr] = array2 [ctr] + array3 [ctr];
printf(" Total %d \ n",total [ctr]);
}
返回总计[count];


计数未申报。

}
____________________________________________
/ * EX9-1.C * /
#include< stdio.h>
#define SIZE 5
void sumarray(int [],int []);
int main(void)
{
int a [SIZE ] = {1,2,3,4,5};
int b [SIZE] = {6,7,8,9,10};
sumarray(a,b);
返回0;
}
void sumarray(int first [],int second [])
{
int总计[SIZE];
int ctr;
for(ctr = 0; ctr< SIZE; ctr ++)
{
total [ctr] = first [ctr] + second [ctr];
printf(" Total %d \ n",total [ctr]);
}
}
Question:
Write a function named sumarrays() that accepts two arrays that are the same
size. The function should add each element in the array together and place
the values in the thrid array.
That''s all? The function doesn''t need to DO anything with the third
array?
Which answer is the better practice; /*EX9.C*/
#include<stdio.h>
#define MAX 5 int array2[MAX] = {1,2,3,4,5};
int array3[MAX] = {6,7,8,9,10};
int sumarray(int array2[], int array3[]); int main(void)
{
sumarray(array2,array3);
return 0;
} int sumarray(int array2[], int array3[])
{
int ctr;
int total[MAX]; for(ctr = 0; ctr < MAX; ctr++)
{
total[ctr] = array2[ctr] + array3[ctr];
printf("Total %d\n",total[ctr]);
}
return total[count];
count is undeclared.
}
____________________________________________
/* EX9-1.C*/
#include<stdio.h>
#define SIZE 5 void sumarray(int [], int []);
int main(void)
{
int a[SIZE] = {1,2,3,4,5};
int b[SIZE] = {6,7,8,9,10}; sumarray(a,b);
return 0;
} void sumarray(int first[], int second[])
{
int total[SIZE];
int ctr; for(ctr = 0; ctr < SIZE; ctr++)
{
total[ctr] = first[ctr] + second[ctr];
printf("Total %d\n",total[ctr]);
} }




我更喜欢第二个,因为它有main()的本地数组()

而不是全局。

你似乎没有在第一个使用sumarray()的返回值

$也适用于任何一项。


-

/ - Joona Palaste(pa*****@cc.helsinki.fi) - -----------芬兰-------- \

\-- http://www.helsinki.fi/~palaste ---------------------规则! -------- /

微软做出一些不吸的事情的那一天可能是他们开始制造吸尘器的那一天。

- Ernst Jan Plugge



I like the second one better, because it has the arrays local to main()
instead of global.
You don''t appear to use the return value from sumarray() in the first
one for anything either.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The day Microsoft makes something that doesn''t suck is probably the day they
start making vacuum cleaners."
- Ernst Jan Plugge


2004年2月17日星期二21:33:11 +0100,在comp.lang.c,Bruno Desthuilliers

< bd ***************** @ free.quelquepart.fr>写道:
On Tue, 17 Feb 2004 21:33:11 +0100, in comp.lang.c , Bruno Desthuilliers
<bd*****************@free.quelquepart.fr> wrote:
Darklight写道:
Darklight wrote:


int sumarray(int array2 [],int array3 []);
int sumarray(int array2[], int array3[]);



如果你把整个函数放在第一位,你就不需要这个。



You don''t need this if you put the whole function first.




True,但原型是一件好事,值得鼓励。


-

Mark McIntyre

CLC FAQ< http: //www.eskimo.com/~scs/C-faq/top.html>

CLC自述文件:< http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>

---- ==通过Newsfeed.Com发布 - 无限制 - 未经审查 - 安全Usenet新闻== ----
http://www.newsfeed.com 世界排名第一的新闻组服务! > 100,000新闻组

--- = 19东/西海岸专业服务器 - 通过加密的总隐私= ---



True, but prototypes are a Good Thing, and to be encouraged.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---


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

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