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

Arrays PDF

Uploaded by

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

Arrays PDF

Uploaded by

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

Arrays in C

Dr. MD Asrar Ahmed


Arrays – Basic Concept

• Let we have to read and print 10 integers.


• What we can do? Declare 10 variables, each with a different name.
• To read 10 different integers, we need 10 𝑠𝑐𝑎𝑛𝑓() and to print them we
need 10 𝑝𝑟𝑖𝑛𝑡𝑓() function calls.
• This is simple, however, when we need 10,000 or more variables, this
leads to complex code.
• To process large amount of data, we need a data structure called as
“Array”.
• An Array is a collection of elements of same data type. It is a sequenced
collection, its elements can be termed as first, second, etc.
Arrays – Basic Concepts

• When we put 10 integers into an array, arr, the index of first element
in the array will be 0, second element will be 1, and so on, the last
element has index 9.

• Array elements can be read and printed using loops, hence reducing
the size of code.

• To access 2nd element of an array, we use a subscript as 1 in square


brackets, arr[1]. First element is accessed using 𝑎𝑟𝑟[0] and last
element using 𝑎𝑟𝑟[9] in an array of size 10 elements.
Using Arrays in C

• C provides two different types of


arrays: fixed length and variable
length.

• In a fixed-length array, size of the


array is known when the program is
written.

• In variable-length array, size of the


array is unknown until the program
is run.
Arrays – Declaration and Definition

• An array must be declared and defined before it can be used.


• Array declaration and definition tell the compiler:
– Name of the array
– Type of each element
– Size or number of elements in the array

• In a fixed-length array, size of array is a constant and must exist at


compile time. type arrayName[size]
• A variable-length array declaration:
float temperatures[noOfDays];
The noOfDays must have been initialized before.
Arrays – Accessing Array Elements

• C uses an index in an array to access an element.


• The index must be an integral value, or an expression evaluating to
integral value, or simply a numeric constant.
• To access first element of an array, we use: 𝑎𝑟𝑟 0 .
• To print all elements of an array:
𝑓𝑜𝑟 𝑖 = 0; 𝑖 < 𝑠𝑖𝑧𝑒; 𝑖 + + 𝑝𝑟𝑖𝑛𝑡𝑓("%d",arr[i]);
• The array’s name is a symbolic reference for address to the first byte of
the array. Wherever we use array name, we are actually referring to first
byte of the array.
• The index represents an offset from beginning of the array to the
element being referred.
Arrays – Accessing Array Elements

• The array’s name is a symbolic reference for address to the first byte
of the array. Wherever we use array name, we are actually referring
to first byte of the array.
• The index represents an offset from beginning of the array to the
element being referred.
• Using starting byte and an index, C can also access an element at a
given index using following formula:
element_address=array_address + (sizeof(element)*index))
Let starting address be 10,000, stores integers, and index be 3, then
Element_address=10,000 + 4*3 = 10,012.
Arrays – Storing Values in an Array

• Declaration and definition only allocate space for elements in array.


• No values are stored.
• To store values, we must either initialize, read values from keyboard
or assign values to each individual element.
• Fixed length arrays can be initialized when they are defined, but
variable length arrays cannot be initialized when they are defined.
• Values to be initialized must be in braces { , }, it can be one value or
a comma separated list of values.
• It is an error to specify more values than the size of an array.
Arrays – Storing Values in an Array
Arrays – Storing Values in an Array

• Another way to store values into arrays:


for(i=0;i<size;i++)
scanf(“%d”, &arr[i]);
• Values can also be assigned to individual elements as: arr[4]=23;
• If values have a pattern we can use for loop to assign values:
for(i=0;i<size;i++) arr[i]=i*2;
• An array cannot be copied to another array by just using assignment
operator =.
• To print array contents: for(i=0;i<size;i++) printf(“%d, ”, arr[i]);
Inter-function Communication

• To process arrays in a large program, we need to pass them to


functions.
• We can pass arrays in two ways: pass individual elements or pass
the whole array.
• Passing individual elements:
Inter-function Communication

• Passing individual element by address:


Inter-function Communication

Passing whole array as a parameter: (Fixed Length Arrays)


• To pass the whole array, we use array name as the actual parameter
in function call.
• In called function, we declare corresponding formal parameter is an
array.
• We need not specify number of elements in a fixed size array.
• Functions taking whole arrays as parameters can be declared as
follows:
Inter-function Communication

Passing whole array as a parameter: (Variable Length Arrays)


• When the called function receives a variable-length array, we must
declare and define it as a variable length.
• We must use a variable in function definition to denote size of the
array.
• An array can also be passed as constant so that receiving function
Void fun1(int size, int arr[]); //function declaration
does not change it.
• Sorting Void fun1(int size, int arr[size]) //function definition
{
• Searching ---
}
Two-Dimensional Arrays

• The arrays discussed so far are known as One-dimensional arrays are elements are
arranged linearly in only one direction.
• Many applications require data be arranged in more than one direction, such as a
table, which is a collection of rows and columns.
• This is represented as an array that consists of rows and columns, known as Two-
dimensional array.
• It can be viewed as an array of arrays.
Two-dimensional Arrays

• Two-dimensional arrays must be declared before being used.


• Declaration tells compiler name of the array, the type of each
element, and size of each dimension.
• 2D arrays can be either fixed size or variable size.
• Fixed size 2D array: int table[4][5];
• First dimension specifies rows, and second specifies columns in each
row.
• Variable length 2D array: in table1[rows][cols];
Two-dimensional Arrays

• Initialization: a comma separated list of elements

• Initialization with nested braces:


Two-dimensional Arrays

• When a 1D array is fully initialized with a set of values, we do not


require the size to be specified explicitly, i.e., int a[ ]={12,34,44,5};
• In a 2D array, we can omit only first dimension, second dimension
(or in case of multi-dimensional, all except first) needs to be specified
explicitly.
• The whole 2D array can be set to 0:
int table1[4][4]={0};
• Inputting values in 2D array:
Passing Two-dimensional Arrays to Functions

• We can pass: individual elements, a row, or a the complete array.


• Passing a 2D array row by row:
Passing Two-dimensional Arrays to Functions

• Passing a 2D array as a whole:


• To pass 2D array,
1. The function must be called by
passing only name of 2D array.
2. In function definition, formal
parameter must be a 2D array,
with size of second dimension
specified.

You might also like