我正在创建一个具有以下输出和输入(2.0)和(3.0)的程序:
0.38s后氨浓度= 1.729 mol/L
速率降到0.040 mol/(L*s)以下需要3.39s
3.39s后氮气浓度= 0.603 mol/L
3.39s后水的平均浓度= 1.148 mol/L
反应已完成50%
然而,由于某种原因,我的while循环没有迭代那么远。下面是我使用的代码:
#include <stdio.h>
#include <math.h>
int main() {
//Define value of k, starting concentration of NH3,starting concentration of O2,current concentration of NH3,current concentration of O2, current concentration of N2, current concentration of water, waterProduced, reactionRate and time as floats; initialize k and time, since k is a constant and t has a fixed starting time
float ammoniaStart,oxygenStart,ammoniaCurrent,oxygenCurrent,nitrogenCurrent, waterCurrent, waterProduced,reactionRate;
float k = 1.14*pow(10,-2);
printf("k = %fn",k);
float time = 0.38;
//Define int i: used as an iterator
int i = 0;
//Scan in the starting concentrations of O2 and NH3
printf("Enter initial ammonia & oxygen concentrations:");
scanf("%f %f",&ammoniaStart,&oxygenStart);
ammoniaCurrent = ammoniaStart;
oxygenCurrent = oxygenStart;
printf("ammoniaCurrent = %f, oxygenCurrent = %fn",ammoniaCurrent,oxygenCurrent);
//While loop: while the concentration of NH3 is greater than the starting concentration of NH3 / 2 OR while the concentration of oxygen is greater than the starting concentration of oxygen / 2
while(ammoniaCurrent > (ammoniaStart / 2) || reactionRate > 0.04){
//Calculate the reaction rate using equation 1: reactionRate = k*current concentration of NH3*(pow(current concentration of O2, 2));
reactionRate = k*ammoniaCurrent*(pow(oxygenCurrent, 2));
printf("reaction rate = %fn",reactionRate);
//Calculate the current concentration of N2 by rearranging equation 2: current concentration of N2 = reactionRate*2*time;
nitrogenCurrent = nitrogenCurrent + reactionRate*2*(time-0.38);
//Calculate the current concentration of water by rearranging equation 2: current concentration of water = reactionRate*6*time;
waterCurrent = waterCurrent + reactionRate*6*(time-0.38);
//Add the current concentration of water to waterCurrent
waterProduced = waterProduced + waterCurrent;
//Calculate the current concentration of NH3 by rearranging equation 2: current concentration of NH3 = reactionRate*4*time*-1;
printf("ammoniaCurrent = %f - %fn",ammoniaCurrent, (reactionRate*4*(time-0.38)));
ammoniaCurrent = ammoniaCurrent - (reactionRate*4*(time-0.38));
printf("ammoniaCurrent = %fn",ammoniaCurrent);
//printf("%f now, %f thenn",ammoniaCurrent, ammoniaStart);
//Calculate the current concentration of O2 by rearranging equation 2: current concentration of O2 = reactionRate*3*time*-1;
oxygenCurrent = oxygenCurrent - (reactionRate*3*(time-0.38));
//Else, print “Reaction has not passed 50% completion”.
printf("Reaction has not passed 50%% completionn");
//Add 1 to i
i++;
//Increment time by 0.01 seconds
time = time + 0.01;
printf("time = %fn",time);
}
//Print "Concentration of ammonia after 0.38s = %f mol/L",current concentration of NH3 = reactionRate*4*0.38*-1
printf("Concentration of ammonia after 0.38s = %f mol/Ln",(ammoniaStart - (k*ammoniaStart*(pow(oxygenStart,2))*0.38*4)));
//Print "Average concentration of water after %fs = %f mol/L",t, waterCurrent/i
waterCurrent = waterCurrent / i;
printf("Average concentration of water after %f s = %f mol/Ln",time, waterCurrent);
//Print "%fs required for rate to drop below 0.040 mol/(L*s)"
printf("%fs required for rate to drop below 0.040 mol/(L*s)n",time);
//Print "Concentration of nitrogen gas after %fs = current concentration of N2 mol/L"
printf("Concentration of nitrogen gas after %fs = %f mol/Ln",time,nitrogenCurrent);
//Print "Average concentration of water after %fs = concentration of water mol/L"
printf("Average concentration of water after %fs = %f mol/Ln",time,waterCurrent);
//Print “Reaction has passed 50% completion”.
printf("Reaction has passed 50%% completionn");
//Missing: when to print that the reaction has not passed 50% completion, using nested while loops (check with someone ASAP for this one)???
}
当我输入所需的值时,我得到结果输出:
0.38s后氨浓度= 1.688096 mol/L
0.660000 s后平均水浓度= 0.068756 mol/L
速率降到0.040 mol/(L*s)以下需要0.66000 s
0.66000s后的氮气浓度= 0.641721 mol/L
0.6600s后平均水浓度= 0.068756 mol/L
反应已完成50%
是什么导致它停得这么快?
变量reactionRate
,nitrogenCurrent
,waterCurrent
和waterProduced
从未初始化。因此,它们的初始值是不确定的,并且它们可能包含一个不确定的看似随机的值。
在使用它们之前将它们初始化为0
将main
的第一行更改为:
int main() {
float ammoniaStart, oxygenStart, ammoniaCurrent, oxygenCurrent;
// >>>> initialize those variables to zero
float waterCurrent = 0, waterProduced = 0, reactionRate = 0, nitrogenCurrent = 0;
通过这些更改,您可以得到预期的输出。
重写没有无意义注释的代码,并使用double
。
#include <stdio.h>
#include <math.h>
int main() {
//Define value of k, starting concentration of NH3,starting
// concentration of O2,current concentration of NH3,current
// concentration of O2, current concentration of N2, current
// concentration of water, waterProduced, reactionRate and time
// as doubles; initialize k and time, since k is a constant and
// t has a fixed starting time
double ammoniaStart, oxygenStart, ammoniaCurrent, oxygenCurrent;
// >>>> initialize those variables to zero
double waterCurrent = 0, waterProduced = 0, reactionRate = 0, nitrogenCurrent = 0;
double k = 1.14 * pow(10, -2);
printf("k = %fn", k);
double time = 0.38;
int i = 0;
//Scan in the starting concentrations of O2 and NH3
printf("Enter initial ammonia & oxygen concentrations:");
scanf("%lf %lf", &ammoniaStart, &oxygenStart);
ammoniaCurrent = ammoniaStart;
oxygenCurrent = oxygenStart;
printf("ammoniaCurrent = %f, oxygenCurrent = %fn", ammoniaCurrent, oxygenCurrent);
// while the concentration of NH3 is greater than the starting concentration
// of NH3 / 2 OR while the concentration of oxygen is greater than the starting concentration of oxygen / 2
while (ammoniaCurrent > (ammoniaStart / 2) || reactionRate > 0.04) {
//Calculate the reaction rate using equation 1: reactionRate = k*current concentration of NH3*(pow(current concentration of O2, 2));
reactionRate = k * ammoniaCurrent * (pow(oxygenCurrent, 2));
printf("reaction rate = %fn", reactionRate);
//Calculate the current concentration of N2 by rearranging equation 2: current concentration of N2 = reactionRate*2*time;
nitrogenCurrent = nitrogenCurrent + reactionRate * 2 * (time - 0.38);
//Calculate the current concentration of water by rearranging equation 2: current concentration of water = reactionRate*6*time;
waterCurrent = waterCurrent + reactionRate * 6 * (time - 0.38);
waterProduced = waterProduced + waterCurrent;
//Calculate the current concentration of NH3 by rearranging equation 2: current concentration of NH3 = reactionRate*4*time*-1;
printf("ammoniaCurrent = %f - %fn", ammoniaCurrent, (reactionRate * 4 * (time - 0.38)));
ammoniaCurrent = ammoniaCurrent - (reactionRate * 4 * (time - 0.38));
printf("ammoniaCurrent = %fn", ammoniaCurrent);
//printf("%f now, %f thenn",ammoniaCurrent, ammoniaStart);
//Calculate the current concentration of O2 by rearranging equation 2: current concentration of O2 = reactionRate*3*time*-1;
oxygenCurrent = oxygenCurrent - (reactionRate * 3 * (time - 0.38));
printf("Reaction has not passed 50%% completionn");
i++;
time = time + 0.01;
printf("time = %fn", time);
}
printf("Concentration of ammonia after 0.38s = %f mol/Ln", (ammoniaStart - (k * ammoniaStart * (pow(oxygenStart, 2)) * 0.38 * 4)));
waterCurrent = waterCurrent / i;
printf("Average concentration of water after %f s = %f mol/Ln", time, waterCurrent);
//Print "%fs required for rate to drop below 0.040 mol/(L*s)"
printf("%fs required for rate to drop below 0.040 mol/(L*s)n", time);
printf("Concentration of nitrogen gas after %fs = %f mol/Ln", time, nitrogenCurrent);
printf("Average concentration of water after %fs = %f mol/Ln", time, waterCurrent);
printf("Reaction has passed 50%% completionn");
//Missing: when to print that the reaction has not passed 50% completion, using nested while loops (check with someone ASAP for this one)???
}