- •Vasyl Yatskiv,
- •Content
- •Introduction
- •1. Theory
- •1.1. Arithmetic Operators
- •1.2. Relational Operators
- •1.3. Logical Operators
- •1.4. Increment/Decrement Operators
- •1.5. Assignment Operator
- •1.6. Conditional Operator
- •1.7. Comma Operator
- •1.8. The sizeof Operator
- •1.9. Operator Precedence
- •1.10. Simple Type Conversion
- •2. Lab Overview
- •3. Report Structure
- •4. Control Exercises
- •5. References
- •Lab #2. Control structures
- •1. Theory
- •1.1. Simple and Compound Statements
- •1.2. The if Statement
- •1.3. The switch Statement
- •1.4. The while Statement
- •1.5. The do Statement
- •1.6. The for Statement
- •1.7. The continue Statement
- •1.8. The break Statement
- •1.9. The goto Statement
- •1.10. The return Statement
- •2. Lab Overview
- •3. Report Structure
- •4. Control Exercises
- •5. References
- •1. Theory
- •1.1. A Simple Function
- •1.2. Parameters and Arguments
- •1.3. Global and Local Scope
- •1.4. Scope Operator
- •1.5. Symbolic Constants
- •1.6. Inline Functions
- •1.7. Recursion
- •1.8. Default Arguments
- •2. Lab Overview
- •3. Report Structure
- •4. Control Exercises
- •5. References
- •Lab #4. Arrays, pointers, references and dynamic variables
- •1. Theory
- •1.1 Arrays
- •1.2 Multidimensional Arrays
- •1.3 Pointers
- •1.4 Dynamic Memory
- •1.5 Pointer Arithmetic
- •1.6 References
- •2. Lab Overview
- •3. Report Structure
- •4. Control Exercises
- •5. References
- •Lab #5. Structures
- •1. Theory
- •1.1 Introducing Structures
- •1.2 Using a Structure in a Program
- •1.3 Program Notes
- •1.4 Other Structure Properties
- •1.5 Arrays of Structures
- •2. Lab Overview
- •3. Report Structure
- •4. Control Exercises
- •5. References
- •Lab #6. Strings
- •1. Theory
- •1.1. Introduction to Strings
- •1.2. Concatenating String Constants
- •1.3. Using Strings in an Array
- •1.4. Reading String Input a Line at a Time
- •1.5. Line-Oriented Input with gets()
- •1.6. Introducing the string Class
- •1.7. Assignment, Concatenation, and Appending
- •1.8. More string Class Operations
- •1.9. More on string Class I/o
- •2. Lab Overview
- •3. Report Structure
- •4. Control Questions
- •5. References
- •Annex a Report’s Title Page
- •From discipline “Algorithmization and Programming” Topic: _______________________________________________
1.5 Arrays of Structures
It’s also possible to create arrays whose elements are structures. The technique is exactly the same as for creating arrays of the fundamental types. For example, to create an array of 100 inflatable structures, you could do the following:
inflatable gifts[100]; // array of 100 inflatable structures
This makes gifts an array of inflatables. Hence each element of the array, such as gifts[0] or gifts[99], is an inflatable object and can be used with the membership operator:
cin >> gifts[0].volume; // use volume member of first struct
cout << gifts[99].price << endl; // display price member of last struct
Keep in mind that gifts itself is an array, not a structure, so constructions such as gifts.price are not valid.
To initialize an array of structures, you combine the rule for initializing arrays (a brace-enclosed, comma-separated list of values for each element) with the rule for structures (a brace-enclosed, comma-separated list of values for each member). Because each element of the array is a structure, its value is represented by a structure initialization. Thus, you wind up with a brace-enclosed, comma-separated list of values, each of which itself is a brace-enclosed, comma-separated list of values:
inflatable guests[2] = { // initializing an array of structs
{“Bambi”, 0.5, 21.99}, // first structure in array
{“Godzilla”, 2000, 565.99} // next structure in array
};
Listing 3 shows a short example that uses an array of structures. Note that because guests is an array of inflatable, guest[0] is type inflatable, so you can use it with the dot opera-tor to access a member of the inflatable structure.
Listing 3
struct inflatable {
char name[20];
float volume;
double price;
};
void main() {
inflatable guests[2] = { // initializing an array of structs
{“Bambi”, 0.5, 21.99}, // first structure in array
{“Godzilla”, 2000, 565.99} // next structure in array
};
cout << “The guests “ << guests[0].name << “ and “ << guests[1].name << “\nhave a combined volume of “ << guests[0].volume + guests[1].volume << “ cubic feet.\n”;
}
Here is the output of the program in Listing 3:
The guests Bambi and Godzilla
have a combined volume of 2000.5 cubic feet.
2. Lab Overview
2.1. Read the theory and try Control Exercises.
2.2. Develop the algorithm flowchart to create input data and handle structures array according to individual case from the Table below.
2.3. Write the program code according to the developed algorithm. Dynamic variables, pointers, user-defined functions should be used.
2.4. Debug the program, run it and make screenshots.
2.5. Prepare the Lab Report according to the required structure.
# |
Structures array |
Operation on array |
|
|
Books |
Find a book with a minimum number of pages |
|
|
Vegetables |
Find vegetables with red color |
|
|
Computers |
Output a configuration of the most expensive computer |
|
|
Films |
Find a film with a highest budget |
|
|
Shoes |
Output shoes with a price more than average one |
|
|
Cell phones |
Output Nokia cell phones |
|
|
Students |
Calculate the group average rating |
|
|
Laptops |
Output laptops with a screen diagonal more than 15 inches |
|
|
Fruits |
Output fruits with weight less than 200g |
|
|
Shirts |
Find a shirt with a lowest price |
|
|
Bags |
Find a bag with a highest number of pockets |
|
|
Soundtracks |
Output soundtracks of the film “Transformers” |
|
|
Drinks |
Output non-alcoholic drinks |
|
|
Pens |
Output black pens with a price more than $100 |
|
|
Workers |
Find a worker with a highest salary |
|
|
Cars |
Output cars of 2012 year |
|
|
Songs |
Output songs with a rating bellow the indicated one |
|
|
Soft |
Output freeware soft |
|
|
Animals |
Calculate the total weight of all animals in the database |
|
|
Countries |
Output countries with a population more than the population in Ukraine |
|
|
Soccer Teams |
Output teams which won Champion League more than 3 times |
|
|
Tanks |
Output middle weight tanks |
|
|
Motorcycles |
Output motorcycles with engine volume more than 50 sm3 |
|
|
Rock groups |
Output rock groups from Ukraine |
|
|
Cosmetics |
Output goods from Avon |
