Structure

  1. "Define a structure of student having data member’s name, address, marks in C language, and marks in information system. Take data for n students in an array dynamically and find the total marks obtained."
    #include <stdio.h>
    #include <string.h>

    // Define the structure for a student
    struct Student {
        char name[50];
        char address[100];
        int marks;
    };

    int main() {
        // Declare a student variable
        struct Student student1;

        // Assign values to the student1 fields
        strcpy(student1.name, "Suman Adhikari");
        strcpy(student1.address, "Kathmandu, Nepal");
        student1.marks = 85;

        // Print the student details
        printf("Name: %s\n", student1.name);
        printf("Address: %s\n", student1.address);
        printf("Marks: %d\n", student1.marks);

        return 0;
    }




  2. define structure of employee having data members name, address, age, and salary. Take data for n employee in an array dynamically and find the average salary."
    #include <stdio.h>
    #include <stdlib.h>

    // Define the Employee structure
    struct Employee {
        char name[100];
        char address[100];
        int age;
        float salary;
    };

    int main() {
        int n;
        float sumSalary = 0.0, avgSalary;

        // Input the number of employees
        printf("Enter the number of employees: ");
        scanf("%d", &n);

        // Dynamically allocate memory for n employees
        struct Employee *employees = (struct Employee *)malloc(n * sizeof(struct Employee));

        // Check if memory has been allocated successfully
        if (employees == NULL) {
            printf("Memory allocation failed.\n");
            return 1;
        }

        // Input data for each employee
        for (int i = 0; i < n; i++) {
            printf("Enter details for employee %d:\n", i + 1);

            printf("Name: ");
            scanf(" %[^\n]%*c", employees[i].name); // to read string with spaces

            printf("Address: ");
            scanf(" %[^\n]%*c", employees[i].address);

            printf("Age: ");
            scanf("%d", &employees[i].age);

            printf("Salary: ");
            scanf("%f", &employees[i].salary);

            // Add salary to sum
            sumSalary += employees[i].salary;
        }

        // Calculate the average salary
        avgSalary = sumSalary / n;

        // Output the average salary
        printf("The average salary is: %.2f\n", avgSalary);

        // Free the allocated memory
        free(employees);

        return 0;
    }





  3. "Write a program to enter two 3x3 matrices and calculate the product of given matrices."
    #include <stdio.h>

    #define SIZE 3

    // Define a struct for a 3x3 matrix
    struct Matrix {
        int data[SIZE][SIZE];
    };

    // Function prototypes
    void inputMatrix(struct Matrix *matrix, const char *name);
    void printMatrix(struct Matrix *matrix);
    void multiplyMatrices(struct Matrix *matrix1, struct Matrix *matrix2, struct Matrix *result);

    int main() {
        struct Matrix matrix1, matrix2, result;

        // Input matrices
        inputMatrix(&matrix1, "Matrix 1");
        inputMatrix(&matrix2, "Matrix 2");

        // Multiply matrices
        multiplyMatrices(&matrix1, &matrix2, &result);

        // Output the result matrix
        printf("Resultant matrix after multiplication:\n");
        printMatrix(&result);

        return 0;
    }

    // Function to input a matrix
    void inputMatrix(struct Matrix *matrix, const char *name) {
        printf("Enter elements of %s (3x3):\n", name);
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                scanf("%d", &matrix->data[i][j]);
            }
        }
    }

    // Function to print a matrix
    void printMatrix(struct Matrix *matrix) {
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                printf("%d ", matrix->data[i][j]);
            }
            printf("\n");
        }
    }

    // Function to multiply two matrices
    void multiplyMatrices(struct Matrix *matrix1, struct Matrix *matrix2, struct Matrix *result) {
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                result->data[i][j] = 0;
                for (int k = 0; k < SIZE; k++) {
                    result->data[i][j] += matrix1->data[i][k] * matrix2->data[k][j];
                }
            }
        }
    }









0 Comments had been done yet:

Post a Comment