查找最大总体数和C中的最大代数,包括指针

时间:2015-07-11 15:57:21

标签: c pointers while-loop

我做了一个程序,看看细菌可以多快繁殖,我使用一个细菌容器,看看它有多大(以英尺为单位),营养水平有多高,以及细菌的温度是多少成长。

输出" t"的printf和" maxpop"总是为世代和人口输出1。

我的输出是:

Welcome to the population growth program...
What is the size of the container (in feet)?
Enter value --> 5

What is the nutrient level coefficient?
Enter value --> 3

What is the incubating temperature (in degree C.)
Enter value --> 33
maximum population reached after 1 generations
maximum population 1

我想要的输出是:

Welcome to the population growth program...
What is the size of the container (in feet)?
Enter value --> 22
22 is an unacceptable value for this data...
The value must be between 1 and 8
Enter value --> 0
0 is an unacceptable value for this data...
The value must be between 1 and 8
Enter value --> 8

What is the nutrient level coefficient?
Enter value --> 4
4 is an unacceptable value for this data...
The value must be between 0 and 3
Enter value --> 3

What is the incubating temperature (in degrees C.)
Enter value --> 80
80 is an unacceptable value for this data...
The value must be between 20 and 54
Enter value --> 37

maximum population reached after 91 generations
maximum population 608637958121099264
maximum population reached after 30.33 hours
population reaches 0 after 181 generations
colony lived for 60.33 hours

到目前为止,这段代码如下:

#include <stdio.h>
#include <math.h>

int readcont();
int readnutr();
int readtemp();
int getmaxpop(int, int, int, float, int*);

int main(){

    int cont, nutr, temp;
    int N, cont2, temp2, nutr2;
    int t=1, maxpop; 
    float k=0;


    printf("Welcome to the population growth program...\n");
    printf("What is the size of the container (in feet)?\n");

    cont = readcont();  

    printf("\nWhat is the nutrient level coefficient?\n");

    nutr = readnutr();

    printf("\nWhat is the incubating temperature (in degree C.)\n");

    temp = readtemp();
    printf("The feed is: %d \nThe nutrient level is: %d \nThe incubating temperature is: %d\n", cont, nutr, temp);

    temp2 = abs(temp2 = 37 - temp);
    nutr2 = 3 - nutr;
    cont2 = 8 - cont;


    maxpop = getmaxpop(cont2,nutr2,temp2,k,&t);


    printf("maximum population reached after %d generations\n", t);
    printf("maximum population %d\n", maxpop);



    return 0;


}

int readcont(){
    int x = 0;
    while(x < 1 || x > 8){
        printf("Enter value --> ");
        scanf("%d", &x);

        if(x < 1 || x > 8){
            printf("%d is an unacceptable value for this data...\n", x);
            printf("The value must be between 1 and 8\n");
        }
    }
    return x;   
}

int readnutr(){
    int x;
    while(x < 0 || x > 3){  
        printf("Enter value --> ");
        scanf("%d", &x);
        if(x < 0 || x > 3){
            printf("%d is an unacceptable value for this data...\n", x);
            printf("The value must be between 0 and 3\n");
        }
    }
    return x;
}

int readtemp(){
    int x;
    while(x < 20 || x > 54){        
        printf("Enter value --> ");
        scanf("%d", &x);

        if(x < 20 || x > 54){
            printf("%d is an unacceptable value for this data...\n", x);
            printf("The value must be between 20 and 54\n");
        }
    }
    return x;
}

int getmaxpop(int cont, int nutr, int temp, float k, int *t){
    int maxpop = 1, currpop = 1;

    for(*t =2 ; currpop < maxpop; *t++){
        maxpop = currpop;

        k = 0.005+(cont*0.003)+(nutr*0.004825)+(temp*0.001);
        currpop = 1 * pow(2.71828, ((1 - k) * (*t)));
    }
    *t--;
    return maxpop;
    return *t;
}

1 个答案:

答案 0 :(得分:1)

您的getmaxpop功能相当于

int getmaxpop(int cont, int nutr, int temp, float k, int *t)
{
    int maxpop = 1, currpop = 1;

    for(;currpop < maxpop;)
    {
        // never happens because currpop is not less than maxpop
    }
    return maypop;  // always returns 1
}