Comprehensive Study Guide on C++ Structures and Data Organization

Comparison of Data Structures: Arrays vs. Structs

  • Array: Used for variables and data collection that consist of the same data type. For example, an array of integers would be declared as: int marks[10];int\,marks[10];.
  • Struct (Structure): Used for variables that contain different data types within a single unit. For example, a single record might require an integer for credits and a float for GPA: int credit;int\,credit; and float cgpa;float\,cgpa;.
  • Memory Allocation (Declaration): Declaring standard variables like intint, floatfloat, charchar, or an arrayarray allocates a specific location in the computer's memory.
    • Example: int mark;int\,mark; allocates one space for a single integer.
    • Example: int mark[2];int\,mark[2]; allocates two contiguous spaces for integers, referred to as mark[0]mark[0] and mark[1]mark[1].

Understanding Struct Variables

  • Organizing a struct involves two primary steps: Definition and Declaration.
  • Step 1: Definition: This defines the blueprint or template for the structure. It specifies the struct name and the members within it.
    • Syntax for struct definition: cpp struct CourseInfo { string name; string code; int credit; };         
    • In this example, CourseInfo is the struct name, and name, code, and credit are the struct members.
  • Step 2: Declaration: This step creates an actual occurrence of the struct in memory.
    • Example: CourseInfo Course1; and CourseInfo Course2; declare two separate variables of the CourseInfo type.
  • Alternative Inline Declaration (Figure 1):     Structures can be defined and declared simultaneously: cpp struct { string name; string code; int credit; } Course1, Course2;     

Struct Initialization and Member Access

  • Initialization: Assigning values at the time of declaration.
    • Example: CourseInfo Course1 = {"Structured Programming", "MTS3013", 3}; assigns "Structured Programming" to the name, "MTS3013" to the code, and 3 to the credit member.
  • Accessing Struct Members:
    • While an array uses an index or subscript (e.g., marks[0]marks[0]), a struct uses the dot operator (..), also known as the member access operator.
    • Example: Course1.name; or Course2.code; targets a specific data field within the struct variable.

Operations on Structures

  • A. Input Data via Assignment (Assign Operator):
    • Members can be assigned values individually:
      • Course1.name = "Data Struct";
      • Course1.code = "MTS3023";
      • Course1.credit = 3;
  • B. Input Data via cin:
    • To get user input for specific members: cin >> Course1.name; cin >> Course1.code; cin >> Course1.credit;.
  • C. Output Data via cout:
    • To display member data to the console: cout << Course1.name; cout << Course1.code; cout << Course1.credit;.

Program Example: Implementation of Structs

  • The following program demonstrates the definition, declaration, and assignment of a structure: ```cpp

    include

    include

    struct CourseInfo { string name; string code; int credit; };

    int main() { CourseInfo Course1; Course1.name = "Data Struct"; Course1.code = "MTS3023"; Course1.credit = 3; return 0; }     ```

  • Result after running a similar program (using Course1 details: Structured Programming, MTS3013, 3):     Structured Programming MTS3013 3

Dynamic Variables (Pembolehubah Dinamik)

  • These are variables created during the execution of a program (runtime).
  • new: Used to build a new temporary variable by allocating a memory location pointed to by a specific pointer.
  • delete: Used to destroy the temporary variable and free up the allocated memory location.

Practice Exercises and Examples

Question 1: Car Information

  • Struct Definition: cpp struct car { char no_plat[9]; char name[40]; int year; double price; }; &nbsp;&nbsp;&nbsp;&nbsp;
  • **(i) Declare a single struct variable named ** car1 ** to store information for one car**:
    • car car1;
  • **(ii) Declare a struct variable named ** list ** to store information for 20 cars**:
    • car list[20];
  • **(iii) Write input statements for ** car1 ** and ** list ** (Input data into the struct)**:
    • For car1: cin >> car1.no_plat; cin >> car1.name; cin >> car1.year; cin >> car1.price;
    • For list: for (int x = 0; x < 20; x++) { cin >> list[x].no_plat; cin >> list[x].name; cin >> list[x].year; cin >> list[x].price; }

Question 2: Camera Database

  • Struct Definition: cpp struct Camera { char cameraMake[20]; char cameraModel[20]; char zoomLevel[5]; float megaPixels; float price; }; &nbsp;&nbsp;&nbsp;&nbsp;
  • Task A: Declare an array for 25 types of camera:
    • Camera Camera1[25];
  • Task B: Write code to input data into the 25 camera arrays: cpp for (int y = 0; y < 25; y++) { cin >> Camera1[y].cameraMake; cin >> Camera1[y].cameraModel; cin >> Camera1[y].zoomLevel; cin >> Camera1[y].megaPixels; cin >> Camera1[y].price; } &nbsp;&nbsp;&nbsp;&nbsp;
  • Task C: Display camera models with a price greater than RM 500: cpp for (int y = 0; y < 25; y++) { if (Camera1[y].price > 500) { cout << Camera1[y].cameraModel; } } &nbsp;&nbsp;&nbsp;&nbsp;

Question 3: Info_Kad Management

  • Struct Definition: cpp struct Info_Kad { string name; int no_kad; int year; }; &nbsp;&nbsp;&nbsp;&nbsp;
  • **Task A: Declare ** kad_debit ** for 10 customers**:
    • Info_Kad kad_debit[10];
  • **Task B: main() function for input/output of ** kad_debit ** data**: cpp void main() { // Input loop for (int i = 0; i < 10; i++) { cin >> kad_debit[i].name; cin >> kad_debit[i].no_kad; cin >> kad_debit[i].year; } // Output loop for (int i = 0; i < 10; i++) { cout << kad_debit[i].name; cout << kad_debit[i].no_kad; cout << kad_debit[i].year; } } &nbsp;&nbsp;&nbsp;&nbsp;

Question 4: Personal Records

  • Struct Definition: cpp struct Personal_Rekod { string last_name; string first_name; int age; }; &nbsp;&nbsp;&nbsp;&nbsp;

  • **Task A: Declare ** staf ** for 10 staff members**:

    • Personal_Rekod staf[10];
  • Task B: Full program with input and formatted output: ```cpp

    include

    include

    struct Personal_Rekod { string namaTerakhir; string namaPertama; int umur; };

    void main() { Personal_Rekod ahli_akademik[10]; for (int i = 0; i < 10; i++) { cout << "Masukkan nama Pertama: "; cin >> ahli_akademik[i].namaPertama; cout << "Masukkan nama Terakhir: "; cin >> ahli_akademik[i].namaTerakhir; cout << "Masukkan umur: "; cin >> ahli_akademik[i].umur; } for (int i = 0; i < 10; i++) { cout << ahli_akademik[i].namaPertama << " " << ahli_akademik[i].namaTerakhir << " " << ahli_akademik[i].umur; } }     ```