Unit 4 Qba PDF
Unit 4 Qba PDF
Unit 4 Qba PDF
PART-A
Ans:3 bytes
15. Invent the application of size of operator to this structure. Consider the
declaration:
struct
{
char name;
intnum;
} student;
Ans.: Size of operator to this structure is 3 bytes.
16. Is it mandatory that the size of all elements in a union should be same?
No.The standard only guarantee that the size of a union is sufficient for the largest
member, i.e, not necessarily the same size.
17. Develop a structure namely Book and create array of Book structure with size of
ten.
struct Book
{
char book_title[20];
char author[20];
int cost;
};
int count;
struct Book b[10];
PART – B
Functions
• Return Type − A function may return a value. The return_type is the data type of
the value the function returns. Some functions perform the desired operations
without returning a value. In this case, the return_type is the keyword void.
• Function Name − This is the actual name of the function. The function name and
the parameter list together constitute the function signature.
• Parameters − A parameter is like a placeholder. When a function is invoked, you
pass a value to the parameter. This value is referred to as actual parameter or
argument. The parameter list refers to the type, order, and number of the
parameters of a function. Parameters are optional; that is, a function may contain
no parameters.
• Function Body − The function body contains a collection of statements that define
what the function does.
Example
Given below is the source code for a function called max(). This function takes two
parameters num1 and num2 and returns the maximum value between the two −
return result;
}
Structure:
structure is user defined data type available in C that allows to combine data items of
different kinds.
Defining a Structure
To define a structure, you must use the struct statement. The struct statement defines a
new data type, with more than one member. The format of the struct statement is as
follows −
struct[structure tag]{
member definition;
member definition;
...
member definition;
}[one or more structure variables];
The structure tag is optional and each member definition is a normal variable definition,
such as int i; or float f; or any other valid variable definition. At the end of the structure's
definition, before the final semicolon, you can specify one or more structure variables but
it is optional. Here is the way you would declare the Book structure −
structBooks{
char title[50];
char author[50];
char subject[100];
intbook_id;
} book;
Accessing Structure Members
To access any member of a structure, we use the member access operator (.). The
member access operator is coded as a period between the structure variable name and the
structure member that we wish to access. You would use the keyword struct to define
variables of structure type. The following example shows how to use a structure in a
program −
#include<stdio.h>
#include<string.h>
structBooks{
char title[50];
char author[50];
char subject[100];
intbook_id;
};
int main(){
/* book 1 specification */
strcpy(Book1.title,"C Programming");
strcpy(Book1.author,"Nuha Ali");
strcpy(Book1.subject,"C Programming Tutorial");
Book1.book_id =6495407;
/* print Book1 info */
printf("Book 1 title : %s\n",Book1.title);
printf("Book 1 author : %s\n",Book1.author);
printf("Book 1 subject : %s\n",Book1.subject);
printf("Book 1 book_id : %d\n",Book1.book_id);
return0;
}
To define a structure, you must use the struct statement. The struct statement defines
a new data type, with more than one member. The format of the struct statement is as
follows −
struct[structure tag]{
member definition;
member definition;
...
member definition;
}[one or more structure variables];
The structure tag is optional and each member definition is a normal variable
definition, such as int i; or float f; or any other valid variable definition. At the end
of the structure's definition, before the final semicolon, you can specify one or more
structure variables but it is optional. Here is the way you would declare the Book
structure −
structBooks{
char title[50];
char author[50];
char subject[100];
intbook_id;
} book;
Accessing Structure Members
To access any member of a structure, we use the member access operator (.). The
member access operator is coded as a period between the structure variable name
and the structure member that we wish to access. You would use the keyword struct
to define variables of structure type. The following example shows how to use a
structure in a program −
#include<stdio.h>
#include<string.h>
structBooks{
char title[50];
char author[50];
char subject[100];
intbook_id;
};
int main(){
/* book 1 specification */
strcpy(Book1.title,"C Programming");
strcpy(Book1.author,"Nuha Ali");
strcpy(Book1.subject,"C Programming Tutorial");
Book1.book_id =6495407;
return0;
}
Array of Structures: It is possible to create an array whose elements are of structure type.
Such an array is known as array of structures.
ARRAY OF STRUCTURES
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[30];
float percentage;
};
int main()
{
int i;
struct student record[2];
OUTPUT:
Records of STUDENT : 1
Id is: 1
Name is: Raju
Percentage is: 86.500000
Records of STUDENT : 2
Id is: 2
Name is: Surendren
Percentage is: 90.500000
Records of STUDENT : 3
Id is: 3
Name is: Thiyagu
Percentage is: 81.500000
• Nested Structure
struct Date
{
int dd;
int mm;
int yyyy;
};
struct Employee
{
int id;
char name[20];
struct Date doj;
}emp1;
As you can see, doj (date of joining) is the variable of type Date. Here doj is used as a
member in Employee structure. In this way, we can use Date structure in many structures.
1. e1.doj.dd
2. e1.doj.mm
3. e1.doj.yyyy
4. Write a C program using structures to prepare the students mark statement. (13)
#include<stdio.h>
#include<conio.h>
structmark_sheet{
char name[20];
long introllno;
int marks[10];
int total;
float average;
char rem[10];
char cl[20];
}students[100];
int main(){
inta,b,n,flag=1;
char ch;
clrscr();
printf("How many students : \n");
scanf("%d",&n);
for(a=1;a<=n;++a){
clrscr();
printf("\n\nEnter the details of %d students : ", n-a+1);
printf("\n\nEnter student %d Name : ", a);
scanf("%s", students[a].name);
printf("\n\nEnter student %d Roll Number : ", a);
scanf("%ld", &students[a].rollno);
students[a].total=0;
for(b=1;b<=5;++b){
printf("\n\nEnter the mark of subject-%d : ", b);
scanf("%d", &students[a].marks[b]);
students[a].total += students[a].marks[b];
if(students[a].marks[b]<40)
flag=0;
}
students[a].average = (float)(students[a].total)/5.0;
if((students[a].average>=75)&&(flag==1))
strcpy(students[a].cl,"Distinction");
else
if((students[a].average>=60)&&(flag==1))
strcpy(students[a].cl,"First Class");
else
if((students[a].average>=50)&&(flag==1))
strcpy(students[a].cl,"Second Class");
else
if((students[a].average>=40)&&(flag==1))
strcpy(students[a].cl,"Third Class");
if(flag==1)
strcpy(students[a].rem,"Pass");
else
strcpy(students[a].rem,"Fail");
flag=1;
}
for(a=1;a<=n;++a){
clrscr();
printf("\n\n\t\t\t\tMark Sheet\n");
printf("\nName of Student : %s", students[a].name);
printf("\t\t\t\t Roll No : %ld", students[a].rollno);
printf("\n------------------------------------------------------------------------");
for(b=1;b<=5;b++){
printf("\n\n\t Subject %d \t\t :\t %d", b, students[a].marks[b]);
}
printf("\n\n------------------------------------------------------------------------ \n");
printf("\n\n Totl Marks : %d", students[a].total);
printf("\t\t\t\t Average Marks : %5.2f", students[a].average);
printf("\n\n Class : %s", students[a].cl);
printf("\t\t\t\t\t Status : %s", students[a].rem);
printf("\n\n\n\t\t\t\t Press Y for continue . . . ");
ch = getche();
if((ch=="y")||(ch=="Y"))
continue;
}
return(0);
}
Output:
How many students : 2
Enter the details of 2 students :
Enter student 1 Name : H.Xerio
Enter student 1 Roll Number : 536435
Enter the mark of subject-1 : 46
Enter the mark of subject-1 : 56
Enter the mark of subject-1 : 76
Enter the mark of subject-1 : 85
Enter the mark of subject-1 : 75
Enter the details of 1 students :
Enter student 1 Name : K.Reriohe
Enter student 1 Roll Number : 237437
Enter the mark of subject-1 : 36
Enter the mark of subject-1 : 52
Enter the mark of subject-1 : 66
Enter the mark of subject-1 : 45
Enter the mark of subject-1 : 74
Mark Sheet
Name of Student : H.Xerio Roll No : 536435
------------------------------------------------------------------------
subject 1 : 46
subject 2 : 56
subject 3 : 76
subject 4 : 85
subject 5 : 75
------------------------------------------------------------------------
Total Marks : 338 Average Marks : 67.6
Class : First Class Status : Pass
5. Write a C program to add two distances in feet and inches using structure (13)
#include<stdio.h>
struct Distance {
int feet;
float inch;
} d1, d2, sumOfDistances;
int main() {
printf("Enter information for 1st distance\n");
printf("Enter feet: ");
scanf("%d", &d1.feet);
printf("Enter inch: ");
scanf("%f", &d1.inch);
printf("\nEnter information for 2nd distance\n");
printf("Enter feet: ");
scanf("%d", &d2.feet);
printf("Enter inch: ");
scanf("%f", &d2.inch);
sumOfDistances.feet=d1.feet+d2.feet;
sumOfDistances.inch=d1.inch+d2.inch;
struct book
{
char book_title[20];
char author[20];
int cost;
};
int count;
struct book b[200];
void main()
{
intch;
while(1)
{
clrscr();
printf("\n 1:Read book \n");
printf("\n 2:Total cost of the books \n");
printf("\n 3:Display books \n");
printf("\n 4:exit \n");
printf("\n\n enter the choice \n");
scanf("%d",&ch);
switch(ch)
{
case 1: read_book(); getch();
break;
case 2: book_cost(); getch();
break;
case 3: disp_book(); getch();
break;
case 4:exit(0);
}
}
}
void read_book()
{
for(count=0;count<200;count++) {
printf("\n enter book detail ");
printf("\n enter name of book\n");
scanf("%s",b[count].book_title);
printf("\n enter name of author\n");
scanf("%s",b[count].author);
printf("\n enter book cost \n");
scanf("%d",&b[count].cost);
}
}
void disp_book()
{
int i;
printf("\n detail of %d book ",count);
for(i=0;i<200;i++)
{
printf("\n %s\n%s\n%d",b[i].book_title,b[i].author,b[i].cost);
}
void book_cost()
{
intI,p=0;
for(i=0;i<200;i++)
{
p=p+b[i].cost;
{
printf("\nTotal cost %d",p);
}
}
}
7. (i) Express a structure with data members of various types and declare two structure
variables. Write a program to read data into these and print the same. (8)
#include <stdio.h>
struct student {
char firstName[20];
char lastName[20];
char SSN[10];
float gpa;
};
main()
{
struct student student_a;
strcpy(student_a.firstName, "Deo");
strcpy(student_a.lastName, "Dum");
strcpy(student_a.SSN, "2333234" );
student_a.gpa = 2009.20;
Output
Example:
In C, the only operation that can be applied to struct variables is assignment. Any other
operation (e.g. equality check) is not allowed on struct variables.
For example, program 1 works without any error and program 2 fails in compilation.
Program 1
#include <stdio.h>
structPoint {
intx;
inty;
};
intmain()
{
structPoint p1 = {10, 20};
structPoint p2 = p1; // works: contents of p1 are copied to p1
printf(" p2.x = %d, p2.y = %d", p2.x, p2.y);
getchar();
return0;
}
Program 2
#include <stdio.h>
structPoint {
intx;
inty;
};
intmain()
{
structPoint p1 = {10, 20};
structPoint p2 = p1; // works: contents of p1 are copied to p1
if(p1 == p2) // compiler error: cannot do equality check for
// whole structures
{
printf("p1 and p2 are same ");
}
getchar();
return0;
}
typemember1;
typemember2;
……………};
Example:
#include <stdio.h>
struct student {
char firstName[20];
char lastName[20];
char SSN[10];
float gpa;
};
main()
{
struct student student_a;
strcpy(student_a.firstName, "Deo");
strcpy(student_a.lastName, "Dum");
strcpy(student_a.SSN, "2333234" );
student_a.gpa = 2009.20;
Output
(ii) Create enum of week days. Write a program in C, use this enum and display it.
(6)
enum week{Mon=10, Tue, Wed, Thur, Fri=10, Sat=16, Sun};
enum day{Mond, Tues, Wedn, Thurs, Frid=18, Satu=11, Sund};
int main() {
printf("The value of enum week: %d\t%d\t%d\t%d\t%d\t%d\t%d\n\n",Mon , Tue, Wed,
Thur, Fri, Sat, Sun);
printf("The default value of enum day: %d\t%d\t%d\t%d\t%d\t%d\t%d",Mond , Tues,
Wedn, Thurs, Frid, Satu, Sund);
return 0;
}
Output
The value of enum week: 10 11 12 13 10 16 17
The default value of enum day: 0 1 2 3 18 11 12
struct tag
{
member 1;
member 2;
…………
…………
struct tag *name;
};
The structure of type tag contains a member, ‘name’ which is a pointer to structure of
type tag . Thus tag is a self referential structure.
struct node
int data;
Example Program:
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
void create();
voidinsert_atfirst();
voidinsert_atlast();
void insert();
voiddelete_bypos();
voiddelete_byval();
void display();
int menu();
struct node
{
int data;
struct node *next;
}*first=NULL,*last=NULL,*temp=NULL;
void main()
{
intch;
clrscr();
do{
ch=menu();
switch(ch)
{
case 1:create();
break;
case 2:insert_atfirst();
break;
case 3:insert_atlast();
break;
case 4:insert();
break;
case 5:delete_bypos();
break;
case 6:delete_byval();
break;
case 7:display();
break;
case 8:exit(0);
case 9:clrscr();
default:printf("\nError-->Enter a valid choice!!");
exit(0);
}
}while(1);
}
11. Explain nested structure and write C Program to Implement the same. (13)
struct Date
{
int dd;
int mm;
int yyyy;
};
struct Employee
{
int id;
char name[20];
struct Date doj;
}emp1;
Accessing Nested Structure
1. e1.doj.dd
2. e1.doj.mm
3. e1.doj.yyyy
#include<stdio.h>
struct address
{
char city[20];
int pin;
char phone[14];
};
struct employee
{
char name[20];
struct address add;
};
void main ()
{
struct employee emp;
printf("Enter employee information?\n");
scanf("%s %s %d %s",emp.name,emp.add.city, &emp.add.pin, emp.add.phone);
printf("Printing the employee information....\n");
printf("name: %s\nCity: %s\nPincode: %d\nPhone:
%s",emp.name,emp.add.city,emp.add.pin,emp.add.phone);
}
12. Compare with example code for Structure and Union. (13)
Similarities between Structure and Union
1. Both are user-defined data types used to store data of different types as a single
unit.
2. Their members can be objects of any type, including other structures and unions or
arrays. A member can also consist of a bit field.
3. Both structures and unions support only assignment = and sizeof operators. The two
structures or unions in the assignment must have the same members and member
types.
4. A structure or a union can be passed by value to functions and returned by value by
functions. The argument must have the same type as the function parameter. A
structure or union is passed by value just like a scalar variable as a corresponding
parameter.
5. ‘.’ operator is used for accessing members.
Differences
// declaring structure
structstruct_example
{
int integer;
float decimal;
char name[20];
};
// declaraing union
unionunion_example
{
int integer;
float decimal;
char name[20];
};
13. Illustrate a C program to store the employee information using structure and search
a particular employee details.(13)
• Define a structure called emp that would contain name, empno and
pay,allow and ded of n employees. Read the details of n employees and
calculated the net pay and display theemployee details.
• In this program, also read a particular employee no and search the employee
no .
• If it is found, display the details otherwise display not found.
The program to store details of an employee in a structure.
#include<stdio.h>
#include<conio.h>
struct emp
{
int empno ;
char name[10] ;
int bpay, allow, ded, npay ;
} e[10] ;
void main()
{
int i, n,no,f=1;
clrscr() ;
printf("Enter the number of employees : ") ;
scanf("%d", &n) ;
for(i = 0 ; i < n ; i++)
{
printf("\nEnter the employee number : ") ;
scanf("%d", &e[i].empno) ;
printf("\nEnter the name : ") ;
scanf("%s", e[i].name) ;
printf("\nEnter the basic pay, allowances & deductions : ") ;
scanf("%d %d %d", &e[i].bpay, &e[i].allow, &e[i].ded) ;
e[i].npay = e[i].bpay + e[i].allow - e[i].ded ;
}
printf("\nEmp. No. Name \t Bpay \t Allow \t Ded \t Npay \n\n") ;
for(i = 0 ; i < n ; i++)
{
printf("%d \t %s \t %d \t %d \t %d \t %d \n", e[i].empno,
e[i].name, e[i].bpay, e[i].allow, e[i].ded, e[i].npay) ;
}
14. Define a structure called student that would contain name, regno and marks of five
subjects and percentage. Write a C program to read the details of name, regno and
marks of five subjects for 30 students and calculate the percentage and display the
name, regno, marks of 30 subjects and percentage of each student.(13)
#include<stdio.h>
#include<conio.h>
struct stud{
char name[20];
long int rno;
int marks[5];
intp;
}students[30];
void calcpercentage(int);
int main(){
int a,b,j;
clrscr();
for(a=0;a<30;++a){
clrscr();
printf("\n\nEnter the details of %d student : ", a+1);
printf("\n\nEnter student %d Name : ", a);
scanf("%s", students[a].name);
printf("\n\nEnter student %d Reg Number : ", a);
scanf("%ld", &students[a].rno);
for(b=0;b<=4;++b){
printf("\n\nEnter the mark of subject-%d : ", b+1);
scanf("%d", &students[a].marks[b]);
}
}
}
calcpercentage(30);
for(a=0;a<30;++a){
clrscr();
printf("\n\n\t\t\t\tMark Sheet\n");
printf("\nName of Student : %s", students[a].name);
printf("\t\t\t\t Reg No : %ld", students[a].rno);
printf("\n------------------------------------------------------------------------");
printf("\n\n\t Percentage \t\t :\t %d", students[a].p);
}
printf("\n\n------------------------------------------------------------------------\n");
getch();
}
return(0);
}
void calcpercentage(int n)
{
int a,b,j,total;
for(a=1;a<=n;++a){
total=0;
for(j=0;j<=4;++j){
total += students[a].marks[j];
}
students[a].p=total/5;
}
}
}
PART-C
1. Write a structure to store the name, account number and balance of customers
(more than 10) and store their information.Write a function to print the names of all
the customers having balance less than $200.(15)
#include<stdio.h>
/* Defining Structre*/
struct bank
{
int acc_no;
char name[20];
int bal;
}b[15];
/*Function to find the details of customer whose balance <200.*/
void check(struct bank b[],int n) /*Passing Array of structure to function*/
{
int i;
printf("\nCustomer Details whose Balance <200 Rs. \n");
printf("----------------------------------------------\n");
for(i=0;i<n;i++)
{
if(b[i].bal<200)
{
printf("Account Number : %d\n",b[i].acc_no);
printf("Name : %s\n",b[i].name);
printf("Balance : %d\n",b[i].bal);
printf("------------------------------\n");
}
}
}
int main()
{
int i;
for(i=0;i<15;i++)
{
printf("Enter Details of Customer %d\n",i+1);
printf("------------------------------\n");
printf("Enter Account Number : ");
scanf("%d",&b[i].acc_no);
printf("Enter Name : ");
scanf("%s",b[i].name);
printf("Enter Balance : ");
scanf("%d",&b[i].bal);
printf("------------------------------\n");
}
check(b,15); //call function check
return 0;
}
2. Write a C program using structures to prepare the employee pay roll of a company.
(13)
#include<stdio.h>
#include<conio.h>
Struct employee
{
char ename[30];
int eno;
float bsal;
};
void main()
{
Struct employee emp;
char name[30];
int id;
float basic,hra,da,tds,net,gross;
clrscr();
printf("\EMPLOYEE PAYROLL PROCESSING\n");
printf("\----------------------------\n");
printf("DETAILS OF THE EMPLOYEE\n");
printf("Enter the Employee Name: ");
scanf("%s",emp.ename);
strcpy(name,emp.ename);
printf("Enter the Employee Id: ");
scanf("%d",&emp.eno);
id=emp.eno;
printf("Enter the Basic Salary: ");
scanf("%f",&emp.bsal);
basic=emp.bsal;
hra=basic*.10;
da=basic*.35;
tds=basic*.15;
gross=basic+hra+da;
net=gross-tds;
printf("SALARY DETAILS FOR THE MONTH\n");
printf("-----------------------------");
printf("\nEmployee Name\t: %s",name);
printf("\nEmployee No.\t: %d",id);
printf("\nBasic salary\t: %.2f",basic);
printf("\nHRA\t\t: %.2f",hra);
printf("\nDA\t\t: %.2f",da);
printf("\nTDS\t\t: %.2f",tds);
printf("\nGross Salary\t: %.2f",gross);
printf("\nNet Salary\t: %.2f",net);
getch();
}
Output:
EMPLOYEE PAYROLL PROCESSING
--------------------------------------------
DETAILS OF THE EMPLOYEE
Enter the Employee Name: Arun
Enter the Employee Id. : 2001
Enter the Basic Salary : 10000
SALARY DETAILS FOR THE MONTH
--------------------------------------------
Employee Name : Arun
Employee Id : 2001
Basic Salary : 10000.00
HRA : 1000.00
DA : 3500.00
TDS : 1500.00
Gross Salary : 14500.00
Net Salary : 13000.00
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[30];
float percentage;
};
int main()
{
int i;
struct student record[2];
OUTPUT:
Records of STUDENT : 1
Id is: 1
Name is: Raju
Percentage is: 86.500000
Records of STUDENT : 2
Id is: 2
Name is: Surendren
Percentage is: 90.500000
Records of STUDENT : 3
Id is: 3
Name is: Thiyagu
Percentage is: 81.500000
Nested structures
A structure can be nested within another structure.
struct Date
{
int dd;
int mm;
int yyyy;
};
struct Employee
{
int id;
char name[20];
struct Date doj;
}emp1;
Accessing Nested Structure
1. e1.doj.dd
2. e1.doj.mm
3. e1.doj.yyyy
#include<stdio.h>
struct address
{
char city[20];
int pin;
char phone[14];
};
struct employee
{
char name[20];
struct address add;
};
void main ()
{
struct employee emp;
printf("Enter employee information?\n");
scanf("%s %s %d %s",emp.name,emp.add.city, &emp.add.pin, emp.add.phone);
printf("Printing the employee information....\n");
printf("name: %s\nCity: %s\nPincode: %d\nPhone:
%s",emp.name,emp.add.city,emp.add.pin,emp.add.phone);
}
The memory occupied by a union will be large enough to hold the largest member of the
union. For example, in the above example, Data type will occupy 20 bytes of memory
space because this is the maximum space which can be occupied by a character string.
#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
char str[20];
};
int main( ) {
data.i = 10;
printf( "data.i : %d\n", data.i);
data.f = 220.5;
printf( "data.f : %f\n", data.f);
return 0;
}
When the above code is compiled and executed, it produces the following result −
data.i : 10
data.f : 220.500000
data.str : C Programming
Here, all the members are getting printed very well because one member is being used at
a time.