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

如何将计算存储在for循环中,因此每次计算时,都会更新一个整数值,然后打印出来,C,c,for-loop,cs50,C,For Loop,Cs50,我的目标的细节:假设我们有n头骆驼。每年有n/3头新骆驼出生,n/4头骆驼死亡 例如,如果我们从n=1200头美洲驼开始,那么在第一年,1200/3=400头新美洲驼出生,1200/4=300头美洲驼死亡。到那年年底,我们将有1200+400-300=1300头骆驼 我得出了一个结论,每1200/3和1200/4一年过去一次,现在我尝试使用for循环来迭代一个变量,每次计算完成时,它迭代+1,这意味着它算作一年过去了,然后打印出过去的年数 预期结果: /人口 起始尺寸:100 末端尺寸:1000

我的目标的细节:假设我们有n头骆驼。每年有n/3头新骆驼出生,n/4头骆驼死亡

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

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

预期结果:
/人口
起始尺寸: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);
}
#包括
#包括
内部主(空)
{
int start_pop_size;
int end_pop_尺寸;
几年过去了;
//TODO:提示输入开始大小
做
{
start_pop_size=get_int(“输入起始人口大小:\n”);
}
while(起始尺寸<9);
//TODO:提示输入端点大小
做
{
end_pop_size=get_int(“输入结束人口大小:\n”);
}

虽然(end_pop_size您事先不知道达到
end_pop_size
所需的迭代次数,因此您的状况不能基于
年数
。相反,您需要跟踪当前人口,并基于此决定何时结束循环:

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

您事先不知道需要多少次迭代才能达到
end\u pop\u大小
,因此您的状况不能基于
年数
。相反,您需要跟踪当前人口,并基于此决定何时结束循环:

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

建议的守则如下:

#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);
}
  • 干净地编译
  • 执行所需的功能
  • 使用
    size\u t
    数年,因为该值永远不会小于0
  • 将输入值与计算值分开
  • 请注意,当开始填充为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);
    }
    

    建议的守则如下:

    #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);
    }
    
  • 干净地编译
  • 执行所需的功能
  • 使用
    size\u t
    数年,因为该值永远不会小于0
  • 将输入值与计算值分开
  • 请注意,当开始填充为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);
    }
    

    @Jabberwocky:虽然婴儿美洲驼或25%死亡的美洲驼的想法有点可怕……提示:对于
    start\u pop\u size=100
    end\u pop\u size=1000000
    ,什么是
    start\u pop\u size/3-end\u pop\u size/4
    ?for循环会发生什么事?@Jabberwocky:虽然
    0.333333333
    一头小美洲驼,或者一头死了25%的美洲驼有点可怕……提示:如果
    start\u pop\u size=100
    end\u pop\u size=1000000
    ,那么什么是
    start\u pop\u size/3-end\u pop\u size/4
    ?for循环会发生什么?当然可以,但是do-while肯定会更多suitable@klutt当然,添加了替代方案。我更喜欢
    while
    公式,因为结束条件不是基于循环变量。是的,我同意您的解决方案更好。for版本甚至不值得一提。我只是反对不能使用它的说法。:)首先,我更新了代码如下://TODO:Calculate number of years int cur_pop_size=start_pop_size;years_passed=0;do{start_pop_size/3-end_pop_size/4;years_passed++}while(cur pop_sizecur\u pop\u size
    乌拉,不是几年过去了。好吧,你可以,但是一段时间当然更重要suitable@klutt当然,添加了替代方案。我更喜欢
    while
    公式,因为结束条件不是基于循环变量。是的,我同意你的解决方案更好。for版本甚至不值得一提。我只是反对cl首先我更新了我的代码如下://TODO:Calculate number of years int cur_pop_size=start_pop_size;years_passed=0;do{start_pop_size/3-end_pop_size/4;years_passed++}而(cur pop_sizecur\u pop\u sizeula,不是
    年\u过去的