Class 10 Computer Science Chapter 9 Structure In C

Class 10 Computer Science Chapter 9 Structure In C Question Answer, SEBA Class 10 Computer Science Solutions, to each chapter is provided in the list so that you can easily browse throughout different chapters SCERT Class 10 Computer Science Chapter 9 Structure In C and select needs one.

Class 10 Computer Science Chapter 9 Structure In C

Join Telegram channel

Also, you can read the SCERT book online in these sections Solutions by Expert Teachers as per SCERT (CBSE) Book guidelines. NCERT Solution of Class 10 Computer Science Chapter 9 Structure In C is part of AHSEC All Subject Solutions. Here we have given Class 10 Computer Science Chapter 9 Structure In C Notes for All Subjects, You can practice these here in Class 10 Computer Science.

STRUCTURE IN C

Chapter – 9

COMPUTER SCIENCE

1. Define structure in context of a programming language. How is it different from an array?

Ans. Structures are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure. Unlike an array, a structure can contain many different data types (int, float, char, etc.).

The major difference between an array and a structure is 

1. An array is a collection of variables of the same data type and A structure is a collection of variables of different data types.

2. Array elements are stored in a contiguous memory location and Structure elements may not be stored in a contiguous memory location.

3. The array’s name points to the first element of the array, so we can say Array is like a pointer but the structure is not a pointer because the structure name does not point to the first element of the structure.

2. Is structure a built – in – data type? Can we apply basic arithmetic operations such as addition, subtraction to structure variable? Show with an example.

Ans. Structure is not a built – in – data type.

Yes, we can apply basic arithmetic operations such as addition, subtraction to structure variable. 

This can be explained with the help of following example.

Example:

Code:

#include <stdio.h>

// Define a structure

struct Point {

    int x;

    int y;

};

int main() {

    // Declare and initialize two structure variables

    struct Point p1 = {3, 4};

    struct Point p2 = {1, 2};

    // Add the x and y components of the two points

    int sum_x = p1.x + p2.x;

    int sum_y = p1.y + p2.y;

    // Subtract the x and y components of the two points

    int diff_x = p1.x - p2.x;

    int diff_y = p1.y - p2.y;

    // Print the results

    printf("Sum: (%d, %d)\n", sum_x, sum_y);

    printf("Difference: (%d, %d)\n", diff_x, diff_y);

    return 0;

}

Output:

Sum: (4, 6)

Difference: (2, 2)

3. Identify the members of the structure from the below code segment.

struct Account { char acNo[15] ; 

char ifsc[15]; 

char acType[7]; 

double balance; 

double minBalance; }; 

struct Account account1, account2, account3, account[10];

Ans. Members of the structure: acNo, ifsc, ac Type, balance, minBalance.

4. Identify the structure variables from the below code segment.

struct Account { char acNo[15] ; 

char ifsc[15]; 

char acType[7]; 

double balance; 

double minBalance; }; 

struct Account account1, account2, account3, account[10];

Ans. Structure variables : account1, account2, account3, account[10].

5. Consider the structure below and write statements for the following:

(a) To declare variable of the structure.

(b) To display age of the teacher.

struct Teacher 

{

char name [30];

int age;

};

Solution:

(a) struct Teacher obj;

(b) printf(“\n The age of the teacher is %d”, obj.age);

6. Declare a pointer for structure Teacher (from Q No.5) and dynamically allocate memory for 10 records.

Solution:

#include <stdio.h>

#include <stdlib.h>

struct Teacher

{

    char name[30];

    int age;

};

int main()

{

    struct Teacher obj;

    struct Teacher *ptr;

    // Allocate memory for an array of 10 Teacher structures

    ptr = (struct Teacher*)malloc(10 * sizeof(struct Teacher));

    if (ptr == NULL) {

        printf("Memory allocation failed.\n");

        return 1; // Exit the program with an error code

    }

    // Access and modify the first element of the array

    strcpy(ptr[0].name, "John");

    ptr[0].age = 35;

    // Free the allocated memory when done

    free(ptr);

    return 0;

}

7. Consider the structure below and write the statements for the following:

(a) To declare a pointer for the above structure and display the salary.

(b) To declare a single pointer for two different variables of the higher structure and display the details of the employee whose salary is more.

struct Employee

{

char name [30];

double salary;

};

Solution:

Code:

#include <stdio.h>

struct Employee

{

    char name[30];

    double salary;

};

int main()

