CH-9 STRUCTURE IN C

1. Define structure in the context of a programming language. How is it different from an array?
Ans:- 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.
Ans:- No,Structure is not a built-in data type .It is a user-defined data type that allows us to combine data of different types together.
Yes,we can apply basic arithmetic operations such as addition, subtraction to structure variables.
Code:-

#include<stdio.h>
struct Calculate
{
int n1, n2;
int sum,dif;
};
int main()
{
struct Calculate c;
printf("\nEnter 1st number : ");
scanf(" %d", &c.n1);
printf("\nEnter 2nd number : ");
scanf("%d",&c.n2);
c.sum = c.n1 + c.n2;
c.dif=c.n1-c.n2;
printf("\n Sum is :- %d ", c.sum);
printf("\nDifference is :- %d ", c.dif);
return 0;
}

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 accountl, account2, account3, account[10];

Ans:- Members of the structure from the above code segment are :- acNo,ifsc,acType,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 accountl, account2, account3, account[10];

Ans:- Variables of the structure from the above code segment are : accountl, account2, account3, account[10].

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;
};

Ans:-
a. struct Teacher t1;
b. printf(“\n Age is :- %d”,t1.age);
Code :-

#include<stdio.h>
#include<stdlib.h>
struct Teacher
{
char name[30];
int age;
};
int main()
{
struct Teacher t1;// declare structure variable
printf("Enter age:-");
scanf("%d",&t1.age);
printf("\n Age is :- %d",t1.age);// display age of teacher
return 0;
}

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

Code:-

#include<stdio.h>
#include<stdlib.h>
struct Teacher
{
char name[30];
int age;
};
int main()
{
struct Teacher t1;
struct Teacher*ptr;
ptr=&t1;
ptr=(struct Teacher*)malloc(10*sizeof(struct Teacher ));
printf("Enter age:-");
for(int i=0;i<10;i++)
{
scanf("%d",&(ptr+i)->age);
}
printf("\n Entered age are as follows :-");

for(int i=0;i<10;i++){
printf("\n%d",(ptr+i)->age);
}
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.

struct Employee
{
char name[30];
double salary;
}:

Code :-

#include<stdio.h>
#include<stdlib.h>

struct Employee
{
char name[30];
double salary;

};

int main()
{
struct Employee obj1;
struct Employee *ptr;
ptr=&obj1;
printf("\n Enter the name of the first employee: ");
gets(ptr->name);
printf("\n Enter the salary: ");
scanf("%lf",&ptr->salary);
printf("\n Details of the employee is as follows :\n");
printf("\n Name is= ");
puts(ptr->name);
printf("\n Salary = %lf",ptr->salary);
return 0;
}

b. to declare a single pointer for two different variables of the higher structure am display the details of the employee whose salary is more.

struct Employee
{
char name[30];
double salary;
}:

Code :-

#include<stdio.h>
#include<stdlib.h>

struct Employee
{
char name[30];
double salary;
};
int main()
{
struct Employee e1, e2;
struct Employee *ptr;
ptr=&e1;
printf("\n Enter the name of the first employee: ");
gets(ptr->name);
printf("\n Enter the salary: ");
scanf("%lf",&ptr->salary);
ptr=&e2;
printf("\n Enter tthe name of the second employee:");
getchar();
gets(ptr->name);
printf("\n Enter the salary: ");
scanf("%lf",&ptr->salary);

printf("\n Details of the employee whose salary is more ");
if (e1.salary>e2.salary)
{
printf("\n Name: ");
puts(e1.name);
printf("\n Salary=%lf", e1.salary);
}
else
{
printf("\n Name: ");
puts(e2.name);
printf("\n Salary = %lf", e2.salary);
}
return 0;
}

8. Rewrite the program of Q. No. 7 to facilitate dynamic memory allocation for N number of record where N in a user input.
Code:-

#include<stdio.h>
#include<stdlib.h>
struct Employee
{
char name[30];
double salary;
};
int main()
{
struct Employee *ptr;
int i, n;
printf("\n Enter the number of employees:");
scanf("%d", &n);
ptr= (struct Employee*)malloc(n* sizeof (struct Employee));
for(i=0;i<n;i++)
{
printf("\n Enter the name of the employee: ");
getchar();
gets((ptr+i)->name);
printf("\n Enter the salary: ");
scanf("%lf", &(ptr+i)->salary);
}
printf("\n Details of the employees are as follows : ");
for(i=0;i<n;i++)
{
printf("\n Name of the employee: ");
puts((ptr+i)->name);
printf("\n the salary: ");
printf("%lf",(ptr+i)->salary);
}
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];
double balance;
char acType[8];
double minBalance;
};

