1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
//Boltzmann population program.
//Libraries being used.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void getEnergies(double & E1, double & E2);
double getTemperature();
void calculation(double E1, double E2, double temperature);
int main()
{
int option;
double E1;
double E2;
double temperature;
// Introductory Message
printf("This Program calculates the Botlzmann population ratio for two ");
printf("energy levels at differing temperatures.\n");
getEnergies(E1, E2); // Asks the user to input the two energy levels.
temperature = getTemperature(); //Asks the user to input the temperature.
// Checks if the option is correct for the calculation.
do
{
printf("\nYour temperature is %g K, E1 is %g J and E2 is %g J,", temperature,E1,E2);
printf("\n\n Are these the correct values for your calculation?\n\n");
printf(" 1. Change just your energy levels\n");
printf(" 2. Change just your temperature\n");
printf(" 3. Change both your energy levels and temperature\n");
printf(" 4. Continute with the already entered values\n");
printf(" 0. EXIT the program\n");
printf("\n Please type and enter an option: ");
scanf("%d",&option);
switch (option)
{
case 1:
getEnergies(E1, E2);
break;
case 2:
temperature = getTemperature();
break;
case 3:
getEnergies(E1, E2);
temperature = getTemperature();
break;
case 4:
calculation(E1, E2, temperature);
break;
}
} while (option != 0);
return 0;
}
//-----------------------------------------------------------------------------
void getEnergies(double & E1, double & E2)
{
printf("\nPlease would you enter the two energy levels you would ");
printf("like to workout the Boltzmann ratio for in Joules.\n");
while (true) {
E1 = 0.0;
E2 = 0.0;
// Energy1
printf("\n E1 = ");
scanf("%lg",&E1);
// Energy2
printf(" E2 = ");
scanf("%lg",&E2);
if ((E1 == 0.0) || (E2 == 0.0))
{
printf("Please ensure E1 and E2 are non zero\n");
continue;
}
if (E2 > E1) {
break; // SUCCESS, E2 is bigger than E1
}
printf("Please ensure E2 is larger than E1\n");
}
}
//-----------------------------------------------------------------------------
double getTemperature()
{
printf("\nPlease input the temperature of the system in Kelvins.");
double result;
while (true)
{
result = 0.0;
printf("\n\n Temp = ");
scanf("%lg",&result);
// Ensures the temperature is a valid value.
if (result > 0.0) {
return result;
}
printf("Please input a temperature greater than 0 K.\n");
}
}
//-----------------------------------------------------------------------------
void calculation(double E1, double E2, double temperature)
{
const double k = 1.3806488e-23;
double deltaE = E2-E1;
double answer = exp(-deltaE/(temperature*k));
printf("\n\n Your answer is %g \n", answer);
printf("\n Press enter to continue\n");
// wait for a key press
fflush(stdin);
getchar();
}
//------------------------------------------------------------------------------
|