用c ++查找最小/最大值 [英] Finding min/max with c++

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

问题描述

我有三个问题。代码低于我的Q'。


1.我只是盲目地工作。我甚至都不知道这意味着什么:

max = iNumbers [1];

min = iNumbers [1];

what这是什么意思?


2.这是什么意思:


if(iNumbers [i]> max){

max = iNumbers [i];


3.总和和平均部分可以用更简单的方式完成吗?


Hi, I have three questions. The code is below my Q''s.

1. I just made it to work blindly. I don''t even know what this means:
max=iNumbers[1];
min=iNumbers[1];
what does it mean?

2. And what does this mean:

if (iNumbers[i] > max ) {
max=iNumbers[i];

3. The sum and avg part could have been done in an easier way?


展开 | 选择 | Wrap | 行号

推荐答案

展开 | 选择 | Wrap | 行号


max = iNumbers [1];

你''访问位置0的元素。所以对于{1,12,9,154};

iNumbers [0] = 1

iNumbers [1] = 12

iNumbers [2] = 9

iNumbers [3] = 154
max=iNumbers[1];
You''re accessing the element in position 0. So for {1,12,9,154};
iNumbers[0] = 1
iNumbers[1] = 12
iNumbers[2] = 9
iNumbers[3] = 154


基本上你当前的算法是错误的,因为输出语句位于错误的位置,目前是第25行和第29行。这是在计算中,因为它在里面循环。在循环结束后,您应该在计算结束后输出结果。


因此,您必须在第19行和第20行选择启动索引,这会导致输出正确但是这并没有改变算法错误的事实,只适用于你给出的输入数据而不是任何数据,例如试试


double iNumbers [] = {11,12,9,154 };





double iNumbers [] = {13,12,9,154};


第19行和第20行应该从索引0初始化,你需要修复算法的其余部分,然后打印正确的值。
Basically your current algorithm is wrong because you have the output statements in the the wrong place, currently lines 25 and 29. This is inside the calculation because it is inside the loop. You should output your results once the calculation has finished, after the loop has finished.

Because of this you have had to choose start indexes at lines 19 and 20 that cause the correct output but that does not change the fact the algorithm is wrong and only works for the input data you have given not any data, for example try

double iNumbers[]={11,12,9,154};

or

double iNumbers[]={13,12,9,154};

Line 19 and 20 should initialise from index 0, you need to fix the rest of your algorithm so that it then prints the correct values.


这篇关于用c ++查找最小/最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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