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

Arrays, Records and Pointers

Uploaded by

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

Arrays, Records and Pointers

Uploaded by

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

Arrays, Records and

Pointers
Csc-391
Data Structures and Algorithms
2

Introductio
n data structure. Is used to store similar types of data. An array is a
Linear
finite collection of similar elements stored in adjacent memory locations.

Decleration of the Arrays: (In C)


the array name, the element type and the array size.
Examples:
int a[20], b[3],c[7]; // one-dimensional arrays
float f[5], c[2];
char m[4], n[20];

Initialization of an array is the process of assigning initial values.


Examples:
float, b[3]={2.0, 5.5, 3.14};
char name[4]= {‘E’,’m’,’r’,’e’};
int c[10]={0};
© SMT, Faculty, CSE, IUBAT
Data Structures and Algorithms
3

Two- dimensional arrays

©SMT, Faculty, CSE, IUBAT


Data Structures and Algorithms
4

Two- dimensional arrays

©SMT, Faculty, CSE, IUBAT


Data Structures and Algorithms
5

Two- dimensional arrays

©SMT, Faculty, CSE, IUBAT


Data Structures and Algorithms
6

Remarks
Pointer arrays and pointers:
Array of pointers is called pointer array. Details on pointers and arrays is discussed
in Array, func, point
document
Group Group Group Group
Jagged arrays: 1 2 3 4
Arrays whose rows or Evans Conrad Davis Baker * * * * 0 0 0 0 0
columns begins with Harris Felt Segal Cooper * * * * * * * * *
different numbers * * 0 0 0 0 0 0 0
Lewis Glass Ford
of data elements and
Shaw Hill Gray * * * * * * 0 0 0
ends with unused space,
King Jones
are called
jagged array. Penn Reed
Silver
Troy
Dynamic arrays: Wagner
int *my_array;
my_array = new int[10]; // array size is defined during run-time
©SMT, Faculty, CSE, IUBAT
Data Structures and Algorithms
7
#include<iostream.h>
int main ()
{
int i,n; int * p;
Sample code for Dynamic
cout << "How many numbers would you like to type? "; Array declaration
cin >> i;
p= new int[i]; // it takes memory at run-time from Heap
if(p == NULL)
cout << "Error: memory could not be allocated";
else
{
for(n=0; n<i; n++)
{
cout << "Enter number: ";
cin >> p[n];
}
int*k=p; // to hold the base address of dynamic array
cout << "You have entered: \n";
for(n=0; n<i; n++) cout << *k<< ", "; k++;
cout<<"\n";
delete[] p; // it release the memory to send it back to Heap
}
return 0;
}
©SMT, Faculty, CSE, IUBAT
Data Structures and Algorithms
8

Records and structure (struct in C)


Records may contain different types of information. Structures are used to store
these records. A structure is a collection of logically related variables under a
single unit/name. (In c++ we can create class to store records)
Example:
struct Rectangle // this is type/name for structure
{ float Length;
float width;
float area;
};

A structure is
usually
declared before
main( )
function.

©SMT, Faculty, CSE, IUBAT


Data Structures and Algorithms
9

Travers, Insert, Delete on Arrays


a) Traversing in Linear Array

b) inserting in Linear Array

©SMT, Faculty, CSE, IUBAT


Data Structures and Algorithms
10

Travers, Insert, Delete on Arrays


c) Deleting from Linear Array

©SMT, Faculty, CSE, IUBAT


Data Structures and Algorithms
11

Searching
Linear Search
The linear search compares each element of the array with the search key
until the search key is found. To determine that a value is not in the
array, the program must compare the search key to every element in the array. It
is also called “Sequential Search” because it traverses the data sequentially
to locate the element.

©SMT, Faculty, CSE, IUBAT


Data Structures and Algorithms
12

Searching
Binary Search
It is useful for the large sorted arrays. The binary search algorithm can
only be used with sorted array and eliminates one half of the elements in
the array being searched after each comparison.

©SMT, Faculty, CSE, IUBAT


Data Structures and Algorithms
13

Searching
Binary Search

©SMT, Faculty, CSE, IUBAT


Data Structures and Algorithms
14

Sorting
Bubble Sort
The technique we use is called “Bubble Sort” because the bigger value
gradually
bubbles their way up to the top of array like air bubble rising
in water, while the small values sink to the bottom of array.

This technique is to make several passes through the array. On


each pass,
successive pairs of elements are compared. If a pair is in
increasing order (or the values are identical), we leave the values as
they are. If a pair is in decreasing order, their values are swapped in
the array.

©SMT, Faculty, CSE, IUBAT


Data Structures and Algorithms
15

Sorting
Bubble Sort

©SMT, Faculty, CSE, IUBAT


Data Structures and Algorithms
16

Sorting
Bubble Sort

©SMT, Faculty, CSE, IUBAT

You might also like