Computer Programming Laboratory Experiment # 6: Structures Objectives

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Computer Programming Laboratory, Spring 2015

COMPUTER PROGRAMMING LABORATORY


Experiment # 6:
Structures

OBJECTIVES

The main purpose of the experiment is to introduce you to C structures. In this experiment,
firstly, definition and initialization of the structures is explained. Then, accessing structure
members concept is introduced. Lastly, passing structures to functions issue is analyzed and
some examples are studied.

INFORMATION

Structures

In many applications, we need to use a group of related values. For example, in a student
record, the name and surname, the student id, birth date, etc. should be stored together. As seen
from the example, generally, the related values are stored different types of variables. Structures
provide a way grouping the related values under the same name. A complete definition of C
Structures could be given as follows:

A struct in the C programming language (and many derivatives) is a complex data


type declaration that defines a physically grouped list of variables to be placed under one name
in a block of memory, allowing the different variables to be accessed via a single pointer, or the
struct declared name which returns the same address. [1].

Definition of Structures

Structures could be defined as follows:

struct pose{

double x;
double y;

};

In order to define a structure, the struct keyword should be used. After this keyword, pose could
be thought as user-defined variable type and generally called as structure tag. The tag represents
name of the entire type of structure. double x and double y are declared within the braces. The
variables are named members of the structure. Structure members may be defined one of the any
standard types. Additionally, array and pointers could also be used in definition of a struct
member. After right brace, the definition of the structure must end with semicolon(;).

There are two ways to declare structure type variables. Structure variables could be defined
like other standard variables such as integer, double, char, etc.

struct pose currentPose, robotPoseArray[100], * robotPosePtr;

In this statement, currentPose defines a variable of type pose. Similarly, robotPoseArray is


defined to store 100 elements with pose type. Lastly, a pointer is declared to handle pose type
variable.

E-mail : burakaleci@gmail.com
Computer Programming Laboratory, Spring 2015

On the other hand, structure variables are declared after definition of the structure as follows:

struct pose{

double x;
double y;

} currentPose, robotPoseArray[100], * robotPosePtr;

Initialization of Structures

There are two ways to initialize structure type variables. Structure variables could be
initialized with initializer list like as arrays. If there are fewer elements in the list, remaining
items are initialized to zero. In second way, initial values are assigned members of the structure
explicitly.

Way1

struct pose currentPose={3.7,4.5};

Way2

currentPose.x=3.7;
currentPose.y=4.5;

Accessing Structure Members

There are two operators to access members of the structure variables. If the variable is not a
pointer, the dot operator is used to assign, to reach or to use structure members. Otherwise, if the
variable is a pointer arrow operator is used for the same purposes. An example code for
structures is given in Fig. 1. In lines 5-9, pose structure is declared. Then, in lines 15 and 18,
pose type variable and pointer are defined, respectively. Initialization of the variable is explicitly
realized in lines 21-22. Lastly, accessing of the structure member is achieved by using dot
operator in line 28. Similarly, in line 31, arrow operator is used to access structure member of the
structure.

Fig. 1 An example code for structures.

E-mail : burakaleci@gmail.com
Computer Programming Laboratory, Spring 2015

Passing Structures to Functions

There are two ways to pass structures to a function: call-by-value and call-by-reference. In
call-by-value method, the entire structure or individual structure members could be send
function. In this case, the copy of the value is created and is used in the function. After the
function finishes its task one value may be return to caller function [2].

Pointers enable us to pass arguments to function call-by-reference. In this method, the address
of the structure variable passes the function by using address operator (&). The original
variable is used in the function and its value will be modified. Additionally, arrays of structure
are automatically passed by reference [2].

An example code to examine call-by-value and call-by-reference methods is given in Fig. 2.


In lines 12 and 13 prototypes of the functions are given. The functions are called in lines 28 and
30. Then, in function definitions, they just print the values of the members (lines 38 and 44).
Other cases such as passing individual structure members or arrays of structures are rest to
student.

Fig. 2 An example code for call-by-value and call-by-reference.

E-mail : burakaleci@gmail.com
Computer Programming Laboratory, Spring 2015

QUESTIONS

Consider we have a Grade structure as follows:

struct Grade {
int q1;
int q2;
int q3;
int mt;
int final;
double avg;
char *letter;
};

Define a structure variable and initialize the variable by using initializer list to zero. Then,
prompt the q1, q2, q3, mt, and final values from the user. The weights of the exams are 10% for
each quiz, 25% for midterm and %45 for final.

Avg Letter Grade


85-100 AA
65-84 BB
45-64 CC
35-44 DD
0-34 FF

1) Write a C program to calculate average of the exams and determine letter grades of the
student. Calculate average then assign it to avg member of the Grade structure. Lastly, determine
the letter grade according to given table and assign it to letter member.

2) Repeat 1). Use average function that receives the structure variable. In function, calculate
average then assign it to avg member. Lastly, determine the letter grade according to given table
and assign it to letter member.

3) Repeat 2). In function, calculate average then assign avg variables of the Grade structure.
Then, in main function, determine the letter grade according to given table and assign it to letter
member (Hint: Use call-by-reference).

4) Write a C program to calculate grade histogram of a course. Define a structure array that
stores 10 Grade structures (i.e struct Grade CP[10]), then initialize the array members by using
loop initialization method such that each element must contain (0-100) number randomly (i.e
CP[0].q1=rand%101). Use histogram function that receives the structure array. In function, for
each student, calculate average then assign avg members and determine the letter grade
according to given table assign it to letter member. Sort the array according to avg member.
Define a local array to store grade histogram of the course (i.e. int gradeHist[5]). Then, update
gradeHist array. Print the result.

[1] http://en.wikipedia.org/wiki/Struct_%28C_programming_language%29, 2017.


[2] P. J. Deitel and H. M. Deitel, C How To Program Fifth Edition, Prentice Hall, 2007.

E-mail : burakaleci@gmail.com

You might also like