Anda di halaman 1dari 14

Design and Analysis of Algorithms 214.

Introduction to the C Programming Language.

Structures and Union, Merge sort


Algorithm.

U.U.Samantha Rajapaksha
B.Sc.(Eng.)

Sri Lanka Institute of Information Technology.


Samantha.r@sliit.lk
0112301904

DAA 214 Sri Lanka Institute of Information Technology.


Structures
 A structure is a collection of one or more variables, possibly of
different types, grouped together under a single name for
convenient handling. (Structures are called ``records'' in some
languages, notably Pascal.)
 Structures help to organize complicated data, particularly in large
programs, because they permit a group of related variables to be
treated as a unit instead of as separate entities.
 Arrays require that all elements be of the same data type. Many
times it is necessary to group information of different data types. An
example is a materials list for a product. The list typically includes a
name for each item, a part number, dimensions, weight, and cost.
 C and C++ support data structures that can store combinations of
character, integer floating point and enumerated type data. They
are called a structs.

DAA 214 Lab 02 Sri Lanka Institute of Information Technology.


The struct statement
 The keyword struct introduces a structure declaration, which is a list
of declarations enclosed in braces. An optional name called a
structure tag may follow the word struct.
 The variables named in a structure are called members.
 A struct declaration defines a type. The right brace that terminates the
list of members may be followed by a list of variables, just as for any
basic type. That is,
 struct { ... } x, y, z;
 is syntactically analogous to int x, y, z; in the sense that each
statement declares x, y and z to be variables of the named type and
causes space to be set aside for them.
 A structure declaration that is not followed by a list of variables
reserves no storage; it merely describes a template or shape of a
structure.

DAA 214 Lab 02 Sri Lanka Institute of Information Technology.


Declaring Structures.
Reserves no storage Reserves storage

struct rec { struct rec {


int i; int i;
int a[3]; int a[3];
int *p; int *p;
}; } Mystruc;

Memory Layout
i a p
0 4 16 20

DAA 214 Lab 02 Sri Lanka Institute of Information Technology.


User Defined Data Types (typedef)
 The C language provides a facility called typedef for
creating synonyms for previously defined data type
names. For example, the declaration:
typedef int Length;
makes the name Length a synonym (or alias) for the data
type int.
 The data “type” name Length can now be used in
declarations in exactly the same way that the data type int
can be used:
Length a, b, len ;
Length numbers[10] ;

DAA 214 Lab 02 Sri Lanka Institute of Information Technology.


Typedef & Struct
 Often, typedef is used in combination with struct to declare
a synonym (or an alias) for a structure:

typedef struct
{
int number ;
char letter;
char name[10] ;
} any_name ;
any_name mystruct ;

DAA 214 Lab 02 Sri Lanka Institute of Information Technology.


Accessing Struct Members
 Individual members of a struct variable may be accessed using the
structure member operator (the dot, “.”):
mystruct.letter ;

 If a pointer to the struct has been declared and initialized


any_name *myptr = &mystruct ;

by using the structure pointer operator (the “->“):


myptr -> letter ;
which could also be written as:
(*myptr).letter ;

DAA 214 Lab 02 Sri Lanka Institute of Information Technology.


A struct in memory
 In memory, the amount of space a struct takes is equal to the sum of
the size of its components.
 In memory, the individual elements of the struct are stored
consecutively.

int main( ) {
struct coordinates { coordinates c1;
unsigned char x; coordinates c2;
c1.x = 3;
unsigned char y; c1.y = 2;
unsigned char z; c1.z = 1;
}; c2.x = 0;
c2.y = 2;
c2.z = 3;
return 0;
}
c1 c2

00000011 00000010 00000001 00000001 00000010 00000011


DAA 214 Lab 02 Sri Lanka Institute of Information Technology.
Struct within struct
 Since a struct is just another type, then you can put it inside
another struct.

struct coordinates {
unsigned char x;
unsigned char y; int main( ) {
unsigned char z;
}; new m1;
m1.position.x = 3;
struct new { return 0;
coordinates position;
char name[32]; }
int size;
};

DAA 214 Lab 02 Sri Lanka Institute of Information Technology.


Pointer to struct
 Since a struct is just another type, you can also declare a
pointer to a struct. int main( ) {

struct coordinates { new *newptr;


unsigned char x;
unsigned char y; (*newptr).position.x = 3;
unsigned char z; }
};

struct new { int main( ) {


coordinates position;
char name[32]; new * newptr;
int size;
}; newptr ->position.x = 3;

DAA 214 Lab 02 Sri Lanka Institute of Information Technology.


Union
 A union is a variable that may hold (at different times)
objects of different types and sizes, with the compiler
keeping track of size and alignment requirements.
 Unions provide a way to manipulate different kinds of data
in a single area of storage, without embedding any
machine dependent information in the program. They are
analogous to variant records in pascal.
 union is a data structure that stores one of several types
of data at a single location.

DAA 214 Lab 02 Sri Lanka Institute of Information Technology.


Union Allocation
union U1 {
char c; c
int i[2]; i[0] i[1]
double v; v
} *up; up+0 up+4 up+8

struct S1 {
char c;
int i[2];
double v;
} *sp;
(Windows alignment)

c i[0] i[1] v
sp+0 sp+4 sp+8 sp+16 sp+24

DAA 214 Lab 02 Sri Lanka Institute of Information Technology.


#include <stdio.h>
#define CPU_VENDOR_OS "i386-pc-bsdi3.0"
int
main(int argc, char **argv)
{
union {
short s;
char c[sizeof(short)];
} un;
un.s = 0x0102;
printf("%s: ", CPU_VENDOR_OS);
if (sizeof(short) == 2) {
if (un.c[0] == 1 && un.c[1] == 2)
printf("big-endian\n");
else if (un.c[0] == 2 && un.c[1] == 1)
printf("little-endian\n");
else
printf("unknown\n");
} else
printf("sizeof(short) = %d\n", sizeof(short));
exit(0);
}
DAA 214 Lab 02 Sri Lanka Institute of Information Technology.
Exercise
 Write a program to read a set of numbers and store them on an array. Then
using the following psedocode for merge procedure for merge sort algorithm,
divide the array into two parts giving the partition point.

MERGE(A, p, q, r)
1 n1 ← q - p + 1
2 n2 ← r - q
3 create arrays L[1.. n1 + 1] and R[1.. n2 + 1]
4 for i ← 1 to n1
5 do L[i] ← A[p + i - 1]
6 for j ← 1 to n2
7 do R[j] ← A[q + j]
8 L[n1 + 1] ← ∞
9 R[n2 + 1] ← ∞
10 i ← 1
11 j ← 1
12 for k ← p to r
13 do if L[i] ≤ R[j]
14 then A[k] ← L[i]
15 i←i+1
16 else A[k] ← R[j]
17 DAA 214 Lab 02 j←j+1
Sri Lanka Institute of Information Technology.

Anda mungkin juga menyukai