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

时间:2016-09-14 01:12:19

标签: c++

我正在完成一项学校作业,并且无法弄清楚如何创建一个函数来计算序列中打印的数量。我之前有过工作(如下所示),但是count函数需要是它自己的:

7。写一个函数的契约,然后是一个实现,它接受一个整数n并返回从n开始的hailstone序列的长度。此功能不得写入(打印)任何内容。

修改你的主要功能,使其显示冰雹序列和从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;
}

当tryign使用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++;
}

我相信我现在正在打破它。任何人都可以告诉我我做错了什么,或者帮助我理解如何计算功能?我还在尝试google如何在函数和函数中调用函数来尝试这些方法。

如果没有while循环的if语句会更容易使用吗? 我无法使用递归进行此分配。

1 个答案:

答案 0 :(得分:0)

您需要更改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");