LabManual 09
LabManual 09
LAB MANUAL 09
CSC1102
Programming Language 1 [EEE]
Prepared By:
M. ARIFUR RAHMAN
1
TITLE
PREREQUISITE
OBJECTIVE
THEORY
STRUCTURE
When we handle real world data, we don’t usually deal with little atoms of information by
themselves—things like integers, characters and such. Instead we deal with entities that are
collections of things, each thing having its own attributes, just as the entity we call a ‘student’ is
a collection of things such as ID, Name, Semester, CGPA, Completed Credit, etc. As you can see
all this data is dissimilar, for example CGPA is a float, where as Semester is an integer. For dealing
with such collections, C provides a data type called ‘structure’. A structure gathers together,
different atoms of information that comprise a given entity.
For example, suppose you want to store data about a student. You might want to store the
students semester (an int) and CGPA (a float). If data about say 3 such students is to be stored,
then we can follow two approaches:
Construct individual arrays, one for storing semester, and another for storing CGPA.
Use a structure variable.
2
Let us examine these two approaches one by one.
Let us begin with a program that uses arrays.
#include<stdio.h>
void main(){
int semester[3];
float CGPA[3];
int i;
for(i=0;i<=2;i++)
{
printf("Enter semester: \n");
scanf("%d",&semester[i]);
printf("Enter CGPA: \n");
scanf("%f",&CGPA[i]);
}
printf("Output: \n");
for(i=0;i<=2;i++)
{
printf("Semester: %d \n",semester[i]);
printf("CGPA: %f \n",CGPA[i]);
}
}
This approach no doubt allows you to store semester and CGPA. But as you must have realized,
it is an unwieldy approach that obscures the fact that you are dealing with a group of
characteristics related to a single entity—the student.
The program becomes more difficult to handle as the number of items relating to student go on
increasing. For example, we would be required to use a number of arrays, if we also decide to
store student Name, ID, etc. To solve this problem, C provides a special data type—the structure.
A structure contains a number of data types grouped together. These data types may or may
not be of the same type. The following example illustrates the use of this data type.
#include<stdio.h>
void main()
{
struct Student{
int semester;
float CGPA;
};
3
struct Student s1, s2;
int i;
printf("Enter semester:\n");
scanf("%d",&s1.semester);
printf("Enter CGPA:\n");
scanf("%f",&s1.CGPA);
printf("You have entered: \n");
printf("Semester: %d\n",s1.semester);
printf("CGPA: %f \n",s1.CGPA);
printf("Enter semester:\n");
scanf("%d",&s2.semester);
printf("Enter CGPA:\n");
scanf("%f",&s2.CGPA);
printf("You have entered: \n");
printf("Semester: %d\n",s2.semester);
printf("CGPA: %f \n",s2.CGPA);
}
This program demonstrates two fundamental aspects of structures:
• Declaration of a structure
• Accessing of structure elements
DECLARING A STRUCTURE
In our example program, the following statement declares the structure type:
struct Student{
int semester;
float CGPA;
};
This statement defines a new data type called struct Student. Each variable of this data type will
consist of an integer variable called semester and a float variable called CGPA.
Once the new structure data type has been defined one or more variables can be declared to be
of that type. For example the variables s1, s2, s3 can be declared to be of the type struct Student,
as,
This statement sets aside space in memory. It makes available space to hold all the elements in
the structure—in this case, 6 bytes—four for CGPA and two for semester. These bytes are
always in adjacent memory locations.
4
Having declared the structure type and the structure variables, let us see how the elements of
the structure can be accessed.
In arrays we can access individual elements of an array using a subscript. Structures use a
different scheme. They use a dot (.) operator. So to refer to semester of the structure defined in
our sample program we have to use,
s1.semester
Similarly, to refer to CGPA we would use,
s1.CGPA
Note that before the dot there must always be a structure variable and after the dot there must
always be a structure element.
The program below will help clarify the concept of accessing structure elements.
#include<stdio.h>
void main()
{
struct Student{
int semester;
float CGPA;
};
struct Student s1={1,3.5},s2={2,3.75};
int i;
printf("Output: \n");
printf("Semester: %d\n",s1.semester);
printf("CGPA: %f \n",s1.CGPA);
printf("Semester: %d\n",s2.semester);
printf("CGPA: %f \n",s2.CGPA);
}
Whatever be the elements of a structure, they are always stored in contiguous memory locations.
The following program would illustrate this:
#include<stdio.h>
void main()
{
struct Student {
int semester;
int CGPA;
char ID[12];
};
struct Student s1={1,3.5,"11-18834-1"};
5
int i;
printf("Output: \n");
printf("Semester: %d\n",s1.semester);
printf("CGPA: %f \n",s1.CGPA);
printf("ID %s \n",s1.ID);
printf("\n");
printf("Address of Semester: %u\n",&s1.semester);
printf("Address of CGPA: %u \n",&s1.CGPA);
printf("Address of ID %u \n",&s1.ID);
}
ARRAY OF STRUCTURES
In our sample program, to store data of 100 students we would be required to use 100 different
structure variables from s1 to s100, which is definitely not very convenient. A better approach
would be to use an array of structures. Following program shows how to use an array of
structures.
#include<stdio.h>
int main()
{
struct Student {
int semester;
float CGPA;
};
struct Student s[10];
int i;
for(i=0;i<=9;i++)
{
printf("Enter semester:\n");
scanf("%d",&s[i].semester);
printf("Enter CGPA:\n");
scanf("%f",&s[i].CGPA);
}
for(i=0;i<=9;i++)
{
printf("You have entered: \n");
printf("Semester: %d\n",s[i].semester);
printf("CGPA: %f \n",s[i].CGPA);
}
}
APPLICATION OF STRUCTURES
Where are structures useful? The immediate application that comes to the mind is Database
Management. That is, to maintain data about employees in an organization, books in a library,
items in a store, financial accounting transactions in a company, students in a university etc. But
6
mind you, use of structures stretches much beyond database management. They can be used for
a variety of purposes like:
• Changing the size of the cursor
• Clearing the contents of the screen
• Placing the cursor at an appropriate position on screen
• Drawing any graphics shape on the screen
• Receiving a key from the keyboard
• Checking the memory size of the computer
• Finding out the list of equipment attached to the computer
• Formatting a floppy
• Hiding a file from the directory
• Displaying the directory of a disk
• Sending the output to printer
• Interacting with the mouse
And that is certainly a very impressive list! At least impressive enough to make you realize how
important structure is as a data type.
EXERCISE
II. Once the new structure data type has been defined one or more can be
declared to be of that type.
III. Before the dot there must always be a structure variable and after the dot there must
always be a element.
IV. Elements of a structure are always stored in memory location.
LAB WORK
I. Write a program that uses a structure Book to hold the Name, Price, Number of pages
and Name of publisher of 3 books.
II. Write a program that uses an array of structure Student to hold the Name, ID, Semester,
Age, and CGPA of 10 students. Find the average CGPA of the students, the students who
are under probation, the students aged more than 21 years and last semester students.
III. Write a program that uses an array of structures to hold the square and cubes of numbers
1 to 10. Display the contents of the array.