{

    // Declare Employee structure variables and initialize them

    struct Employee emp1, emp2;

    emp1.salary = 50000.0; // Set the salary for emp1

    emp2.salary = 60000.0; // Set the salary for emp2

    // Declare a pointer to an Employee structure and point it to emp1

    struct Employee *empPtr = &emp1;

    // Access and display the salary using the pointer

    printf("Salary: %.2f\n", empPtr->salary);

    // Declare a pointer for the higher salary Employee

    struct Employee *higherSalaryEmp;

    // Compare salaries and point the pointer to the employee with the higher salary

    if (emp1.salary > emp2.salary)

    {

        higherSalaryEmp = &emp1;

    }

    else

    {

        higherSalaryEmp = &emp2;

    }

    // Display details of the employee with the higher salary

    printf("Name: %s\n", higherSalaryEmp->name);

    printf("Salary: %.2f\n", higherSalaryEmp->salary);

    return 0;

}

Output:

Salary: 50000.00

Name: 

Salary: 60000.00

8. Rewrite the program of Q.no. 7 to facilitate dynamic memory allocation for N number of record where N is a user input.

Solution:

#include <stdio.h>

#include <stdlib.h>

struct Employee

{

    char name[30];

    double salary;

};

int main()

{

    struct Employee *ptr;

    int i, n;

    printf("Enter the number of employees: ");

    scanf("%d", &n);

    ptr = (struct Employee*)malloc(n * sizeof(struct Employee));

    for (i = 0; i < n; i++)

    {

        printf("Enter the name of employee %d: ", i + 1);

        getchar(); // Consume the newline character from the previous input

        fgets((ptr + i)->name, sizeof((ptr + i)->name), stdin);

        printf("Enter the salary of employee %d: ", i + 1);

        scanf("%lf", &(ptr + i)->salary);

    }

    // Display the employee data

    printf("\nEmployee Details:\n");

    for (i = 0; i < n; i++)

    {

        printf("Employee %d\n", i + 1);

        printf("Name: %s", (ptr + i)->name);

        printf("Salary: %.2lf\n", (ptr + i)->salary);

    }

    // Free the allocated memory

    free(ptr);

    return 0;

}

Output:

Enter the number of employees: 3

Enter the name of employee 1: John Doe

Enter the salary of employee 1: 55000.50

Enter the name of employee 2: Jane Smith

Enter the salary of employee 2: 60000.75

Enter the name of employee 3: Robert Johnson

Enter the salary of employee 3: 52000.25

Employee Details:

Employee 1

Name: John Doe

Salary: 55000.50

Employee 2

Name: Jane Smith

Salary: 60000.75

Employee 3

Name: Robert Johnson

Salary: 52000.25

9. Consider the below structure and design a simple banking system that supports the following operations.

(a) Opening of accounts.

(b) Displaying details based on account number.

(c) Displaying all account details.

(d) Displaying details of all accounts whose balance is more than 1000.

(e) Depositing an amount from an account.

(f) Withdrawing some amount from an account.

struct Account

{

char acNo[15];

char ifsc[15];

char acType [7];

double balance;

double minBalance;

};

Solution:

Code:

#include <stdio.h>

#include <string.h>

struct Account

{

    char acNo[15];

    char ifsc[15];

    char acType[7];

    double balance;

    double minBalance;

};

void displayAccount(struct Account account)

{

    printf("Account Number: %s\n", account.acNo);

    printf("IFSC: %s\n", account.ifsc);

    printf("Account Type: %s\n", account.acType);

    printf("Balance: %.2lf\n", account.balance);

    printf("Minimum Balance: %.2lf\n", account.minBalance);

}

int main()

