如何创建一个计数函数来从另一个函数中计算序列中的数字数量
How do I create a count function to count the amount of numbers in a sequence from another function?
我正在做一项学校作业,不知道如何创建一个函数来计算在一个序列中打印了多少个数字。我之前让它工作(如下所示),但计数函数需要是它自己的:
7。写一个合约,然后实现一个函数,该函数接受整数n并返回从n开始的冰雹序列的长度。这个函数不能写(打印)任何东西。
修改main函数,使其同时显示冰雹序列和从n开始的冰雹序列的长度。
我对c++还是个新手,我还在学习如何做这些事情。如果这是一件愚蠢的事,我很抱歉。
// This program takes a user defined number 'n'
// and runs it through a function that returns
// the number that follows 'n' in hailstone sequence.
// Since there is no number that follows 1 in the sequence,
// this fuction requires its parameter 'n' to be greater
// than 1.
/*
What number shall I start with? 7
The hailstone sequence starting at 7 is:
7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
The length of the sequence is 17.
The largest number in the sequence is 52.
The longest hailstone sequence starting with a number up to 7 has length 17
The longest hailstone sequence starting with a number up to 7 begins with 7
*/
#include <algorithm>
#include <cstdio>
using namespace std;
void hailstoneSequence(int n);
int main(int argc, char** argv)
{
int n;
printf("What number shall I start with? ");
scanf("%d", &n);
printf("The hailtone sequence starting at %d is: n", n);
hailstoneSequence(n);
return 0;
}
// In hailstoneSequence, while 'n' is not equal to 1
// the function will calculate whether 'n' is even
// to computer n/2, otherwise it will compute 3n + 1 if 'n' is odd.
void hailstoneSequence(int n)
{
// hailLength will keep track of how many
// numbers are produced in the sequence.
int hailLength = 1;
printf("%i", n);
printf(" ");
while(n != 1)
{
if(n % 2 == 0)
{
n /= 2;
printf("%i", n);
printf(" ");
}
else
{
n = (3 * n) + 1;
printf("%i", n);
printf(" ");
}
hailLength++;
}
printf("n");
printf("The length of the sequence is %i.", hailLength);
printf("n");
}
这是我试图计算hailstoneSequence被使用的次数的另一个函数。
int hailstoneLength(int hailLength)
{
hailLength++;
return hailLength;
}
当我尝试使用count函数时,这是我的主要函数的一部分:
int main(int argc, char** argv)
{
int n;
int hailLength = 0;
下面是hailstoneSequence函数:
while(n != 1)
{
if(n % 2 == 0)
{
n /= 2;
printf("%i", n);
printf(" ");
}
else
{
n = (3 * n) + 1;
printf("%i", n);
printf(" ");
}
hailstoneLength(hailLength);
//hailLength++;
}
我敢肯定我现在弄坏的更多了。谁能告诉我我做错了什么,或者帮助我理解如何让函数计数?我还在谷歌上搜索如何在函数中调用函数等等来尝试这些方法。
如果没有while循环,使用if语句会更容易吗?
您需要更改hailstoneSequence函数如下,并从main()
中删除hailLength和nbl的声明int n;
int hailLength = 1;
while(n != 1)
{
if(n % 2 == 0)
{
n /= 2;
printf("%i", n);
printf(" ");
}
else
{
n = (3 * n) + 1;
printf("%i", n);
printf(" ");
}
hailLength=hailstoneLength(hailLength); //You should store the returned value somewhere
//hailLength++;
}
printf("n");
printf("The length of the sequence is %i.", hailLength);
printf("n");
- 递归函数计算序列中的平方和(并输出过程)
- 函数计算用户按下按钮的频率
- C++ 在编译时具有函数计算全局变量
- C++ - 使用特定函数计算 x 和 y 的最终值
- 尝试使用函数计算有剩余球员的球队数量
- 我正在尝试创建一个布尔函数,该函数计算字符并在字符为"()*-+"时返回 true
- 哈希函数计算
- 为什么 if 语句对于运算符重载函数计算 false
- 使用C 中的内在函数计算相交
- 如何使用 2 个迭代器进行求和函数计算映射<字符串、双精度>
- vector.size() 函数计算量大吗?
- 如何使用opencv函数计算图像的孔数(二进制)
- 斐波那契函数计算不正确
- 如何用3个用户定义的函数计算一个数字的真平方根
- 元函数计算 x^n 并返回整数限制而不溢出(如果不可能)
- 使用递归函数计算字符串中的元音
- 如何使用 pow() 函数计算 C++ 中超过 2^32 的幂
- 返回 void 的函数的无序函数计算
- 在 C++ 中使用尾递归函数计算列表的总和
- 使用结构和函数计算线的长度