// declare a data type to represent time,
// with ints for hours and minutes,
// and a float for seconds (0-59.9999)
struct Time {
int hour; // 0-23
int minute; // 0-59
float second; // 0-59.9999
};
|
Time startTime = { 11, 30, 0.00 }; // sets hour, minute, and seconds
|
startTime.hour = 9;
startTime.minute = 20;
startTime.second = 0.01;
printf("Start time is %02d:%02d:%05.2f\n", startTime.hour, startTime.minute, startTime.second);
|
// sets time to 00:00:00
void setTime(Time &T);
int main()
{
Time t;
setTime(t);
}
void setTime(Time &T)
{
T.hour = 0;
T.minute = 0;
T.second = 0;
}
|
int main()
{
const int size = 10;
Time timevals[size]; // creating the array
// filling it one element at a time with calls to setTime
for (int i = 0; i < size; i++) {
setTime(timevals[i]);
}
// accessing the elements directly through the array
for (int j = 0; j < size; j++) {
cout << "Hour: ";
cout << timevals[j].hour; // the hour field of the j'th element
cout << ", minute: ";
cout << timevals[j].minute; // the minute field of the j'th element
cout << endl;
}
}
|