0% found this document useful (0 votes)
3 views

data strucures-arrays

Chapter 6 discusses data structures, which are organized groups of data elements that improve efficiency compared to simple data types. It classifies data structures into linear, non-linear, homogeneous, non-homogeneous, dynamic, and static types, and explains arrays as a specific example of data structures that store multiple values of the same type. The chapter also covers array declaration, initialization, accessing elements, and introduces bubble sort as a sorting technique.

Uploaded by

felix
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

data strucures-arrays

Chapter 6 discusses data structures, which are organized groups of data elements that improve efficiency compared to simple data types. It classifies data structures into linear, non-linear, homogeneous, non-homogeneous, dynamic, and static types, and explains arrays as a specific example of data structures that store multiple values of the same type. The chapter also covers array declaration, initialization, accessing elements, and introduces bubble sort as a sorting technique.

Uploaded by

felix
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CHAPTER 6

DATA STRUCTURES
These refer to groups of data elements that are organized in a single unit so that
they can be used more efficiently as compared to the simple data types such as
integers and strings. An example of a data structure is the array. Ordinary variables
store one value at a time while an array will store more than one value at a time in a
single variable name.

Data structures are important for grouping sets of similar data together and passing
them as one. For example, if you have a method that prints a set of data but you don't
know when writing the procedure how large that set is going to be, you could use an
array to pass the data to that method and loop through it.
Data structures can be classified using various criteria.
a) Linear
In linear data structures, values are arranged in linear fashion. A linear data structure
traverses the data elements sequentially. The elements in the structure are adjacent to
one another other and every element has exactly two neighbour elements to which it is
connected. Arrays, linked lists, stacks and queues are examples of linear data
structures.
b) Non-Linear
The data values in this structure are not arranged in order but every data item is
attached to several other data items in a way that is specific for reflecting
relationships. Tree, graph, table and sets are examples of non-linear data structures.
c) Homogenous
In this type of data structures, values of the same types of data are stored, as in an
array.
d) Non-homogenous
In this type of data structures, data values of different types are grouped, as in
structures and classes.
e) Dynamic
In dynamic data structures such as references and pointers, size and memory locations
can be changed during program execution. These data structures can grow and shrink
during execution.
f) Static
With a static data structure, the size of the structure is fixed. Static data structures
such as arrays are very good for storing a well-defined number of data items.

56
ARRAYS
An array is a named list of elements, all with the same data type. It is better defined as
a consecutive group of memory locations all of which have the same name and the
same data type. Arrays store a fixed-size sequential collection of elements of the same
type.

Instead of declaring individual variables, such as number0, number1, ..., and


number99, you declare one array variable such as numbers and use numbers[0],
numbers[1], and ..., numbers[99] to represent individual variables. A specific element
in an array is accessed by an index.

All arrays consist of contiguous memory locations. The lowest address corresponds to
the first element and the highest address to the last element.

DECLARING ARRAYS
To declare an array in C, a programmer specifies the type of the elements and the
number of elements required by an array as follows:

type arrayName [ arraySize ];

This is called a single-dimensional array. The arraySize must be an integer constant


greater than zero and type can be any valid C data type. For example, to declare a 10-
element array called balance of type double, use this statement:

double balance[10];

57
Now balance is a variable array which is sufficient to hold up to 10 double numbers.

INITIALIZING ARRAYS
You can initialize an array in C either one by one or using a single statement as
follows:

double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};

The number of values between braces { } can not be larger than the number of
elements that we declare for the array between square brackets [ ]. Following is an
example to assign a single element of the array:

If you omit the size of the array, an array just big enough to hold the initialization is
created. Therefore, if you write:

double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};

You will create exactly the same array as you did in the previous example.

balance[4] = 50.0;

The above statement assigns element number 5th in the array a value of 50.0. Array
with 4th index will be 5th ie. last element because all arrays have 0 as the index of
their first element which is also called base index. Following is the pictorial
representation of the same array we discussed above:

ACCESSING ARRAY ELEMENTS


An element is accessed by indexing the array name. This is done by placing the index
of the element within square brackets after the name of the array. For example:

double salary = balance[9];

The above statement will take 10th element from the array and assign the value to
salary variable. Following is an example which will use all the above mentioned three
concepts viz. declaration, assignment and accessing arrays:

#include <stdio.h>

int main ()
{
int n[ 10 ]; /* n is an array of 10 integers */
int i,j;

58
/* initialize elements of array n to 0 */
for ( i = 0; i < 10; i++ )
{
n[ i ] = i + 100; /* set element at location i to i
+ 100 */
}

/* output each array element's value */


for (j = 0; j < 10; j++ )
{
printf("Element[%d] = %d\n", j, n[j] );
}

return 0;
}

When the above code is compiled and executed, it produces the following result:

Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109

SORT TECHNIQUES

Bubble Sort
In the bubble sort, as elements are sorted they gradually "bubble" (or rise) to their
proper location in the array, like bubbles rising in a glass of soda. The bubble sort
repeatedly compares adjacent elements of an array. The first and second elements
are compared and swapped if out of order. Then the second and third elements are
compared and swapped if out of order. This sorting process continues until the last
two elements of the array are compared and swapped if out of order.

When this first pass through the array is complete, the bubble sort returns to elements
one and two and starts the process all over again.

The table below follows an array of numbers before, during, and after a bubble sort

59

You might also like