Introduction To Computer Programming Programming
Introduction To Computer Programming Programming
Programming
An array is used to represent a list of numbers ,
or a list of names.
The size and type of arrays cannot be
changed after its declaration. For Example : If
you want to store marks of 100 students you can
create an array for it.
float marks[100]; 3
Element of an array
0 1 2 3 4 Element
Array of 5 index
elements
Introducing A r r a y s
Array is a data structure that represents a collection of
the same types of data.
int num[10];num [0]
Num reference num[1]
num[2]
num [3]
An Array of 10 Elements
num[4] of type int.
num[5]
num[6]
num[7]
num[8]
num[9]
Contd…
income[10]
Few Keypoints:
• Arrays have 0 as the first index not 1. In this example,
mark[0].
• If the size of an array is n, to access the last element, (n-1)
index is used. In this example, mark[4].
• Suppose the starting address of mark[0] is 2120d. Then, the
next address, mark[1], will be 2124d, address of mark[2] will
be 2128d and so on. Its because the size of a float is 4 bytes.
Example of Arrays:
For Example :
1. List of employees in an organization.
2. Test scores of a class of students.
3. List of customers and their telephone numbers.
4. List of students in the college.
For Example, to represent 100 students in college , can
be written as
student [100]
Here student is a array name and [100] is called index
or subscript.
9
Types of Arrays
Based on their dimensions arrays can be
classified ino three types.
Types of Array :
1. One-dimensional arrays
2. Two-dimensional arrays
3. Multidimensional arrays
10
One-dimensional Arrays.
11
One-dimensional Arrays.
and the computer store these numbers as shown below :
number [0]
number [1]
number [2]
number [3]
number [4]
The values can be assigned to the array as follows :
number [0] = 35;
number [1] = 40;
number [2] = 20;
number [3] = 57;
number [4] = 19;
12
DECLARATION OF ONE-DIMENSIONAL ARRAYS :
13
Example
For example :
14
Important thing to remember when working with Carrays:
int testArray[10];
0 1 2 3 4
myIntArray … … … … …
managed heap
(dynamic memory)
Accessing Array Elements
Scanf(“%d”,&array[5]);
-------------------------------------------------
-------------------------------------------------
Printing Arrays
printf(“a[5]=%d”,array[5]);
-------------------------------------------------
-------------------------------------------------
INITIALIZATION OF ONE-DIMENSIONAL ARRAYS :
21
The type specifies the type of the element that will be
contained in the array, such as int, float, or char and the size
indicates the maximum number of elements that can be stored
inside the array.
Now as we declare a array
int number[5];
Then the computer reserves five storage locations as
the size o the array is 5 as show below.
number[0] number[0] 35
number[1] number[1] 20
number[2] number[2] 40
number[3] number[3] 57
number[4] number[4] 19
Initialization of onedimensional array:
At compile time
At run time
Compile Time Initialization:
The general form of initialization of array is:
type array-name[size] ={list of values};
The values in the list are separated by commas.
For example:
int number[3] ={0,5,4};
Example :
int number[3] = {4,5,9};
Here array number of size 3 and will assign 4 to first
element(number[0]), 5 is assign with second
element(number[1]) and 9 is assign with third
element(number[2]).
25
Compile time initialization
Example :
int number[ ] = {1,2,3,4};
26
Run time initialization:
An array can also be explicitly initialized at run time.
For example consider the following segment of a c program.
for(i=0;i<10;i++)
{
scanf(“%d”, &x[i]);
}
In the run time initialization of the arrays looping
statements are almost compulsory.
0 1 2 3 4 0 1 2 3 4
0 1 2
0 0
0 1 2 3
1 1
1 4 5 6
2 2 7
2 7 8 9
3 3
3 10 11 12
4 4
For example:
int table[2][3];
33
DECLARATION OF TWO-DIMENSIONAL ARRAYS :
type array-name[row_size][column_size];
34
INITIALIZATION OF TWO-DIMENSIONAL ARRAYS :
35
INITIALIZATION OF TWO-DIMENSIONAL ARRAYS :
Example :
int table[ ][3] = {{0,0,0}, {1,1,1}};
If the values are missing in an initializer, they are
automatically set to zero.
Example :
int table[2][3] = {1,1,2};
Here first row initialize to 1,1 and 2, and second row
initialize to 0,0 and 0 automatically.
36
Memory Layout of Two-dimensional array :
37
multi-dimensional Arrays
38
multi-dimensional Arrays
float table[5][4][5][3];
39
Declaration of Multidimensional Array
A multidimensional array is declared using the
following syntax
Syntax:
data_type array_name[d1][d2][d3][d4]....[dn];
Above statement will declare an array on N
dimensions of name array_name, where each element of
array is of type data_type.
The maximum number of elements that can be
stored in a multi dimensional array array_name is size1 X
size2 X size3....sizeN.
For example:
char cube[50][60][30];
Dynamic Arrays
42
Some Examples of Array
Ex: Write a program to print first 10 number.
::
#include<stdio.h>
void main() // pr [ 1 0 ] i s a r r a y o f 10
{
Int i, r[10];
elements.
// assign value to array
for (i = 1; i < =10; i++)
{
r[i]=i;
printf("%d",r[i]);
printf(”\n”);
}
}
:: output::
1
2
3
4
5
6
7
8
9
10
Write a program to read and display 3x3matrix.
#include<stdio.h>
v oi d main()
{
int i,j,a[3][3];
p r i n t f ( “ E n t e r t h e e l e m e n t s of 3 x 3 matrix”);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
p r i n t f ( “ a [ % d ] [ % d ] = s c a n f (“% d ” , a [ i ] [ j ] ) ;
{
p r i n t f (“ \ n \ t \ t ”);
for(j=0;j<3;j++)
p r i n t f (“%d \ t ” , a [ i ] [ j ] ) ;
}}
: : o u t p u t ::
Element of Amatrix
a[1][1]:1
a[1][2]:4
a[2][1]:6
a[2][2]:3
Element of Bmatrix
b[1][1]:1
b[1][2]:7
b[2][1]:3
b[2][2]:9
Addition of a matrix is
2 11
9 12
THANK YOU