SPL Lec 7 (Array) - 1
SPL Lec 7 (Array) - 1
Array is a Data Structure which can store a fixed-size sequential collection of elements of the
same data type. An array is used to store a collection of data. A specific element in an array is
accessed by an index.
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.
For example, to declare a 10-element array called balance of type double, use this statement.
Declaring Arrays
You can initialize array in C either one by one or using a single statement as follows:
The number of values between braces { } can not be larger than the number of elements that
we declare for the array between square brackets [ ]. If you omit the size of the array, an array
just big enough to hold the initialization is created. Therefore, if you write:
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.
The above statement will take 10th element from the array and assign the value to salary
variable.
Copying Entire Arrays
We cannot directly copy one array to another, even if they have the same length and share the
same data type.
{
a2[m] = a1[m];
}//end for
Declaring Array Example
Example:
Two-Dimensional Array
A two-dimensional array can be think as a table which will have x number of rows and y
number of columns. A 2-dimensional array a, which contains three rows and four columns can
be shown as below: a[3][4]
Thus, every element in array a is identified by an element name of the form a[ i ][ j ]. where a
is the name of the array, and i and j are the subscripts that uniquely identify each element in a.
Initializing Two-Dimensional Array
Multidimensional arrays may be initialized by specifying bracketed values for each row.
Following is an array with 3 rows and each row has 4 columns.
The nested braces, which indicate the row, are optional. The following initialization is
equivalent to previous example:
Accessing Two-Dimensional Array Element
An element in 2-dimensional array is accessed by using the row index and column index of the
array. For example
The above statement will take 4th element from the 3rd row of the array. You can verify it in
the above diagram.
Example
Let us check below program where we have used nested loop to handle a two dimensional
array:
Memory Address Calculation
X= B+W*[ I*C+J]
X = B + W * [I + J * R]
Memory Address Calculation
An array S[10][15] is stored in the memory with each element requiring 4 bytes of storage. If
the base address of S is 1000, determine the location of S[8][9] when the array is S stored
by
(i) Row major (ii) Column major.
Solution:
✔ Let us assume that the Base index number is [0][0].
✔ Number of Rows = R = 10
✔ Number of Columns = C = 15
✔ Size of data = W = 4
• Base address = B = S[0][0] = 1000
• Location of S[8][9] = X
Memory Address Calculation
(i) When S is stored by Row
Major
X = B + W * [ 8 * C + 9]
= 1000 + 4 * [8 * 15 + 9]
= 1000 + 4 * [120 + 9]
= 1000 + 4 * 129
= 1516
(ii) When X S is
= stored
B + W *by
[8Column
+9*
Major R]
= 1000 + 4 * [8 + 9 *
10]
= 1000 + 4 * [8 + 90]
= 1000 + 4 * 98
= 1000 + 392
Lab Task:
Write a C program to sort an array
#include<stdio.h>
int main( )
{
int avg, sum = 0 ;
int i ;
int marks[30] ; /* array declaration */
for ( i = 0 ; i <= 29 ; i++ )
{
printf ( "Enter marks " ) ;
scanf ( "%d", &marks[i] ) ; /* store data in array */
}
for ( i = 0 ; i <= 29 ; i++ )
sum = sum + marks[i] ; /* read data from an array*/
Lab Task
1. C program take size 10 array and assign 10 values and print all 10 values using array.
2. C program to take 10 number from user by using loop and store it in array.
3. Print the number stored in array by using loop.
4. C program to find the sum of marks of n students given by user using Arrays
Hints-
1. Use for loop.
2. use array