代码从时间0-100开始打印一个2D数组一天通过时间[O]。这些功能将是另一个空隙还是在int中。同样,如果我尝试... rp(植物的再生产(= t [i] .plants 30;它不会编译和运行,因为我无法在第1天结束时添加30个植物。直到第100天。
试图在植物羚羊和老虎方面打印类似的东西,以及涉及出生率,垂死率和繁殖等曲外的时间。时间将是0-100,但其他值将会改变。在此处输入图像描述
谢谢您的帮助,我非常感谢。
#include <iostream>
#include <string>
using namespace std;
struct population {
string Plants;
string Antelopes;
string Tigers;
double time[100];
};
void input (population[]);
void print (population[]);
void initialize (population[]);
void birthrate (population []);
int main ()
{
int Time[100];
int Plants[100];
int Antelopes[100];
int Tigers[100];
cout << " Time - Plants - Antelopes - Tigers n";
cout << " --------------------------------------- n";
for ( int i = 0; i <= 100; i++)
{
Time[0]= i ;
Plants[0] =i;
Antelopes[0] =i;
Tigers[0] =i;
cout << " " << Time[0] << " ---- " << " " << Plants[0] << " ---- " << " " << Antelopes[0] << " ---- " << " " << Tigers[0] << endl;
}
// int plants;
// cout << " Enter value for plants " << endl;
// cin >> plants;
}
void initialize (population t[])
{
for (int i=0; i < 1; i++)
{
t[i].Plants = "";
t[i].Antelopes = "";
t[i].Tigers = "";
for (int j=0; j < 100; j++)
t[i].time[j] = 0;
}
}
void input (population t[])
{
for (int i=0; i < 1; i++)
{
cout << " Enter initial population for Plants ";
cin >> t[i].Plants;
cout << " n Enter initial population for Antelopes ";
cin >> t[i].Antelopes;
cout << " n Enter initial population for Tigers ";
cin >> t[i].Tigers;
for (int j=0; j < 100; j++)
{
cout << "n Enter time ";
cin >> t[i].time[j];
}
}
}
void print (population t[])
{
for (int i=0; i < 1; i++)
{
cout << " Plants Pop: " << t[i].Plants << endl;
cout << " Antelopes Pop: " << t[i].Antelopes << endl;
cout << " Tigers Pop: " << t[i].Tigers << endl;
for (int j=0; j < 100; j++)
cout << " " << t[i].time[j];
cout << endl;
}
}
void birthrate (population t[])
{
for (int i=0; i <= 100; i++)
{
string Rp;
Rp = (t[i].Plants);
cout << " " << Rp << endl;
for (int j=0; j < 100; j++)
cout << t[i].Plants[j] << " ";
cout << endl;
}
}
您的代码有许多重大问题,以阻止有关如何进行的良好建议。
您正在上一堂课来存储所有流行值,但这是使用字符串。代替存储数字数据。
那么,您甚至根本没有在主循环内使用该类,而是您制作了一些完全无连接的新变量并使用它们。基本上拼出多余的变量。这是一个基本轮廓:
struct sample
{
float pop;
float deaths;
float births;
};
struct population
{
sample Plants;
sample Antelopes;
sample Tigers;
}
现在,每个"人口"对象将在一个时间点上放置整个人口的快照。如果您想存储100个快照(这不是真正必要的(,则可以制作100个人口对象的数组:
population pop[100];
,几乎可以肯定的是,只有一个存储当前时间切片的人群对象:
population ecoSystem;
,只需在更新时打印值即可。绝对无需存储价值100个时间点的数据,除非将其保存到文件中。您将访问这样的值:
void input(population& p) // & = pass in a live reference to the population object
{
cout << " Enter initial population for Plants ";
cin >> p.Plants.pop;
cout << " n Enter initial population for Antelopes ";
cin >> p.Antelopes.pop;
cout << " n Enter initial population for Tigers ";
cin >> p.Tigers.pop;
}
也没有基本原因一次输入100个数据点,您只需循环100次循环,并拥有一个单独的" int t"时间变量,您要在哪个时间段。最多,您需要当前的总体变量,这是下一个slice的另一个总体变量,然后在计算更新后将其复制回当前一个。