Exercise 2: Battery Model
it is capable of storing, and the energy it is currently storing (in joules).
(b) Define a function PowerDevice that takes as arguments: a current of an electrical device (amps), a battery of type
BatteryModel, and the time the device is to be powered by the battery (seconds), and determines whether the battery's
energy reserve is adequate to power the device.
If so, the function updates its reserve by subtracting the energy consumed
and returns the value 1.
Otherwise it returns the value 0 and leaves the energy reserve unchanged.
(c) Define a function MaxTime that takes as arguments: a current of an electrical device and a battery of type BatteryModel,
and returns the number of seconds this battery can operate the device before it is fully discharged.
(d) Define a function Recharge that sets a battery of type BatteryModel representing the present energy reserve to its maxi-
the battery's remaining energy could power an 8-A device.
#include <stdio.h>
#include <stdlib.h>
#define MAX 150
double RESERVE = 100;
int PowerDevice(double current, char *batteryModel, double time)
{
double energy = time * current / 1.4;
if(RESERVE >= energy)
{
RESERVE = RESERVE - energy;
printf("The new battery volume is %f\n", RESERVE);
return 1;
}
else
return 0;
}
double MaxTime(double current, char *batteryModel)
{
return 1.4 * RESERVE / current;
}
void Recharge(char *batteryModel)
{
RESERVE = MAX;
}
int main()
{
double current = 5.5;
double time = 10;
PowerDevice(current, "azaz", time);
printf("Max time = %f\n", MaxTime(current, "azaz"));
printf("Reserve = %f" ,RESERVE);
Recharge("azaz");
printf("Reserve = %f" ,RESERVE);
return 0;
}
Comments
Leave a comment