Basic Concepts
1. Define structure in the context of a programming language. How is it different from an array?
A structure in a programming language is a user-defined data type that allows grouping of variables of different data types under a single name. It differs from an array, which can only hold elements of the same data type and is indexed for access, whereas a structure provides a more flexible way to organize heterogeneous data.
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.
No, structure is not a built-in data type; it is user-defined. Basic arithmetic operations like addition and subtraction cannot be directly applied to structure variables. Here's a simple C program to demonstrate:
#include
struct Point {
int x;
int y;
};
int main() {
struct Point p1 = {3, 4};
struct Point p2 = {1, 2};
// Cannot directly add p1 and p2
printf("p1.x = %d, p1.y = %d\n", p1.x, p1.y);
printf("p2.x = %d, p2.y = %d\n", p2.x, p2.y);
return 0;
}
Code Analysis
3. Identify the members of the structure from the below code segment.
- char acNo[15]
- char ifsc[15]
- char acType[7]
- double balance
- double minBalance
4. Identify the structure variables from the below code segment.
- account1
- account2
- account3
- account[10]
Banking System
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)
#include
struct Account {
char acNo[15];
char ifsc[15];
char acType[7];
double balance;
double minBalance;
};
int main() {
struct Account account;
printf("Enter Account Number: ");
scanf("%s", account.acNo);
printf("Enter IFSC Code: ");
scanf("%s", account.ifsc);
printf("Enter Account Type: ");
scanf("%s", account.acType);
printf("Enter Initial Balance: ");
scanf("%lf", &account.balance);
account.minBalance = 1000.0;
printf("Account opened successfully!\n");
return 0;
}
- b. displaying details based on account number
void displayAccount(struct Account account, char* acNoToFind) {
if (strcmp(account.acNo, acNoToFind) == 0) {
printf("Account Number: %s\n", account.acNo);
printf("IFSC Code: %s\n", account.ifsc);
printf("Account Type: %s\n", account.acType);
printf("Balance: %.2f\n", account.balance);
printf("Minimum Balance: %.2f\n", account.minBalance);
}
}
- c. displaying all account details
void displayAll(struct Account accounts[], int n) {
for (int i = 0; i < n; i++) {
printf("Account %d:\n", i+1);
printf("Account Number: %s\n", accounts[i].acNo);
printf("IFSC Code: %s\n", accounts[i].ifsc);
printf("Account Type: %s\n", accounts[i].acType);
printf("Balance: %.2f\n", accounts[i].balance);
printf("Minimum Balance: %.2f\n", accounts[i].minBalance);
}
}
- d. displaying details of all accounts whose balance is more than 1000
void displayHighBalance(struct Account accounts[], int n) {
for (int i = 0; i < n; i++) {
if (accounts[i].balance > 1000) {
printf("Account %d:\n", i+1);
printf("Account Number: %s\n", accounts[i].acNo);
printf("Balance: %.2f\n", accounts[i].balance);
}
}
}
- e. depositing an amount to an account
void deposit(struct Account* account, double amount) {
if (amount > 0) {
account->balance += amount;
printf("Deposited %.2f. New Balance: %.2f\n", amount, account->balance);
} else {
printf("Invalid amount!\n");
}
}
- f. withdrawing some amount from an account
void withdraw(struct Account* account, double amount) {
if (amount > 0 && (account->balance - amount) >= account->minBalance) {
account->balance -= amount;
printf("Withdrawn %.2f. New Balance: %.2f\n", amount, account->balance);
} else {
printf("Invalid amount or insufficient balance!\n");
}
}
Advanced Structures
5. Consider the structure below and write statements for the following.
6. Declare a pointer for structure Teacher (from Q No. 5) and dynamically allocate memory for 10 records.
struct Teacher* ptr;
ptr = (struct Teacher*)malloc(10 * sizeof(struct Teacher));
7. Consider the structure below and write statements for the following.
- a. to declare a pointer for the above structure and display the salary
struct Employee* empPtr;
empPtr = &employee1;
printf("Salary: %.2f\n", empPtr->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* empPtr;
struct Employee employee1 = {"John", 5000.0};
struct Employee employee2 = {"Jane", 6000.0};
empPtr = (employee1.salary > employee2.salary) ? &employee1 : &employee2;
printf("Employee Name: %s\n", empPtr->name);
printf("Salary: %.2f\n", empPtr->salary);
8. Rewrite the program of Q. No. 7 to facilitate dynamic memory allocation for N number of record where N is a user input.
#include
#include
struct Employee {
char name[30];
double salary;
};
int main() {
int n;
printf("Enter the number of employees: ");
scanf("%d", &n);
struct Employee* empPtr = (struct Employee*)malloc(n * sizeof(struct Employee));
for (int i = 0; i < n; i++) {
printf("Enter name for employee %d: ", i+1);
scanf("%s", empPtr[i].name);
printf("Enter salary for employee %d: ", i+1);
scanf("%lf", &empPtr[i].salary);
}
for (int i = 0; i < n; i++) {
printf("Employee %d - Name: %s, Salary: %.2f\n", i+1, empPtr[i].name, empPtr[i].salary);
}
free(empPtr);
return 0;
}