如何创建一个计数函数来从另一个函数中计算序列中的数字数量

How do I create a count function to count the amount of numbers in a sequence from another function?

本文关键字:函数 计算 何创建 数字 另一个 一个 创建      更新时间:2023-10-16

我正在做一项学校作业,不知道如何创建一个函数来计算在一个序列中打印了多少个数字。我之前让它工作(如下所示),但计数函数需要是它自己的:

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");