Lab 9: Structure in C – C Programming Lab Report

Lab 9: Structure in C

A C Programming Lab Report for Lab 9: Structure in C

Lab Report Information

Lab No.: 9

Title: Structure in C

Course: Computer Programming (CT 101), I Year I Part

Credit: Important Notes

C Programming Lab Report: Theory for Lab 9

Structure in C

This C Programming Lab Report explores the use of the Structure in C. A structure is a user-defined data type that groups related variables of different data types into a single unit. It is ideal for representing real-world records like students or employees.

Key Aspects of a Structure:

  • Declaration & Definition: A structure template is created using the struct keyword.
  • Accessing Members: Individual members of a structure variable are accessed using the dot (.) operator.
  • Array of Structures: An array where each element is a structure, perfect for storing multiple records.
  • Nested Structures: A structure can be a member of another structure, allowing for hierarchical data representation.

C Programming Lab Report: Programs for Lab 9

1. Multiply Complex Numbers Using a Structure

Question:

Write a program in C to multiply two complex numbers using the concept of a structure.

Program Code:

#include <stdio.h>
#include <conio.h>

struct complex {
    float r, i;
};

int main() {
    struct complex num1, num2, result;
    printf("\nEnter Real part and Imaginary part of first number: ");
    scanf("%f %f", &num1.r, &num1.i);
    printf("\nEnter Real part and Imaginary part of second number: ");
    scanf("%f %f", &num2.r, &num2.i);
    result.r = num1.r * num2.r - num1.i * num2.i;
    result.i = num1.r * num2.i + num1.i * num2.r;
    printf("\nThe product is %.2f + %.2fi", result.r, result.i);
    getch();
    return 0;
}

Output:

Enter Real part and Imaginary part of first number: 2.5 3.5
Enter Real part and Imaginary part of second number: 1.5 4.5
The product is -12.00 + 16.50i

Algorithm:

  1. Start
  2. Define a `struct complex` with float members `r` and `i`.
  3. Declare three `struct complex` variables: `num1`, `num2`, `result`.
  4. Read the real and imaginary parts for `num1` and `num2`.
  5. Calculate the product: `result.r = num1.r*num2.r – num1.i*num2.i;` and `result.i = num1.r*num2.i + num1.i*num2.r;`.
  6. Print the `result`.
  7. Stop.

2. Add Two Distances Using a Structure

Question:

Write a program in C to add two distances (in feet and inches) using a structure.

Program Code:

#include <stdio.h>
#include <conio.h>

struct Distance {
    int feet;
    int inch;
};

int main() {
    struct Distance d1, d2, sum;
    printf("Enter 1st distance (feet then inch): ");
    scanf("%d %d", &d1.feet, &d1.inch);
    printf("Enter 2nd distance (feet then inch): ");
    scanf("%d %d", &d2.feet, &d2.inch);
    sum.feet = d1.feet + d2.feet;
    sum.inch = d1.inch + d2.inch;
    if (sum.inch >= 12) {
        sum.feet += sum.inch / 12;
        sum.inch = sum.inch % 12;
    }
    printf("\nSum of distances = %d feet %d inches", sum.feet, sum.inch);
    getch();
    return 0;
}

Output:

Enter 1st distance (feet then inch): 10 7
Enter 2nd distance (feet then inch): 5 8
Sum of distances = 16 feet 3 inches

Algorithm:

  1. Start
  2. Define a `struct Distance` with `feet` and `inch`.
  3. Read two distances `d1` and `d2`.
  4. Calculate `sum.feet = d1.feet + d2.feet;` and `sum.inch = d1.inch + d2.inch;`.
  5. If `sum.inch >= 12`, adjust `sum.feet` and `sum.inch`.
  6. Print the final `sum`.
  7. Stop.

3. Sort Employee Records by Salary

Question:

Write a program to read ‘n’ employee records and sort them in ascending order of salary.

Program Code:

#include <stdio.h>
#include <string.h>

struct employee {
    char name[20];
    int salary;
};

int main() {
    struct employee e[50], temp;
    int i, j, n;
    printf("\nEnter number of employees: ");
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        printf("\nEnter employee %d's name and salary: ", i + 1);
        scanf("%s %d", e[i].name, &e[i].salary);
    }
    for (i = 0; i < n - 1; i++) {
        for (j = 0; j < n - i - 1; j++) {
            if (e[j].salary > e[j + 1].salary) {
                temp = e[j];
                e[j] = e[j + 1];
                e[j + 1] = temp;
            }
        }
    }
    printf("\nThe sorted list is:\n");
    for (i = 0; i < n; i++) {
        printf("NAME: %s, SALARY: %d\n", e[i].name, e[i].salary);
    }
    return 0;
}

Output:

Enter number of employees: 3

Enter employee 1's name and salary: Hari 17000
Enter employee 2's name and salary: Pabin 10000
Enter employee 3's name and salary: Kriti 5000
The sorted list is:
NAME: Kriti, SALARY: 5000
NAME: Pabin, SALARY: 10000
NAME: Hari, SALARY: 17000

Algorithm:

  1. Start
  2. Define an `employee` structure.
  3. Create an array of `employee` structures.
  4. Read the number of employees, `n`.
  5. Read the details for `n` employees into the array.
  6. Sort the array of structures based on the `salary` member using Bubble Sort.
  7. Print the sorted records.
  8. Stop.

4. Nested Structure for Employee Data

Question:

Write a program of your own choice using a nested structure.

Program Code:

#include <stdio.h>
#include <conio.h>

struct date {
    int y, m, d;
};

struct employee {
    int id;
    char name[20];
    struct date dob; // Nested structure
};

int main() {
    struct employee e[50];
    int i, n;
    printf("\nEnter no. of employees: ");
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        printf("\nEnter name, ID, and DOB (y/m/d) for employee %d: ", i+1);
        scanf("%s %d %d/%d/%d", e[i].name, &e[i].id, &e[i].dob.y, &e[i].dob.m, &e[i].dob.d);
    }
    printf("\n--- Employee Details ---\n");
    for (i = 0; i < n; i++) {
        printf("NAME: %s\t ID: %d\t DOB: %d/%d/%d\n",
               e[i].name, e[i].id, e[i].dob.y, e[i].dob.m, e[i].dob.d);
    }
    getch();
    return 0;
}

Output:

Enter no. of employees: 2

Enter name, ID, and DOB (y/m/d) for employee 1: Diwakar 101 2001/05/15
Enter name, ID, and DOB (y/m/d) for employee 2: Bhandari 102 2002/08/20
--- Employee Details ---
NAME: Diwakar ID: 101 DOB: 2001/5/15
NAME: Bhandari ID: 102 DOB: 2002/8/20

Algorithm:

  1. Start
  2. Define a `date` structure and an `employee` structure that contains the `date` structure.
  3. Create an array of `employee` structures.
  4. Read `n`, the number of employees.
  5. Loop `n` times, reading the name, ID, and date of birth (accessing nested members like `e[i].dob.y`).
  6. Loop again to print all the stored employee records.
  7. Stop.

Discussion and Conclusion

This C Programming Lab Report on Structure in C provided practical experience with using structures to manage heterogeneous data. The exercises demonstrated how to define structures, declare variables, and access members to represent real-world objects. The lab progressed from simple structures to more advanced concepts like arrays of structures and nested structures, highlighting their utility for handling lists of records and hierarchical data. In conclusion, this lab successfully illustrated the importance of structures in organizing related data into a single, manageable unit, a fundamental concept for building complex applications in C.

Scroll to Top