{

    int numAccounts = 0;

    struct Account accounts[100]; // Assuming a maximum of 100 accounts

    int choice;

    while (1)

    {

        printf("\nBanking System Menu:\n");

        printf("1. Open an Account\n");

        printf("2. Display Account by Account Number\n");

        printf("3. Display All Accounts\n");

        printf("4. Display Accounts with Balance > 1000\n");

        printf("5. Deposit Amount\n");

        printf("6. Withdraw Amount\n");

        printf("7. Exit\n");

        printf("Enter your choice: ");

        scanf("%d", &choice);

        switch (choice)

        {

            case 1: // Opening an Account

                printf("Enter Account Number: ");

                scanf("%s", accounts[numAccounts].acNo);

                printf("Enter IFSC: ");

                scanf("%s", accounts[numAccounts].ifsc);

                printf("Enter Account Type: ");

                scanf("%s", accounts[numAccounts].acType);

                printf("Enter Balance: ");

                scanf("%lf", &accounts[numAccounts].balance);

                printf("Enter Minimum Balance: ");

                scanf("%lf", &accounts[numAccounts].minBalance);

                numAccounts++;

                break;

            case 2: // Display Account by Account Number

                printf("Enter Account Number: ");

                char accountNumber[15];

                scanf("%s", accountNumber);

                int found = 0;

                for (int i = 0; i < numAccounts; i++)

                {

                    if (strcmp(accounts[i].acNo, accountNumber) == 0)

                    {

                        displayAccount(accounts[i]);

                        found = 1;

                        break;

                    }

                }

                if (!found)

                    printf("Account not found.\n");

                break;

            case 3: // Display All Accounts

                for (int i = 0; i < numAccounts; i++)

                {

                    displayAccount(accounts[i]);

                }

                break;

            case 4: // Display Accounts with Balance > 1000

                for (int i = 0; i < numAccounts; i++)

                {

                    if (accounts[i].balance > 1000)

                    {

                        displayAccount(accounts[i]);

                    }

                }

                break;

            case 5: // Deposit Amount

                printf("Enter Account Number: ");

                scanf("%s", accountNumber);

                printf("Enter Deposit Amount: ");

                double depositAmount;

                scanf("%lf", &depositAmount);

                for (int i = 0; i < numAccounts; i++)

                {

                    if (strcmp(accounts[i].acNo, accountNumber) == 0)

                    {

                        accounts[i].balance += depositAmount;

                        printf("Deposit successful. New balance: %.2lf\n", accounts[i].balance);

                        break;

                    }

                }

                break;

            case 6: // Withdraw Amount

                printf("Enter Account Number: ");

                scanf("%s", accountNumber);

                printf("Enter Withdrawal Amount: ");

                double withdrawalAmount;

                scanf("%lf", &withdrawalAmount);

                for (int i = 0; i < numAccounts; i++)

                {

                    if (strcmp(accounts[i].acNo, accountNumber) == 0)

                    {

                        if (accounts[i].balance - withdrawalAmount >= accounts[i].minBalance)

                        {

                            accounts[i].balance -= withdrawalAmount;

                            printf("Withdrawal successful. New balance: %.2lf\n", accounts[i].balance);

                        }

                        else

                        {

                            printf("Withdrawal failed. Insufficient balance.\n");

                        }

                        break;

                    }

                }

                break;

            case 7: // Exit

                return 0

Output:

Banking System Menu:

Open an Account

Display Account by Account Number

Display All Accounts

Display Accounts with Balance > 1000

Deposit Amount

Withdraw Amount

Exit
Enter your choice: 1

Enter Account Number: 12345
Enter IFSC: ABC123
Enter Account Type: Savings
Enter Balance: 2500.00
Enter Minimum Balance: 1000.00

Banking System Menu:

Open an Account

Display Account by Account Number

Display All Accounts

Display Accounts with Balance > 1000

Deposit Amount

Withdraw Amount

Exit
Enter your choice: 1

Enter Account Number: 67890
Enter IFSC: XYZ456
Enter Account Type: Checking
Enter Balance: 500.00
Enter Minimum Balance: 200.00

Banking System Menu:

Open an Account

Display Account by Account Number

Display All Accounts

Display Accounts with Balance > 1000

Deposit Amount

Withdraw Amount

Exit
Enter your choice: 3

Account Number: 12345
IFSC: ABC123
Account Type: Savings
Balance: 2500.00
Minimum Balance: 1000.00
Account Number: 67890
IFSC: XYZ456
Account Type: Checking
Balance: 500.00
Minimum Balance: 200.00

Banking System Menu:

Open an Account

Display Account by Account Number

Display All Accounts

Display Accounts with Balance > 1000

Deposit Amount

Withdraw Amount

Exit
Enter your choice: 4

Account Number: 12345
IFSC: ABC123
Account Type: Savings
Balance: 2500.00
Minimum Balance: 1000.00

Banking System Menu:

Open an Account

Display Account by Account Number

Display All Accounts

Display Accounts with Balance > 1000

Deposit Amount

Withdraw Amount

Exit
Enter your choice: 5

Enter Account Number: 12345
Enter Deposit Amount: 500.00
Deposit successful. New balance: 3000.00

Banking System Menu:

Open an Account

Display Account by Account Number

Display All Accounts

Display Accounts with Balance > 1000

Deposit Amount

Withdraw Amount

Exit
Enter your choice: 2

Enter Account Number: 12345
Account Number: 12345
IFSC: ABC123
Account Type: Savings
Balance: 3000.00
Minimum Balance: 1000.00

Banking System Menu:

Open an Account

Display Account by Account Number

Display All Accounts

Display Accounts with Balance > 1000

Deposit Amount

Withdraw Amount

Exit
Enter your choice: 6

Enter Account Number: 12345
Enter Withdrawal Amount: 2000.00
Withdrawal successful. New balance: 1000.00

Banking System Menu:

Open an Account

Display Account by Account Number

Display All Accounts

Display Accounts with Balance > 1000

Deposit Amount

Withdraw Amount

Exit
Enter your choice: 2

Enter Account Number: 12345
Account Number: 12345
IFSC: ABC123
Account Type: Savings
Balance: 1000.00
Minimum Balance: 1000.00

Banking System Menu:

Open an Account

Display Account by Account Number

Display All Accounts

Display Accounts with Balance > 1000

Deposit Amount

Withdraw Amount

Exit
Enter your choice: 7

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top