CMPT 102 Lab #7
November 13-17 2006
This lab is designed to give further
practice with structures
Practice Problem 1: Problem 7, p447 of you textbook
Create a structure type to represent a battery. A batteryType includes components, voltage, energy storage
capacity (joules), and present energy stored (joules) and battery serial number
(a 6 digit number). Define functions for input and output of a structure of
type batteryType, and to compare two structures of
type batteryType to see if they are equivalent
batteries. To be equivalent the voltage and energy storage capacity must be the
same. The present energy stored need not be the same and the serial numbers
should not be the same. Also a function,
powerDevice, which takes the current of an electrical
device (amps), the time that device is to be powered
(seconds) and a structure of type batteryType as
arguments. The function, powerDevice determines if
the is enough energy stored in the battery to power the device for the
prescribed time. If there is not enough energy the function returns to value 0
to indicate that a different battery is needed for the task. It there is enough
power in the battery, the required energy for running the device is subtracted
from the present stored energy in the battery function then returns 1 to
indicate that the battery should be used.
Also define a function maxTime that takes a
battery and the current of an electrical device as input parameters and returns
the number of seconds the battery can operate that device before it is fully
discharged. This function does not
change any of the values in the batteryType
structure. Write another function recharge that sets the present energy stored
to the energy storage capacity of the batteryType
structure. To relate these quantizes remember that power (Watts) is equal to
Voltage (Volts)*Current (amps), and that energy (joules) is equal to power (Watts)*time (seconds). The prototypes for these functions
should be:
void printBattery( struct
batteryType btype);
struct batteryType readBattery(FILE *filep);
int compareBatteries(struct batteryType b1, struct batteryType b2);
int powerDevice(double
current, int seconds, struct
batteryType *b1);
int maxTime(double
current, struct batteryType
b1);
void rechargeBattery(struct
batteryType *b1);
Create a main function that declares three batteryType structures. Read data from a file to initialize
these structures to represent two 12-V car batteries with maximum energy
storage of 5*106 joules, and one 12V flashlight battery with a
maximum energy storage capacity of 2*104 joules. Print these
batteries. Use the first car battery to power a 4-amp light for 15 minutes then
print battery 1 again. Next find out how long that battery’s remaining energy
could power an 8 amp device. After recharging the battery, print the battery
again then redetermine how long it could operate the
8 amp device. Find out how long the second car battery could power a 6 amp
lamp. Compare the two car batteries and
determine they are equivalent, Compare a car battery to a 12-V flashlight
battery and determine they are not equivalent.