c++ - 如何将不能被2整除的数组中的数字添加元素
问题描述
我正在尝试编写一个 C++ 程序,该程序在数组中添加不能被 2 整除的元素。我在主函数中初始化了我的数组并创建了另一个函数,其中使用 for 循环和 if 语句,我尝试提取数组中不能被 2 整除的数字。在我运行程序之前一切似乎都很好,它打印了几个我似乎无法理解的数字
#include <iostream>
#include <string.h>
using namespace std;
//sum of all numbers that are not divisible by 2
int findDivisibleNumbers(int a[], int n, int sumofnumbers)
{
for (int i = 0; i < n; i++) {
if (i % 2 != 0)
sumofnumbers = sumofnumbers + a[i];
cout << "Sum of values not divisible by 2 = " << sumofnumbers << endl;
}
return 0;
}
int main()
{
` int arrayOfCrazyNumbers[] = { 11,2446,2343,144,65,26,17,8,29,10,238126,1912338 };
findDivisibleNumbers(arrayOfCrazyNumbers, sizeof(arrayOfCrazyNumbers), 0);
}
当我运行程序时,它会打印此输出;
Sum of values not divisible by 2 = 0
Sum of values not divisible by 2 = 2446
Sum of values not divisible by 2 = 2446
Sum of values not divisible by 2 = 2590
Sum of values not divisible by 2 = 2590
Sum of values not divisible by 2 = 2616
Sum of values not divisible by 2 = 2616
Sum of values not divisible by 2 = 2624
Sum of values not divisible by 2 = 2624
Sum of values not divisible by 2 = 2634
Sum of values not divisible by 2 = 2634
Sum of values not divisible by 2 = 1914972
Sum of values not divisible by 2 = 1914972
Sum of values not divisible by 2 = -857078488
Sum of values not divisible by 2 = -857078488
Sum of values not divisible by 2 = -1716071948
Sum of values not divisible by 2 = -1716071948
Sum of values not divisible by 2 = -1716071937
Sum of values not divisible by 2 = -1716071937
Sum of values not divisible by 2 = 1738090781
Sum of values not divisible by 2 = 1738090781
Sum of values not divisible by 2 = 1739319245
Sum of values not divisible by 2 = 1739319245
Sum of values not divisible by 2 = 1743381485
Sum of values not divisible by 2 = 1743381485
Sum of values not divisible by 2 = 1743381486
Sum of values not divisible by 2 = 1743381486
Sum of values not divisible by 2 = 1754875318
Sum of values not divisible by 2 = 1754875318
Sum of values not divisible by 2 = 1766408486
Sum of values not divisible by 2 = 1766408486
Sum of values not divisible by 2 = 1770470818
Sum of values not divisible by 2 = 1770470818
Sum of values not divisible by 2 = 929666308
Sum of values not divisible by 2 = 929666308
Sum of values not divisible by 2 = 930850097
Sum of values not divisible by 2 = 930850097
Sum of values not divisible by 2 = 930850097
Sum of values not divisible by 2 = 930850097
Sum of values not divisible by 2 = 930850097
Sum of values not divisible by 2 = 930850097
Sum of values not divisible by 2 = 930850097
Sum of values not divisible by 2 = 930850097
Sum of values not divisible by 2 = 930850097
Sum of values not divisible by 2 = 930850097
Sum of values not divisible by 2 = 932097113
Sum of values not divisible by 2 = 932097113
Sum of values not divisible by 2 = 932097113
C:\COM205_ProgrammingTwo2021Q1\CPlusPlus\BasicDataTypeMemoryAndPointersPt1\Debug\BasicDataTypeMemoryAndPointersPt1.exe (process 21016) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
可能是什么问题呢?
解决方案
正如到处所说,最大的问题是您滥用sizeof()
. sizeof()
以字节为单位告诉您某些内容的大小。如果您想要数组的长度并想继续使用sizeof()
,则需要考虑每个元素的大小。
sizeof(arrayOfCrazyNumbers) / sizeof(arrayOfCrazyNumbers[0])
这是数组的总字节数除以 an int
(可能是 4)中的字节数,得到int
数组中的数字 s。这为您提供了正确的范围,但您对错误的元素求和。
问题是i % 2 != 0
。i
只是一个计数,而不是数组的元素。你想要a[i] % 2
。你可以省略!= 0
,它是多余的。
对代码的这两项更改应该可以解决您的问题。
但是等等,你标记了这个 C++,这是一种非常 C 的做事方式。您可以使用std::array
,它知道自己的大小,并且不需要您计算或传递它。
您还需要更好地利用您的功能。名字没了 它的工作实际上不是找到可整除的数字,它的工作是对奇数求和。所以我们要改名字。我在选择sum_odds
。您传递一个包含总和的变量作为参数,但您通过值而不是通过引用传递它,并且您还将函数标记为返回一个 int,但无论如何都只返回 0。通过引用(输出参数)传递 sum 变量,或者实际返回 sum 而不是 0。main()
应该总是返回 0,这是我们应该如何编写函数的一个例外。我将选择返回总和并在 main 中进行打印。毕竟,新的函数名不是sum_odds_and_print
.
也不要using namespace std;
。这是不好的做法,也是一个很难改掉的习惯。
#include <array>
#include <iostream>
// sum of all numbers that are not divisible by 2
int sum_odds(const std::array<int, 12>& a) {
int oddSum = 0;
for (auto i : a) { // range-based for loop
if (i % 2) oddSum += i;
}
return oddSum;
}
int main() {
std::array<int, 12> arrayOfCrazyNumbers{11, 2446, 2343, 144, 65, 26,
17, 8, 29, 10, 238126, 1912338};
int oddSum = sum_odds(arrayOfCrazyNumbers);
std::cout << "Sum of odd numbers: " << oddSum << '\n';
}
代码还有另一个变化,那就是使用基于范围的 for 循环。当您想要遍历每个元素时,这是一种速记。在这种情况下,i
不再是计数变量,而是i
在循环时保存数组中每个元素的值。
我仍然不会称之为理想,因为数组大小是其定义的一部分。我可以使用常量或宏,但是嗯。我们将从 切换std::array
到std::vector
。对于这个程序的所有意图和目的,我们可以把它想象成一个数组,除了我不必一直指定大小,它知道自己的大小。
#include <iostream>
#include <vector>
// sum of all numbers that are not divisible by 2
int sum_odds(const std::vector<int>& a) {
int oddSum = 0;
for (auto i : a) { // range-based for loop
if (i % 2) oddSum += i;
}
return oddSum;
}
int main() {
std::vector<int> arrayOfCrazyNumbers{11, 2446, 2343, 144, 65, 26,
17, 8, 29, 10, 238126, 1912338};
int oddSum = sum_odds(arrayOfCrazyNumbers);
std::cout << "Sum of odd numbers: " << oddSum << '\n';
}
这是一个很好的称呼,但只是为了好玩,假设我们决定进一步利用标准库。有几种不同的方法可以解决这个问题,这里有一个:
#include <iostream>
#include <numeric> // std::accumulate()
#include <vector>
// sum of all numbers that are not divisible by 2
int sum_odds(const std::vector<int>& a) {
return std::accumulate(a.begin(), a.end(), 0,
[](int sum, int i) { return sum += i % 2 ? i : 0; });
}
int main() {
std::vector<int> arrayOfCrazyNumbers{11, 2446, 2343, 144, 65, 26,
17, 8, 29, 10, 238126, 1912338};
int oddSum = sum_odds(arrayOfCrazyNumbers);
std::cout << "Sum of odd numbers: " << oddSum << '\n';
}
我知道这可能看起来像胡言乱语,这很好。随着您继续学习编程和 [希望] C++,这将更有意义。最疯狂的语法可以通过查找 lambdas 和三元运算符来学习。我推荐网站https://cppreference.com。
推荐阅读
- c# - 无法在 for 循环中打印随机切换案例字符串
- python-3.x - csv仅在python中写入最后一行
- javascript - 数据层变量不起作用 - GTM 上的自定义 HTML 代码
- python - 加载使用 FloPy MODFLOW 创建的 MNW2 包时出错
- node.js - 使 VSCode 扩展解析实时文档并访问输出
- actionscript-2 - 测试 AS2 消息的客户端
- c# - 如何优化将指针数组复制到另一个数组?
- node.js - Nginx proxy_reverse 和节点服务器。您在哪里看到来自上游 NODE 服务器的控制台日志 OUTPUT?
- math - 在不同计数算法的公式中,^ 符号是什么意思?
- c# - Blazor 在布局上加载元素