简介阵列 [英] Introduction to Arrays

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

问题描述


  

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/6638729/when-do-we-need-to-pass-the-size-of-array-as-a-parameter\">when我们需要数组的大小作为参数传递


所以我刚开始使用数组时,我有3个功能,我需要创建让我去学习。

  INT sumarray(int类型的[],INT N);// a是n个元素的数组
// sumarray必须返回元素的总和
//你可以假设的结果是范围
// [-2 ^ -31,2 ^ 31-1]INT maxarraypos(int类型的[],INT N);// a是n个元素的数组
// maxarraypos必须返回的位置
//最大的首次出现
//一个值
//如果没有这样的值,必须返回0布尔lexlt(int类型的[],诠释N,INT B〔],INT米);//字典小于的阵列之间
//长度为n和一个数组b的长度为m
//如果接在B之前返回true
//字典顺序;否则为false

到底如何我会创造这些功能呢?

有关 sumarray ,我是因为数组存储一定长度内的东西迷惑。为什么需要第二个参数 N

和还我将如何测试消耗数组的功能?我想喜欢 sumarray([3],3) ..是吗?


解决方案

  

sumarray ,即时通讯很困惑,因为数组存储一定长度内的东西,为什么还需要第二个参数 N


您需要第二个参数来告诉你该数组有多长。数组作为参数在C方法不来与连接到他们自己的长度。所以,如果你有需要一个数组作为参数的方法,它可以不知道长度,除非你也传递给方法。这就是 N


  

,以及如何将我测试消耗是一个数组,我在想,比如 sumarray功能([3],3) ..是吗?


没有。你可以说

  INT myArray的[10] = {1,2,3,4,5,6,7,8,9,10};

然后

  INT总和= sumarray(myarray的,10);

要解决所有这些,你需要循环(循环是最好的,我敢肯定你的讲师所提供的示例如何循环的元素数组)。除此之外,我不这样做你的功课。询问具体,尖锐的问题,我很乐意回答考虑他们虽然。

Possible Duplicate:
when do we need to pass the size of array as a parameter

So I just started working with arrays, I have 3 functions i need to create to get me to learn.

int sumarray(int a[], int n);

// a is an array of n elements
// sumarray must return the sum of the elements
// you may assume the result is in the range
//    [-2^-31, 2^31-1]

int maxarraypos(int a[], int n);

// a is an array of n elements
// maxarraypos must return the position of
//   the first occurrence of the maximum
//   value in a
// if there is no such value, must return 0

bool lexlt(int a[], int n, int b[], int m);

// lexicographic "less than" between an array
//   a of length n and an array b of length m
// returns true if a comes before b in
//   lexicographic order; false otherwise

How exactly would I create these functions?

For sumarray, I'm confused since an array stores something within a certain length. Why would need the second parameter n?

And also how would I test a function that consumes an array? I was thinking like sumarray([3], 3) .. is that right?

解决方案

for sumarray, im confused since an array stores something within a certain length, why would need the second parameter n?

You need the second parameter to tell you how long the array is. Arrays as parameters to methods in C don't come with their length attached to them. So if you have a method that takes an array as a parameter, it can't know the length unless you also pass that to the method. That's what n is for.

And also how would i test a function that consume's an array, i was thinking like sumarray([3], 3) .. is that right ?

No. You could say

int myArray[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

and then

int sum = sumarray(myArray, 10);

To solve all of these, you'll need loop (a for loop is best, I'm sure your lecturer provided examples on how to loop over the elements of an array). Beyond that, I'm not doing your homework. Ask specific, pointed questions, and I'd be happy to consider answering them though.

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

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