The following equation describes the voltage of a capacitor
The following equation describes the voltage of a capacitor in a certain RC circuit as it is charging from a starting value of 0 volts to a final value of 10 volts: v = 10 - 10e^-15t Write a program to print a table of time and corresponding voltage values. In function main, prompt the user to enter a limiting voltage greater than 0 and less than 10 volts for the table and use a while or do while input validation loop to prompt the user to re-enter the limiting voltage value if it is outside the range between 0 and 10. Call a function to print a table of time and voltage values from t = 0 up until the time at which the voltage reaches the desired value. The only function parameter will be the limiting voltage value. Include column headings. Use a while loop to print the table. Print values at time intervals of 0.005 seconds. Print values only as long as the voltage is less than the limiting voltage input by the user (and passed as a function parameter). In the table, place time values in the column on the left and voltage values in the column on the right. Print the table using 3 digits after the decimal point for time and 5 digits after the decimal point for voltage. Test with a limiting voltage value of 6.3 volts.
Solution
#include<stdio.h>
#include<math.h>
void printVoltageTable(double limitingVoltage)
{
double voltage = 0;
double t = 0;
printf(\"time\\t\\tVoltage\ \");
printf(\"=======================\ \");
while(voltage < limitingVoltage)
{
voltage = 10 - 10*exp(-15*t);
printf(\"%.3f\\t\\t%0.5f\ \", t, voltage);
t = t+ 0.005;
}
}
int main()
{
double limitingVoltage;
while(1)
{
printf(\"Enter a limiting voltage to print table: \");
scanf(\"%lf\", &limitingVoltage);
if (limitingVoltage < 0 || limitingVoltage > 10)
{
printf(\"Please enter a value in range of 0 and 10\ \");
}
else
{
break;
}
}
printVoltageTable(limitingVoltage);
return 0;
}