Code :-

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Account
{
char acNo[15];
char ifsc[15];
double balance;
char acType[8];
double minBalance;
};
int main()
{
int n, choice;
char ans, s[15];
double d, w;
struct Account act[2];
do
{
printf("\n 1. Opening of accounts.");
printf("\n 2. Displaying details based on account number");
printf("\n 3. Displaying all account details");
printf("\n 4. Displaying details of all accounts whose balance is more than 1000");
printf("\n 5. Depositing an amount from an account");
printf("\n 6. Withdrawing some amount from an account");
printf("\n 7. Exit");
printf("\n Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
for (int i=0; i<2; i++)
{
printf("\n Enter account number:");
getchar();
gets(act[i].acNo);
printf("\n Enter IFSC code :");
gets(act[i].ifsc);
printf("\n Enter account type (Saving/Current):");
gets(act[i].acType);
printf("\n Enter account balance:");
scanf("%lf",&act[i].balance);
}
break;
case 2:
printf("\n Enter the account number to display its details: ");
getchar();
gets(s);
for (int i=0; i<2; i++)
{
if (strcmp(act[i].acNo,s)==0)
{
printf("\n Account no. :");
puts(act[i].acNo);
printf("\n IFSC code: ");
puts(act[i].ifsc);
printf("\n Account type: ");
puts(act[i].acType);
printf("\n Balance: %lf", act[i].balance);
}}
break;
case 3:
printf("\n Displaying all account details \n");
for (int i=0; i<2; i++)
{
printf("\n Account no.: ");
puts(act[i].acNo);
printf("\n IFSC code: ");
puts(act[i].ifsc);
printf("\n Account type: ");
puts(act[i].acType);
printf("\n Balance: %lf", act[i].balance);
}
break;
case 4:
for (int i=0; i<2; i++)
{
if(act[i].balance>1000)
{
printf("\n Account no.: ");
puts(act[i].acNo);
printf("\n IFSC code: ");
puts(act[i].ifsc);
printf("\n Account type: ");
puts(act[i].acType);
printf("\n Balance: %lf", act[i].balance);
} }
break;
case 5:
printf("\n Enter the account number to deposit amount:");
getchar();
gets(s);
for (int i=0; i<2; i++)
{
if (strcmp(act[i].acNo,s)==0)
{
printf("\n Enter the amount to be deposited: ");
scanf("%lf", &d);
act[i].balance=act[i].balance+d;
printf("\n New balance is %lf: ", act[i].balance);
}}
break;
case 6:
printf("\n Enter the account number to withdraw amount: ");
getchar();
gets(s);
for (int i=0; i<2; i++)
{
if (strcmp(act[i].acNo,s)==0)
{
printf("\n Enter the amount to be withdrawn: ");
scanf("%f",&w);
act[i].balance = act[i].balance-w;
printf("\n New balance is %lf: ", act[i].balance);
} }
break;
case 7:
return 0;
}
printf("\n Do you want to continue: (Y/N)");
getchar();
scanf("%c",&ans);
}while(ans == 'y' || ans == 'Y');
return 0;
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here