关于循环和数组的练习 - 使用循环 1 次以及如何获取数组的最大值
Posted
技术标签:
【中文标题】关于循环和数组的练习 - 使用循环 1 次以及如何获取数组的最大值【英文标题】:Exercise about loops and arrays - use loops 1 time and how to get the max value of an array 【发布时间】:2017-08-10 13:33:59 【问题描述】:我对编程知之甚少,但我对 C++ 还是很陌生。我一个星期前刚开始研究它,我发布了一个关于我编写的代码的问题,因为我无法解决一些小问题。
我写的程序询问你想分析多少元素,它返回元素的总和,你插入了多少偶数和奇数,只有奇数和偶数的总和,它应该返回最大值(和最小值)值。
这是代码:
#include <iostream>
using namespace std;
int elements, sum = 0, x, arr[10], even = 0, odd = 0, evenSum = 0, oddSum = 0,
mx; //Variable declaration
int main()
cout << "Type how many elements you would like to sum and analyze" << endl; //Input of the numbers of elements to analyze
cin >> elements;
if (elements > 10)
cout << "Too many elements" << endl; //If the elements are more than 10, the program quit
else
for (x = 1; x <= elements; x++)
cout << "Type the element number " << x << endl; //Input of elements to assign to the array
cin >> arr[x];
for (x = 1; x <= elements; x++) //Sum of the elements of the array
sum += arr[x];
mx = arr[0];
for (x = 1; x <= elements; x++) //Find the maximum value of the array
if (arr[0] >= mx)
mx = arr[0];
for (int x = 1; x <= elements; x++) //Count and sum of the even elements
if (arr[x] % 2 == 0)
even++;
evenSum += arr[x];
if (arr[x] % 2 != 0) //Count and sum of the odd elements
odd++;
oddSum += arr[x];
cout << "The sum of the elements of the array is: " << sum << endl; //Outputs
cout << "The max value of the array is: " << mx << endl;
cout << "Even numbers are: " << even << endl;
cout << "Odd numbers are: " << odd << endl;
cout << "The sum of even numbers is: " << evenSum << endl;
cout << "The sum of odd numbers is: " << oddSum << endl;
return 0;
我的问题是:
-
我是否必须重复“for”循环 4 次,或者,因为条件
的循环是一样的,我只能写一次,然后执行代码?
为了计算最大值,我使用了变量名“max”,但编辑器没有编译。我将名称从“max”更改为“mx”
它编译。 “max”是 C++ 的保留字吗?怎么了?
为什么程序总是给出“0”作为数组的最大值?我不明白算法出了什么问题,我可以得到所列元素的最大值。
非常感谢您的支持。 马特奥
【问题讨论】:
1.不,你不必。您可以在一个循环的主体中完成所有操作 首先你应该注意数组索引从零开始。 您确实记得数组索引是从零开始的吗? IE。一个由 10 个元素组成的数组的索引从0
到 9
(含)。
你甚至不应该为此需要一个数组。您应该能够在获取输入的同时进行所有这些计算。
2.你不能使用 max 因为它被定义为 std 中的一个函数并且你正在使用 using namespace std
所以他们命名为冲突。另外,使用using namespace std
被认为是bad practice
【参考方案1】:
代码清理 101:
有几件事可以使您的代码更简洁。
1)您不需要那么多 for
循环,并且由于您不需要那么多 for
循环,您可以摆脱您的数组。
2)您的变量应该是本地的而不是全局的。
3)您的一些变量未初始化为0
,因此您不能执行odd++;
(例如)
查看我的更改,了解它们,阅读 cmets,如果有问题,请提出问题 :)
#include <iostream>
using namespace std; //probably don't want to use this here
int main()
// keep these variables local instead of global
int elements, sum , even, odd, evenSum, oddSum, mx;
sum = even = odd = evenSum = oddSum = mx = 0; //set them all to 0
cout << "Type how many elements you would like to sum and analyze" << endl; //Input of the numbers of elements to analyze
cin >> elements;
if (elements > 10)
cout << "Too many elements" << endl; //If the elements are more than 10, the program quit
else
// An array is based on the 0 index not the 1 index.
// so the first value in array as at x = 0 and the second is at x = 1 and so on
int currentNum;
for (int x = 0; x < elements; x++) //int x = 0 and x < elements
cout << "Type the element number " << x << endl; //Input of elements to assign to the array
cin >> currentNum;
sum += currentNum;
if (currentNum >= mx)
mx = currentNum;
if (currentNum % 2 == 0)
even++;
evenSum += currentNum;
if (currentNum % 2 != 0) //Count and sum of the odd elements
odd++;
oddSum += currentNum;
cout << "The sum of the elements of the array is: " << sum << endl; //Outputs
cout << "The max value of the array is: " << mx << endl;
cout << "Even numbers are: " << even << endl;
cout << "Odd numbers are: " << odd << endl;
cout << "The sum of even numbers is: " << evenSum << endl;
cout << "The sum of odd numbers is: " << oddSum << endl;
return 0;
【讨论】:
请问为什么我应该在 main() 函数中而不是全局声明我的变量?有什么区别? 创建非静态全局变量是非常糟糕的做法,即使是静态变量也只能在需要时使用。阅读此示例Why global variables are evil 非常感谢!另一个问题:为什么sum = even = odd = evenSum =oddSum = mx = 0而其他变量没有初始化?为什么 sum = even = odd = evenSum =oddSum = mx = 0 开头没有“int”?再次感谢! 它们在一行int elements, ...
上声明,并在下一行初始化为0
。这就是所有=
的用途。如果它对您有用,请确保将其标记为答案:)【参考方案2】:
你应该能够在单个 for 循环中计算所有内容,是的。
这可能是因为你有这条线
using namespace std;
在文件的顶部。当您编写 max
时,编译器假定您的意思是函数 std::max
: http://en.cppreference.com/w/cpp/algorithm/max
您可以尝试删除using namespace std;
,而是在需要的地方插入std::
,例如std::cin
、std::cout
和 std::endl
。
有两个问题。首先你这里有错别字:
if (arr[0] >= mx)
,mx = arr[0];
将0
替换为x
。如果您只输入负数,则会出现第二个问题。 mx
没有显式初始化,因此它将被初始化为零。如果你只输入负数,那么你的程序会错误地说零是最大的数。您可能应该将 mx
初始化为其类型可能的最低数字,例如:std::numeric_limit<int>::min()
。
【讨论】:
【参考方案3】:首先,如果你有基于 0 的索引,你不会在第一个循环中读取 arr[0]
for (x = 1(error, it should be 0); x <(only '<') elements; x++)
cout << "Type the element number " << x << endl; //Input of elements to assign to the array
cin >> arr[x];
现在,您可以在两个循环中完成所有操作。
#include <iostream>
using namespace std;
int elements, sum = 0, x, arr[10], even = 0, odd = 0, evenSum = 0, oddSum =
0,
mx; //Variable declaration
int main()
cout << "Type how many elements you would like to sum and analyze" << endl; //Input of the numbers of elements to analyze
cin >> elements;
if (elements > 10)
cout << "Too many elements" << endl; //If the elements are more than 10, the program quit
else
for (x = 0; x < elements; x++)
cout << "Type the element number " << x << endl; //Input of elements to assign to the array
cin >> arr[x];
mx = arr[0];
for (x = 0; x < elements; x++) //Find the maximum value of the array
sum += arr[x];
if (arr[x] >= mx)
mx = arr[x];
if (arr[x] % 2 == 0)
even++;
evenSum += arr[x];
if (arr[x] % 2 != 0) //Count and sum of the odd elements
odd++;
oddSum += arr[x];
cout << "The sum of the elements of the array is: " << sum << endl; //Outputs
cout << "The max value of the array is: " << mx << endl;
cout << "Even numbers are: " << even << endl;
cout << "Odd numbers are: " << odd << endl;
cout << "The sum of even numbers is: " << evenSum << endl;
cout << "The sum of odd numbers is: " << oddSum << endl;
return 0;
你可以在一个循环中完成所有操作
#include <iostream>
using namespace std;
int elements, sum = 0, x, arr[10], even = 0, odd = 0, evenSum = 0, oddSum =
0,
mx; //Variable declaration
int main()
cout << "Type how many elements you would like to sum and analyze" << endl; //Input of the numbers of elements to analyze
cin >> elements;
if (elements > 10)
cout << "Too many elements" << endl; //If the elements are more than 10, the program quit
else
mx = arr[0];
for (x = 0; x < elements; x++)
cout << "Type the element number " << x << endl; //Input of elements to assign to the array
cin >> arr[x];
sum += arr[x];
if (arr[x] >= mx) //error here, you need compare arr[x], not arr[0]
mx = arr[x];
if (arr[x] % 2 == 0)
even++;
evenSum += arr[x];
if (arr[x] % 2 != 0) //Count and sum of the odd elements
odd++;
oddSum += arr[x];
cout << "The sum of the elements of the array is: " << sum << endl; //Outputs
cout << "The max value of the array is: " << mx << endl;
cout << "Even numbers are: " << even << endl;
cout << "Odd numbers are: " << odd << endl;
cout << "The sum of even numbers is: " << evenSum << endl;
cout << "The sum of odd numbers is: " << oddSum << endl;
return 0;
【讨论】:
【参考方案4】:关于数组索引的要点!我知道数组是 0 索引的,但即使从 1 开始,程序也能正常工作! 我输入 5 作为要分析的元素数量,我可以插入一个,例如,1 2 3 4 5,具有正确的输出:
总和:15 奇数元素:3 偶数:2我不明白为什么!
【讨论】:
以上是关于关于循环和数组的练习 - 使用循环 1 次以及如何获取数组的最大值的主要内容,如果未能解决你的问题,请参考以下文章