

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: Programming Fundamentals II; Subject: Computer Science - CSCI; University: Texas A & M University-Commerce; Term: Spring 2004;
Typology: Study notes
1 / 2
This page cannot be seen from the preview
Don't miss anything!


1 struct Date 2 { 3 int month; 4 int day; 5 int year; 6 }; 7 struct Machine 8 { 9 int id; 10 bool serviceContract; 11 float failRate; 12 int downDays; 13 float cost, 14 deprValue; 15 }; // LOCAL VARIABLE DECLARATIONS 16 Date yesterday; 17 Date today = { 3, 2, 2004 }; // compile-time initialization 18 Machine press; 19 Machine station [100]; The following statements demonstrate references to fields (fully qualified names must be used for all field identifiers). You can do anything with a field that you can do with a single variable of the same type. 20 cout << "Date: " << today.month << "/" << today.day << "/" << today.year << endl; 21 cout << "Machine # " << press.id << endl; 22 cout << "Original cost = $" << setiosflags(ios::showpoint) << setprecision(2) << press.cost << endl; 23 cout << "Depreciation value = $" << press.deprValue << endl; 24 cout << "# down days since purchase = " << press.downDays << endl; 25 if ( press.failRate > MAX_FAIL_RATE) 26 cout << "Excessive downtime\n"; 27 if ( press.serviceContract == false) // or if(! press.serviceContract ) 28 cout << "Service contract has expired\n"; The only defined operations for structure variables (the entire structure) of the same type are: assignment pass as a parameter to a function (either by value or by reference) return by a function 29 yesterday = today ; is equivalent to these three statements: 30 // yesterday.month = today.month ; 31 // yesterday.day = today.day ; 32 // yesterday.year = today.year ;
Another example of structure assignments: 1 station[5] = press; is equivalent to: 2 station[5].id = press.id; 3 station[5].serviceContract = press.serviceContract; 4 station[5].failRate = press.failRate; 5 station[5].downDays = press.downDays; 6 station[5].cost = press.cost; 7 station[5].deprValue = press.deprValue; The following statements demonstrate references to elements of an array of structures. Values are input from a text file to be assigned to successive elements of the station array. char contractCode; int stationNbr, n; bool found; 1 stationNbr = 0; // number of elements currently assigned in array // and subscript of next available array element 2 while (inFile >> station[stationNbr].id >> contractCode)) { 3 if (contractCode == 'Y') 4 station[stationNbr].serviceContract = true; 5 else station[stationNbr].serviceContract = false; 6 inFile >> station[stationNbr].failRate ; 7 inFile >> station[stationNbr].downDays ; 8 inFile >> station[stationNbr].cost
station[stationNbr].deprValue ; 9 stationNbr ++ ;
} // After the station array has been loaded, it can be searched to see if // a particular machine is represented in the array. For example, the // following code will search the array to see if there is a match on // press (assume the press structure variable has been assigned values). 10 n = 0; 11 found = false; 12 while (n < stationNbr &&! found) 13 if ( station[n].id == press.id ) 14 found = true; 15 else n++; // If press is not in the array, insert it into the array in the next // available position, assuming there is space in the array. 16 if (! found) { 17 station[stationNbr] = press; // structure-to-structure assignment 18 stationNbr++; }