如何将计算存储在 for 循环中,以便每次计算时都会更新一个整数值,然后打印出来,C

How to store calculation inside a for loop, so every time it is calculated, a integer value is updated and later printed out, C

提问人:Nermin 提问时间:9/18/2020 最后编辑:JabberwockyNermin 更新时间:8/4/2021 访问量:1920

问:

我的目标细节:假设我们有 n 只骆驼的种群。每年,n/3只新骆驼出生,n/4只骆驼去世。

例如,如果我们从 n = 1200 只骆驼开始,那么在第一年,1200 / 3 = 400 只新骆驼将出生,1200 / 4 = 300 只骆驼将去世。到那年年底,我们将有 1200 + 400 - 300 = 1300 只骆驼。

我得出的结论是,每 1200 / 3 和 1200 / 4 一年过去,现在我正在尝试使用 for 循环来迭代一个变量,每次计算完成时,它都会迭代 + 1,这意味着它算作已经过去了一年,然后打印出经过的年数。

预期结果: ./population
起始大小: 100
结束大小: 1000000
年:

115

这是我到目前为止尝试过的。我相信除了 for 循环之外,这段代码中的一切都是正确的,我无法理解应该如何进行计算然后打印出来的逻辑。我总是以零结束。

代码如下:

#include <cs50.h>
#include <stdio.h>

int main(void)
{
    int start_pop_size;
    int end_pop_size;
    int years_passed;


    // TODO: Prompt for start size

        do
        {
          start_pop_size = get_int ("Enter starting population size: \n");
        }
        while (start_pop_size < 9);

    // TODO: Prompt for end size
        do
        {
            end_pop_size = get_int ("Enter ending population size: \n");
        }
        while (end_pop_size <= start_pop_size);

    // TODO: Calculate number of years
    for(years_passed = 0; years_passed < start_pop_size / 3 - end_pop_size / 4; years_passed++)
    {
        int calculation = start_pop_size / 3 - end_pop_size / 4;
    }

     // TODO: Print number of years

    printf("Years : %i", years_passed);
}
C FOR环路 CS50

评论

1赞 ShadowRanger 9/18/2020
@Jabberwocky:虽然小骆驼或死了 25% 的骆驼的想法有点可怕......0.333333333333
0赞 Jabberwocky 9/18/2020
提示:有了 和 ,什么是?for 循环会发生什么?start_pop_size = 100end_pop_size = 1000000start_pop_size / 3 - end_pop_size / 4

答:

2赞 tzaman 9/18/2020 #1

您事先不知道需要多少次迭代才能达到 ,因此您的条件不能基于 。相反,您需要跟踪当前人口,并据此决定何时结束循环:end_pop_sizeyears_passed

int cur_pop_size = start_pop_size;
years_passed = 0;
do {
    // calculation goes here
    years_passed++;
while (cur_pop_size < end_pop_size);

您也可以将其表示为 ,但再次注意,该条件是关于 而不是关于 :forcur_pop_sizeyears_passed

int cur_pop_size = start_pop_size;
for (years_passed = 0; cur_pop_size < end_pop_size; years_passed++) {
    // calculation goes here
}

评论

0赞 klutt 9/18/2020
好吧,你可以,但做 - 虽然肯定更合适
0赞 tzaman 9/18/2020
@klutt当然,添加了该替代方案。我更喜欢这个公式,因为最终条件不是基于循环变量的。while
0赞 klutt 9/18/2020
是的,我同意你的解决方案要好得多。for版本甚至不值得一提。我只是反对它不能使用的说法。:)
0赞 Nermin 9/19/2020
首先,我更新了我的代码如下: // TODO: 计算年数 int cur_pop_size = start_pop_size;years_passed = 0;do { start_pop_size / 3 - end_pop_size / 4; years_passed++; } while (cur_pop_size < end_pop_size);TODO: 打印年数 printf(“Years : %i”, years_passed);之后,我收到一个错误,说我需要将结果存储到某些东西中。所以我这样做了,我把它存储成这样:years_passed = start_pop_size / 3 - end_pop_size / 4;运行并输入种群后,我不会得到结果,程序也不会结束
0赞 tzaman 9/19/2020
@Nermin 您需要根据公式在每次迭代中更新 ,而不是 .cur_pop_sizeyears_passed
0赞 user3629249 9/19/2020 #2

建议的代码如下:

  1. 干净利落地编译
  2. 执行所需的功能
  3. 使用多年,因为该值永远不会< 0size_t
  4. 将输入值与计算值分开
  5. 请注意,当起始人口为 3 或更少时,此算法将失败

现在,建议的代码:

#include <cs50.h>
#include <stdio.h>

int main(void)
{
    int start_pop_size;
    int end_pop_size;
    

    do
    {
        start_pop_size = get_int ("Enter starting population size: \n");
    }
    while (start_pop_size < 9);


    do
    {
        end_pop_size = get_int ("Enter ending population size: \n");
    }
    while (end_pop_size <= start_pop_size);


    
    size_t years_passed = 0;
    int new_pop = start_pop_size;
    for( int old_pop = start_pop_size; 
         new_pop < end_pop_size; 
         years_passed++ )
    {
        printf( "\nloopStart: oldPop:%d /3:%d /4:%d\n", old_pop, old_pop/3, old_pop/4 );
        new_pop +=  old_pop/3 - old_pop/4;
        old_pop = new_pop;
        printf( "loopEnd: newPop:%d\n", new_pop );
    }

    printf("Years : %zu", years_passed);
}

使用 OP 建议的输入值运行:

Enter starting population size: 
100
Enter ending population size: 
1000000
Years : 115
0赞 Aung Khant Ko Ko 8/4/2021 #3
#include <stdio.h>
#include <cs50.h>

int start_size (void);
int end_size (int x);

int main(void)
{
    int start = start_size();
    int end = end_size(start);

    // size_t is uesed as a integer type that is never be less than 0 
    // (positive number) 
    //And It needs "%zu" to print out
    size_t years = 0;

    int new_start = start;

    for(int old = start ;
        new_start < end ;
        years++)
    {

        new_start += old / 3 - old / 4;
        old = new_start;
    }

    printf("Years: %zu\n",years);
}

int start_size (void)
{
    int s;
    do
    {
        s = get_int("Start Size: ");
    }
    while(s < 9);
    return s;
}

int end_size (int x)// x  is the starting point
{
    int e;
    do
    {
        e = get_int("End Size: ");
    }
    while(e <= x);
    return e;
}