// 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; std::cout << "Start time is " startTime.hour << ":" << startTime.minute; std::cout << ":" << 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; } } |
void printTimes(Time alltimes[], int size) { for (int t = 0; t < size; t++) { cout << "Hour: "; cout << alltimes[j].hour; cout << ", minute: "; cout << alltimes[j].minute; cout << endl; } } |
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]); } // have the function print all the times printTimes(timevals, size); } |
const int NumAssigns = 5; // assuming this is the same for all students in a class struct Student { string name; float marks[NumAssigns]; }; // get the name and marks for a student void fill(Student& s); // display the name and marks for a student void print(Student s); int main() { const int MaxStudents = 32; // max number of students the program can handle Student students[MaxStudents]; // create an array big enough for the max int numStudents; // actual number of students for this run cout << "Please enter the number of students (max " << MaxStudents << "): "; cin >> numStudents; // of course we'd want to add error checking/repetition here // get all the students' info for (int s = 0; s < numStudents; s++) { fill(students[s]); // fill in the info for the student in position s } // print all the students' info for (int s = 0; s < numStudents; s++) { print(students[s]); // display the info for the student in position s } } void fill(Student& s) { cout << "Enter their name: "; cin >> s.name; // just grabs the name as one word in this example for (int m = 0; m < NumAssigns; m++) { cout << "Enter their mark for assignment " << m << ": "; cin >> s.marks[m]; // again, we'd want to add error checking } } void print(Student s) { cout << s.name << ":"; for (int m = 0; m < NumAssigns; m++) { cout << " " << s.marks[m]; // print out the next mark for this student } cout << endl; } |
void printAll(Student allstudents[], int size) { for (int snum = 0; snum < size; snum++) { print(allstudents[s]); } } |
{ ... the same as the earlier main prior to the final for loop ... printAll(students, numStudents); } |