Get answers, questions, notes, textbook solutions, extras, pdf, mcqs, for Computer Chapter 9 Structure in C of class 10 (HSLC/Madhyamik) for students studying under the Board of Secondary Education, Assam (SEBA). These notes/answers, however, should only be used for references and modifications/changes can be made wherever possible.
Summary
In this chapter, we delve into the world of structures in C—a versatile feature that allows you to group various data types under a single, user-defined umbrella. Imagine a structure as a mini-database where you can store different kinds of information, be it integers, characters, or floating-point numbers, all in one neat package.
To define a structure, we use the keyword struct followed by a meaningful name, and then encapsulate its members within curly braces {}. Once you’ve created a structure, you can access its individual components using the dot (.) operator, like so: structureVariable.member.
One of the benefits of structures is their flexibility. You can declare them either globally or locally, depending on your needs. But that’s not all; you can also create arrays of structures, a powerful feature that allows you to manage collections of complex data with ease.
Structures aren’t just standalone entities; they can interact fluidly with other features of C programming. For instance, you can pass structures as arguments to functions, or even have functions return them. Pointers can be used to manipulate structure data, offering a new dimension of versatility. And when it comes to memory management, you can dynamically allocate memory for structures using functions like malloc() or calloc(), depending on how many records you wish to handle.
Why are structures so useful, you ask? Consider real-world applications: Whether you’re building a system to manage student records, employee details, library book inventory, or bank accounts, structures serve as the backbone for these kinds of organized data repositories.
Video tutorial
Exercise/textual questions and answers
1. Define structure in the context of a programming language. How is it different from an array?
Answer: Structure is a user defined data type that allows us to combine data of different types together. Structure helps to construct a complex data type that is more practical and meaningful. Structure is somewhat similar to an array, but an array holds data of similar type only. But structure on the other hand, can combine data of different types.
2. Is structure a built-in data type? Can we apply basic arithmetic operations such as addition, subtraction to structure variables? Show with a simple C program.
Answer: Structure is not a built-in data type in C. We cannot apply basic arithmetic operations such as addition, subtraction to structure variables. The following simple C program demonstrates this:
#include <stdio.h>
struct Student {
int roll;
int age;
};
int main() {
struct Student s1, s2;
s1.roll = 1;
s1.age = 17;
s2.roll = 2;
s2.age = 18;
// This will give compile time error
struct Student s3 = s1 + s2;
return 0;
}
The above program will give a compile time error for the statement struct Student s3 = s1 + s2; because we cannot apply arithmetic operations like addition and subtraction to structure variables.
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];
Answer: The members of the structure Account are:
- acNo (character array of size 15)
- ifsc (character array of size 15)
- acType (character array of size 7)
- balance (double)
- minBalance (double)
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];
Answer: The structure variables are:
- account1
- account2
- account3
- account (array of 10 structure variables)
5. Consider the structure below and write statements for the following:
a) to declare a variable of the structure
b) to display the age of the teacher
struct Teacher
{
char name[30];
int age;
};
Answer: a) To declare a variable of Teacher structure:
struct Teacher t1;
b) To display the age of the teacher:
printf(“Age of teacher: %d”, t1.age);
6. Declare a pointer for structure Teacher (from Q5) and dynamically allocate memory for 10 records.
Answer: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Teacher
{
char name[30];
int age;
};
int main()
{
// Declare a pointer to structure Teacher
struct Teacher *teacherArray;
// Dynamically allocate memory for 10 records
teacherArray = (struct Teacher *) malloc(10 * sizeof(struct Teacher));
// Check for memory allocation success
if (teacherArray == NULL)
{
printf(“Memory allocation failed!\n”);
return 1;
}
// Initialize the first record for demonstration
strcpy(teacherArray[0].name, “John Doe”);
teacherArray[0].age = 40;
// Display the age of the first teacher
printf(“The age of the first teacher is %d\n”, teacherArray[0].age);
// Free the allocated memory
free(teacherArray);
return 0;
}
7. Consider the structure below and write 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;
};
Answer: a) #include <stdio.h>
#include <string.h>
struct Employee
{
char name[30];
double salary;
};
int main()
{
// a) Declare a pointer for the structure
struct Employee *empPtr;
// Allocate memory for the structure
empPtr = (struct Employee *) malloc(sizeof(struct Employee));
// Check for memory allocation success
if (empPtr == NULL)
{
printf(“Memory allocation failed!\n”);
return 1;
}
// Initialize the employee details
strcpy(empPtr->name, “Alice”);
empPtr->salary = 50000.00;
// Display the salary
printf(“The salary of the employee is %.2f\n”, empPtr->salary);
// Free the allocated memory
free(empPtr);
return 0;
}
b) #include <stdio.h>
#include <string.h>
struct Employee
{
char name[30];
double salary;
};
int main()
{
// Declare two variables of the structure
struct Employee emp1, emp2;
// Initialize the first employee details
strcpy(emp1.name, “Alice”);
emp1.salary = 50000.00;
// Initialize the second employee details
strcpy(emp2.name, “Bob”);
emp2.salary = 60000.00;
// b) Declare a single pointer for two different variables
struct Employee *highestSalaryEmpPtr;
// Determine whose salary is higher and point to that
if(emp1.salary > emp2.salary)
{
highestSalaryEmpPtr = &emp1;
}
else
{
highestSalaryEmpPtr = &emp2;
}
// Display details of the employee with the higher salary
printf(“The employee with the higher salary is %s with a salary of %.2f\n”,
highestSalaryEmpPtr->name, highestSalaryEmpPtr->salary);
return 0;
}
8. Rewrite the program of Q7 to facilitate dynamic memory allocation for N number of records where N is a user input.
Answer: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Employee
{
char name[30];
double salary;
};
int main()
{
int N, i;
struct Employee *employees;
struct Employee *highestSalaryEmpPtr = NULL;
// Ask the user for the number of records
printf(“Enter the number of employee records you want to enter: “);
scanf(“%d”, &N);
// Allocate memory for N records
employees = (struct Employee *) malloc(N * sizeof(struct Employee));
// Check for memory allocation success
if (employees == NULL)
{
printf(“Memory allocation failed!\n”);
return 1;
}
// Input N records
for (i = 0; i < N; ++i)
{
printf(“Enter details for employee %d\n”, i + 1);
printf(“Name: “);
scanf(“%s”, employees[i].name);
printf(“Salary: “);
scanf(“%lf”, &employees[i].salary);
}
// Initialize the pointer to the first record
highestSalaryEmpPtr = &employees[0];
// Determine the employee with the highest salary
for (i = 1; i < N; ++i)
{
if (employees[i].salary > highestSalaryEmpPtr->salary)
{
highestSalaryEmpPtr = &employees[i];
}
}
// Display the details of the employee with the highest salary
printf(“The employee with the highest salary is %s with a salary of %.2f\n”,
highestSalaryEmpPtr->name, highestSalaryEmpPtr->salary);
// Free the allocated memory
free(employees);
return 0;
}
9. Consider the below structure and design a simple banking system that supports the following operations:
a) opening of accounts (Hint: input to the variables)
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 to an account
f) withdrawing some amount from an account
struct Account
{
char acNo[15];
char ifsc[15];
char acType[7];
double balance;
double minBalance;
};
Answer: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Account
{
char acNo[15];
char ifsc[15];
char acType[7];
double balance;
double minBalance;
};
void openAccount(struct Account *acc, int *count);
void displayDetails(struct Account *acc, int count);
void displayAccount(struct Account *acc, int count, char *acNo);
void displayAboveThousand(struct Account *acc, int count);
void depositAmount(struct Account *acc, int count, char *acNo, double amount);
void withdrawAmount(struct Account *acc, int count, char *acNo, double amount);
int main()
{
struct Account accounts[100];
int count = 0;
int choice;
char acNo[15];
double amount;
while (1)
{
printf(“\nBanking System Menu:\n”);
printf(“1. Open Account\n”);
printf(“2. Display Details Based on Account Number\n”);
printf(“3. Display All Account Details\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:
openAccount(accounts, &count);
break;
case 2:
printf(“Enter account number: “);
scanf(“%s”, acNo);
displayAccount(accounts, count, acNo);
break;
case 3:
displayDetails(accounts, count);
break;
case 4:
displayAboveThousand(accounts, count);
break;
case 5:
printf(“Enter account number: “);
scanf(“%s”, acNo);
printf(“Enter amount to deposit: “);
scanf(“%lf”, &amount);
depositAmount(accounts, count, acNo, amount);
break;
case 6:
printf(“Enter account number: “);
scanf(“%s”, acNo);
printf(“Enter amount to withdraw: “);
scanf(“%lf”, &amount);
withdrawAmount(accounts, count, acNo, amount);
break;
case 7:
exit(0);
default:
printf(“Invalid choice!\n”);
}
}
return 0;
}
void openAccount(struct Account *acc, int *count)
{
printf(“Enter account number: “);
scanf(“%s”, acc[*count].acNo);
printf(“Enter IFSC code: “);
scanf(“%s”, acc[*count].ifsc);
printf(“Enter account type (Saving/Current): “);
scanf(“%s”, acc[*count].acType);
printf(“Enter initial balance: “);
scanf(“%lf”, &acc[*count].balance);
printf(“Enter minimum balance: “);
scanf(“%lf”, &acc[*count].minBalance);
(*count)++;
}
void displayDetails(struct Account *acc, int count)
{
for (int i = 0; i < count; i++)
{
printf(“\nAccount Number: %s\n”, acc[i].acNo);
printf(“IFSC Code: %s\n”, acc[i].ifsc);
printf(“Account Type: %s\n”, acc[i].acType);
printf(“Balance: %.2f\n”, acc[i].balance);
printf(“Minimum Balance: %.2f\n”, acc[i].minBalance);
}
}
void displayAccount(struct Account *acc, int count, char *acNo)
{
for (int i = 0; i < count; i++)
{
if (strcmp(acc[i].acNo, acNo) == 0)
{
printf(“\nAccount Number: %s\n”, acc[i].acNo);
printf(“IFSC Code: %s\n”, acc[i].ifsc);
printf(“Account Type: %s\n”, acc[i].acType);
printf(“Balance: %.2f\n”, acc[i].balance);
printf(“Minimum Balance: %.2f\n”, acc[i].minBalance);
return;
}
}
printf(“Account not found!\n”);
}
void displayAboveThousand(struct Account *acc, int count)
{
for (int i = 0; i < count; i++)
{
if (acc[i].balance > 1000)
{
printf(“\nAccount Number: %s\n”, acc[i].acNo);
printf(“IFSC Code: %s\n”, acc[i].ifsc);
printf(“Account Type: %s\n”, acc[i].acType);
printf(“Balance: %.2f\n”, acc[i].balance);
printf(“Minimum Balance: %.2f\n”, acc[i].minBalance);
}
}
}
void depositAmount(struct Account *acc, int count, char *acNo, double amount)
{
for (int i = 0; i < count; i++)
{
if (strcmp(acc[i].acNo, acNo) == 0)
{
acc[i].balance += amount;
printf(“Amount deposited. New balance: %.2f\n”, acc[i].balance);
return;
}
}
printf(“Account not found!\n”);
}
void withdrawAmount(struct Account *acc, int count, char *acNo, double amount)
{
for (int i = 0; i < count; i++)
{
if (strcmp(acc[i].acNo, acNo) == 0)
{
if (acc[i].balance – amount < acc[i].minBalance)
{
printf(“Cannot withdraw! Balance will fall below minimum balance.\n”);
return;
}
acc[i].balance -= amount;
printf(“Amount withdrawn. New balance: %.2f\n”, acc[i].balance);
return;
}
}
printf(“Account not found!\n”);
}
Ron’e Dutta is a journalist, teacher, aspiring novelist, and blogger. He manages Online Free Notes and reads Victorian literature. His favourite book is Wuthering Heights by Emily Bronte and he hopes to travel the world. Get in touch with him by sending him a friend request.
Get notes of other boards, classes, and subjects