Professional Documents
Culture Documents
Usp 7
Usp 7
(CSE 3041)
Arrays
Chapter-7
SOA
Deemded to be University
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 1 / 43
Motivation
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 2 / 43
Defining Arrays
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 3 / 43
Declaring Arrays
To set up an array in memory, we must declare both the name of the ar-
ray and the number of cells associated with it.
Array declaration
double x[8]
The number 8 tells how many elements of the type double will
be in our array.
and [8] tell complier that we are dealing with arrays.
instructs the compiler to associate eight memory cells with the
name x;
these memory cells will be adjacent to each other in memory.
Each element of array x may contain a single type double value,
a total of eight such numbers may be stored and referenced
using the array name x.
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 4 / 43
Referencing arrays
How each individual elements in the array can be referenced ?
Done with subscript: the number in [ ] following the array name. It
specifies the element’s position in the array.
All the array elements are numbered, starting with 0.
Subscripted variable is a variable followed [subscript], designating an
array element.
Subscripted variable
x[0] use to reference 0th element of x
x[1] use to reference 1th element of x
x[7] use to reference the last element of x
Array subscript:
a value or expression enclosed in [] after the array name, specifying
which array element to access.
Range of its value :0 to (the number of memory cells in the array-1).
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 5 / 43
Figure: Statements that manipulate array x
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 6 / 43
Multiple array Declarations
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 7 / 43
Array subscript
Array subscript:
are used to differentiate between the individual array elements
are used to specify which array element is to be manipulated.
can be any expression of type int as an array subscript.
must lie between 0 and one less than the declared size of the array.
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 8 / 43
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 9 / 43
A simple C program using array
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 10 / 43
Using for Loops for Sequential Access
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 11 / 43
Statistical Computations Using Arrays
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 12 / 43
Points to ponder
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 13 / 43
Array initialization
Till the array elements are not given any specific values, they are sup-
posed to contain garbage values.
If the array is initialised where it is declared, mentioning the dimension
of the array is optional.
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 14 / 43
Array elements in memory
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 15 / 43
Bounds Checking
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 16 / 43
Using Array Elements as Function Arguments
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 17 / 43
Pointers and Arrays
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 18 / 43
Pointer Arithmetic: DO NOT ATTEMPT
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 19 / 43
Important correlation
Correlation
Array elements are always stored in contiguous memory locations.
A pointer when incremented always points to an immediately next lo-
cation of its type.
Illustration
Write a program that prints out the memory locations in which the
elements of this array are stored.
Two programs to show ways in which we can access the elements of
an array.
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 20 / 43
Which method is better?
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 21 / 43
Passing an Entire Array to a Function
The address of the zero th element can also be passed by just passing
the name of the array.
This address is also called the base address of the array.
Practice problem
WAP to initialize all elements of an array with a specific value.
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 22 / 43
The Real Thing
Figure: All same. Can be used to get i th data item stored in the array num.
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 23 / 43
Arrays as Input Arguments
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 24 / 43
Partially filled arrays
Array can be used for processing shorter lists as well, provided that the
program keeps track of how many array elements are actually in use.
Use sentinel controlled loops to track the number of elements in use.
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 25 / 43
Stacks
A stack is a data structure in which only the top element can be ac-
cessed.
Operations on Stack:
I Pop: Removing a value from a stack.
I Push: storing an item in a stack.
WAP to implement stack with functions for push and pop operations.
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 26 / 43
Application: Linear Search algorithm
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 27 / 43
Application : Selection sort
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 28 / 43
Parallel arrays
Parallel arrays : Two or more arrays with the same number of elements
used for storing related information about a collection of data objects.
If there are n-elements, these parallel arrays contain data for n-objects
of the same kind.
Further, all array elements at subscript i contain data for the ith object
in this group of n-objects.
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 29 / 43
Enumerated Types
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 30 / 43
Enumerated types
Syntax
enum marital status
{
unmarried, married, divorced, widowed
};
enum mar status person1,person2;
First part: declares the data type and specifies its possible values.
These values are called enumerators or Enumeration constants
Second part: declares variables of enumerated data type.
person1=married ; person2=widowed;
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 31 / 43
Enumeration types
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 32 / 43
Renaming data types with typedef
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 33 / 43
Figure: Declaration
Figure: Relational, assignment, and even arithmetic operators can be used with
enumerated types
Figure: Looping
Figure: Selection
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 34 / 43
Two Dimensional Arrays
Multidimensional arrays are arrays with two or more dimensions.
The two dimensional array is also called a matrix.
Basic idea :)
A two-dimensional array is a collection of a number of 1-D arrays
placed one below the other.
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 35 / 43
Initializing 2-D array
Initialization
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 36 / 43
Array arrangement in memory
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 37 / 43
Pointers and 2-Dimensional Arrays
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 38 / 43
Pointer to an Array
We have seen :
int *ip;
float *fp;
char *cp;
A pointer to an array? How to build and use it.
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 39 / 43
Passing 2-D Array to a Function
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 40 / 43
Array of Pointers
We have
an array of ints or
an array of floats or
an char of floats
Lets learn :
Similarly there can be an array of pointers.
As known a pointer variable always contains an address, therefore
An array of pointers would be nothing but a collection of addresses.
(Ashish Kumar Gupta, Ph.D.) Chapter-7, Arrays, C programming November 30, 2021 43 / 43