2
Lecture 1 -- 2Computer Science I - Martin Hardwick C++ structs rSyntax:Example: rThink of a struct as a way to combine heterogeneous data values together to form a new more complex data type. l you can use this new data type to declare variables l you can create arrays with this new data type rPlace struct definitions at the beginning of a program, after include lines and before functions and function prototypes. rTo access the components of a struct, use the dot operator. l can be used anywhere an ordinary variable can be used struct { ;... }; struct acct { int acctnum; string name; float balance; }; acctmyAccount; myAccount.acctnum = 99999; myAccount.name = Hardwick; myAccount.balance = 10000;

3
Lecture 1 -- 3Computer Science I - Martin Hardwick Bank Simulation (2) int main () //PURPOSE: simulate a small bank //PRECONDITIONS: existing // accounts in file account.txt // in project folder //POSTCONDITIONS: finds, inserts, // deletes, and lists accounts { // list of bank accounts acct bank[1000]; // total number of accounts intnumaccts; // file with list of bank accounts ifstreamvault; // user request stringcommand; // account number to find intgoal; rThe main function creates the list of bank accounts and processes user commands to perform transactions. rAssume that there will be no more than 1000 accounts. rThe existing accounts are read from a file at the start of the program. l this creates the bank array for the program to use rUser commands are typed as strings (e.g., find, insert, exit).

4
Lecture 1 -- 4Computer Science I - Martin Hardwick Bank Simulation (3) // subscript of account in bank intloc; // a bank account acctaccount; // read existing accounts from file vault.open("accounts.txt"); numaccts=0; vault >> bank[0].num >> bank[0].name >> bank[0].balance; while (!vault.eof()) { numaccts = numaccts + 1; vault >> bank[numaccts].num >> bank[numaccts].name >> bank[numaccts].balance; } // display dollar values correctly cout << setiosflags(ios::fixed) << setprecision(2); rThe program assumes that a file named accounts.txt contains the data for existing accounts. rNote the position of a subscript when using an array of struct types. rThis is the standard while loop for reading data from a file until the end of file is reached. rDollar values should be displayed with two digits to the right of the decimal point. l the last two lines instruct the cout object to do this when displaying float values

6
Lecture 1 -- 6Computer Science I - Martin Hardwick Function Find (1) int find(acct bank[ ], int numaccts, int goal) //PURPOSE: search for a goal account // in the list of accounts //PRECONDITIONS: 0 <= numaccts, // numaccts is number of accounts // in list of accounts (i.e., bank) //POSTCONDITIONS: returns the // subscript of goal account or -1 if // the goal account is not found { int k;// loop variable rParameters: list of accounts, number of accounts, number of the goal account to find. rReturn: subscript of goal account if found, or –1 to indicate that it is not in the list. rSearch the list from its beginning, element by element until: l find the account l reach the end of the list rThis is called a sequential search.

7
Lecture 1 -- 7Computer Science I - Martin Hardwick Function find (2) //search for the goal account in //the list of accounts for (k=0; k<numaccts; k++) { if (bank[k].num == goal) { return k; } // didn't find goal account return -1; } rIf the goal number is in the account list, it will be found by the for loop. l if it is found at subscript k, return k and terminate the function rIf the loop ended because k is no longer less than numaccts, then the entire list was searched and the goal account was not found. l in this case, return -1

11
Lecture 1 -- 11Computer Science I - Martin Hardwick Function insert (1) void insert(acct bank[ ], int &numaccts, acct newacct) //PURPOSE: insert a new account into // the account list //PRECONDITIONS: 0 <= numaccts, // numaccts is number of accounts // in account list, newacct is a new // account to be added, account // list is in order by account num //POSTCONDITIONS: account list // modified to add new account, // order of account list maintained { intk,j;// loop variables rParameters: list of accounts, number of accounts, new account to add to the list. rBoth the account list (i.e., bank) and the number of accounts (i.e., numaccts) will change. l numaccts must be passed by reference l arrays are always passed by reference rSince the list order must be maintained, we must find where in the list the new account should be placed. rOnce we know where it goes, we must make room in the list to insert the new account.

12
Lecture 1 -- 12Computer Science I - Martin Hardwick Inserting Into An Order List 1.Find where the new item goes. 2.Make room for the new item. 3.Insert the new item into the list. 0 10 20 30 40506070 8 Number of elements in the list: Insert 45 70605045 9

13
Lecture 1 -- 13Computer Science I - Martin Hardwick Function insert (2) // find place to put new account in list // to maintain order of list k=0; while ((bank[k].num < newacct.num) && (k < numaccts)) { k = k + 1; } // add new account to list if (k == numaccts) { // goes at end bank[numaccts] = newacct; numaccts = numaccts + 1; } rThe loop terminates when: l we find the first account in the list with a larger account number l we reach the end of the list having found no larger account numbers rIf there are no larger account number in the list, then k equals the number of accounts at the end of the loop. l in this case, add the new account to the end of the list

14
Lecture 1 -- 14Computer Science I - Martin Hardwick Function insert (3) else { // goes in middle // make room to add it for (j=numaccts; j>k; j--) { bank[j] = bank[j-1]; } // add it to vacated spot bank[k] = newacct; numaccts = numaccts + 1; } rOtherwise, the new account belongs in element k of the list. rMove all elements from k to the end of the list, one element to the right. l it is easiest to do this in reverse order rNow we can put the new account into element k of the list without overwriting another account. rVariable numaccts must be incremented since a new account has been added to the list.