1 Lec 17B Project 2: Arrays of Structs. 2 Introduction A common way to hold data is in an array. A...
-
Upload
morgan-mae-george -
Category
Documents
-
view
213 -
download
0
Transcript of 1 Lec 17B Project 2: Arrays of Structs. 2 Introduction A common way to hold data is in an array. A...
1
Lec 17B Project 2: Arrays of Structs
2
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.
3
Example--an inventory record
struct Record
{
string ID;
int Value;
string Description;
};
4
An Array of 5 Records
Record AutoParts[5];
AutoParts 0 1 2 3 4
This is AutoParts[0]
It has 3 pieces: .ID, .Value and
.Description
ID
Value
Description
5
Storage and Retrieval—By Fields
Record AutoParts[5];
AutoParts[0].ID=“3G45-A”;
AutoParts[0].Value=39.99;
AutoParts[0].Description=“Tire”;
AutoParts 0 1 2 3 4
3G45-A
39.99
Tire
ID
Value
Description
6
Storage and Retrieval—By FieldsDisplay AutoParts[0] first cell in array
cout<<AutoParts[0].ID<<“ ”;cout<<AutoParts[0].Value<<“ ”;cout<<AutoParts[0].Description<<endl;
AutoParts 0 1 2 3 4
cin works the same way! cin>>AutoParts[0].ID >> (…etc)
3G45-A
39.99
Tire
ID
Value
Description
7
Your TurnWrite C++ to store the information for a Muffler, costing $89.89, and ID “1E73-M” at index 4 of the AutoParts array
AutoParts 0 1 2 3 4
3G45-A
39.99
Tire
ID
Value
Description
8
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
Inputing Records using placeholders
9
Storage and Retrieval—By whole RecordRecord Blank;
Blank.ID=“------”;
Blank.Value=0.0;
Blank.Description=“------”;
AutoParts[1]=Blank;
AutoParts 0 1 2 3 43G45-A ------
39.99 0.0
Tire ------
ID
Value
Description
A “Blank” Record
------
0.0
------
Blank
10
Storage and Retrieval—By whole RecordRecord Purchase;
Purchase=AutoParts[0];
Display(Purchase);
AutoParts 0 1 2 3 43G45-A ------
39.99 0.0
Tire ------
ID
Value
Description
A “Purchased” Part
3G45-A
39.99
Tire
Purchase
11
//set the whole array to Blankfor ( 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;
Storage and Retrieval—For Loops
12
void Display(Record rec)
{
cout<<rec.ID<<“ ”;
cout<<rec.Value<<“ ”; cout<<rec.Description<<endl;
}
Function to Display a single Struct Record
13
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;
}
}
Display an Array of Struct Records
14
Your Turn
Make a function call to Display the last AutoPart Record:
Make a function call to Display the whole AutoPart array:
15
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;
float balance;};
How do you read the file into an array of Accounts?
16
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
17
Read the file3) Read one line of the file into temp (or array cell k)
4) Extend with a while loop to read the whole file
18
Sorting—Selection Sort Variationvoid 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++ )
19
{
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
Swap
20
Printing to a file
for ( int i=0; i<size; i++ )
out << setw(15) << Array[i].ID <<setw(15)
<< Array[i].Value << setw(40)
<< Array[i].Description << endl;
Formatting
21
Go to it!
You are now ready to complete problems 8-11 in Lab17Structs.cpp