Lec 17B Project 2: Arrays of Structs 1 Introduction A common way to hold data is in an array. A common way to represent data is with a struct. Many times you will need to put them both together. Accessing the array of structs is fairly easy, once you get used to it. 2 Example--an inventory record struct Record { string ID; int Value; string Description; }; 3 An Array of 5 Records Record AutoParts[5]; AutoParts 0 1 2 3 4 ID Value Description This is AutoParts[0] It has 3 pieces: .ID, .Value and .Description 4 Storage and Retrieval—By Fields Record AutoParts[5]; AutoParts[0].ID=“3G45-A”; AutoParts[0].Value=39.99; AutoParts[0].Description=“Tire”; AutoParts 0 ID Value Description 1 2 3 4 3G45-A 39.99 Tire 5 Storage and Retrieval—By Fields Display AutoParts[0] first cell in array cout<<AutoParts[0].ID<<“ ”; cout<<AutoParts[0].Value<<“ ”; cout<<AutoParts[0].Description<<endl; AutoParts 0 ID Value Description 1 2 3 4 3G45-A 39.99 Tire cin works the same way! cin>>AutoParts[0].ID >> (…etc) 6 Your Turn Write C++ to store the information for a Muffler, costing $89.89, and ID “1E73-M” at index 4 of the AutoParts array AutoParts 0 ID Value Description 1 2 3 4 3G45-A 39.99 Tire 7 Inputing Records using placeholders string ID, Description; float Value; //read in a some values that need to be stored in >> ID >> Value >> Description; //Store them in an array location MyArray[0].ID = ID; MyArray[0].Value = Value; MyArray[0].Description = Description; You can fill the entire array this way, like in a loop or something (replacing [0] with [k] or loop counter) Now you can do things like sort or print them 8 Storage and Retrieval—By whole Record Record Blank; Blank.ID=“------”; Blank.Value=0.0; Blank.Description=“------”; AutoParts[1]=Blank; AutoParts 0 ID Value Description 1 2 A “Blank” Record Blank -----0.0 ------ 3 4 3G45-A -----39.99 0.0 Tire -----9 Storage and Retrieval—By whole Record Record Purchase; Purchase=AutoParts[0]; Display(Purchase); A “Purchased” Part Purchase 3G45-A 39.99 Tire AutoParts 0 ID Value Description 1 2 3 4 3G45-A -----39.99 0.0 Tire -----10 Storage and Retrieval—For Loops //set the whole array to Blank for ( int i =0; i < 5; i++ ) { AutoParts[i].ID=“------”; AutoParts[i].Value=0.0; AutoParts[i].Description=“------”; } OR (simpler) This uses “aggregate” assignment for ( int i =0; i < 10; i++ ) AutoParts[i] = Blank; 11 Function to Display a single Struct Record void Display(Record rec) { cout<<rec.ID<<“ ”; cout<<rec.Value<<“ ”; cout<<rec.Description<<endl; } 12 Display an Array of Struct Records void DisplayAll( Record rec[ ], int N) { for ( int i =0; i < N; i++ ) { cout<<“Record ”<<i<<“: ”; cout<<rec[i].ID<<“ ”; cout<<rec[i].Value<<“ ”; cout<<rec[i].Description<<endl; } } 13 Your Turn Make a function call to Display the last AutoPart Record: Make a function call to Display the whole AutoPart array: 14 Before continuing….let’s see how to apply this to project2 You have a file You also have struct Account { string name; int ID, PIN; }; How do you read the file into an array of Accounts?15 Prepare to read the file 1) Create an input file stream, open to accounts.txt 2) Create an array of Accounts, an integer k, and a temp Account 16 Read the file 3) Read one line of the file into temp (or array cell k) 4) Extend with a while loop to read the whole file 17 Sorting—Selection Sort Variation void Sort ( Record Array[], int size ) { Record TempRecord; int small_pos ; for ( int i=0; i < size-1; i++ ) { small_pos = i; for ( int j = i+1; j < size; j++ ) 18 Swap { if ( Array[small_pos].ID > Array[j].ID ) small_pos = j; } TempRecord = Array[i]; Array[i] = Array[small_pos]; Array[small_pos] = TempRecord; } } Here the “key” field to compare Records by is ID 19 Printing to a file Formatting for ( int i=0; i<size; i++ ) out << setw(15) << Array[i].ID <<setw(15) << Array[i].Value << setw(40) << Array[i].Description << endl; 20 Go to it! You are now ready to complete problems 811 in Lab11Structs.cpp 21
© Copyright 2025 Paperzz