PPSC Unit 3 Lesson Notes
PPSC Unit 3 Lesson Notes
PPSC Unit 3 Lesson Notes
UNIT – 3 A R R A Y S
Arrays: An Array is a linear Data structure which is defined as a collection of variables of similar
data type stored consecutively in memory locations.
Depending upon the no of dimensions Arrays are classified as
1.One – dimensional arrays(1-D)
2.Two – dimensional arrays (2-D)
3.Multidimensional arrays (n-D)
One Dimensional Array: It is the simplest form of away for which list of Elements are stored in
continuous memory locations and accessed using only one sub-script.
Example:
int a[10];
here a is an array of 10 integers.
Declaration of an Array:
Syntax: datatype array_name[size];
where type is the name number fixed the data type of array elements,
Array- -name the array variable and size specifies the no of elements in the array.
The size of array is fixed at compile time and cannot be during the execution of a program hence it is
constant integer value.
Example : int a [10];
This declares array which can hold 10 integer elements. The Total size of an Array is 20 bytes.
Example:
int marks[10]; //integer array
The above statement declares a marks variable to be an array containing 10 elements. In C, the
array index (also known as subscript) start from zero. i.e. The first element will be stored in
marks[0], second element in marks[1], and so on. Therefore, the last element, that is the 10th ele-
ment, will be stored in marks[9].
Accessing elements in array: To access all the elements from an array, we must use a loop. That is, we
can access all the ele- ments of an array by varying the value of the subscript into the array. But note that
the subscript must be an integral value or an expression that evaluates to an integral value.
Input values from keyboard: An array can be filled by inputting values from the keyboard. In this
method, a while or for loop is executed to input the value for each element of the array.
For example:
void main()
{
int i, marks[10];
for (i=0; i<10; i++)
scanf(“%d”,&marks[i]);
}
//Example 1: print the one dimensional Array elements
#include<stdio.h>
main ( )
{
int a[5] = {10,20,30,40,50};
int i;
printf ("elements of the array are\n");
for ( i=0; i<5; i++)
printf ("%d\t", a[i]);
}
Output: elements of the array are
10 20 30 40 50
//Example Program 2 : Write a C Program to read and display N numbers using an array.
#include <stdio.h>
int main()
{
int i, n, arr[10];
printf("\n Enter the number of elements in the array : "); scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n arr[%d] = ", i);
scanf("%d",&arr[i]);
}
printf("\n The array elements are ");
for(i=0;i<n;i++)
printf("\t %d", arr[i]);
return 0;
}
OUTPUT:
Enter the number of elements in the array : 5
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
The array elements are 1 2 3 4 5
Applications of Arrays
1) Array stores data elements of the same data type.
2) Maintains multiple variable names using a single name. Arrays help to maintain large data under
a single variable name. This avoid the confusion of using multiple variables.
3) Arrays can be used for sorting data elements. Different sorting techniques like Bubble sort,
Insertion sort, Selection sort etc use arrays to store and sort elements easily.
4) Arrays can be used for performing matrix operations. Many databases, small and large, consist of
one-dimensional and two-dimensional arrays whose elements are records.
5) Arrays can be used for CPU scheduling.
6) Lastly, arrays are also used to implement other data structures like Stacks, Queues, Heaps, Hash
tables etc.
Two-Dimensional Arrays: Two dimensional array is a list of one dimensional arrays. The 2D array is
organized as matrices which can be represented as the collection of rows and columns.
int marks[3][3];
Initializing Two- Dimensional Arrays: Like the one-dimensional arrays, two-dimensional arrays
may be initialized by following theirdeclaration with a list of initial values enclosed in curly braces.
int table[2] [3] = {0,0,0,1,1,1};
This initializes the elements of first row to zero and the second row to one.
• This initialization can also be done row by row. The above statement can be equivalently
written as: int table[2][3] = { {0,0,0}, {1,1,1} };
• Commas are required after each curly brace that closes of a row, except in case of last row.
• If the values are missing in an initializer, they are automatically set to zero.
• Ex: int table [2] [3] = {{1,1}, \\ 1 1 0 {2}\\ 2 0 0 };
• When all the elements are to be initialized to zero, the following short-cut method may be
used: int m[3][5] = { {0}, {0}, {0}};
• The first element of each row is explicitly initialized to zero while the other elements areautomatically
initialized to zero.
• The following statement will also achieve the same result
int m[3][5] = { 0 };
Accessing the Elements of Two-dimensional Arrays with loops: Since the 2D array contains two
subscripts, we will use two for loops to scan the elements. The first for loop will scan each row in the 2D
array and the second for loop will scan individual columns for every row in the array.
Example
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
int i, j;
for (i = 0; i < 2; i++)
{
for (j = 0; j < 3; j++)
{
printf("%d\n", matrix[i][j]);
}
}
Output:
Enter the number of rows and columns of matrix
2
2
Enter the elements of first matrix
Pragati Engineering College (Autonomous) Page 7
Programming for Problem Solving using C (R20)
1 2
3 4
Enter the elements of second matrix
5 6
2 1
Sum of entered matrices
6 8
5 5
Example : Matrix multiplication :
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
system("cls");
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
Initializing a 3D Array in C:
Like any other variable or array, a 3D array can be initialized at the time of compilation. By default, inC,
an uninitialized 3D array contains “garbage” values, not valid for the intended use.
Syntax:
datatype arrayname [size1] [size2] ------ [sizen];
eg: for 3 – dimensional
array:int a[3] [3] [3]
No of elements = 3*3*3 = 27 elements
//Example program
#include<stdio.h>
main ( )
{
int a[2][2] [2] = {1,2,3,4,5,6,7,8};
int i,j,k;
printf (“elements of the array are”);
for ( i=0; i<2; i++)
{
for (j=0;j<2; j++)
{
for (k=0;k<2; k++)
{
printf(“%d ”, a[i] [j] [k]);
}
}
}
}
Output : Elements of the array are :
1 2 3 4 5 6 7 8
21 22 23
24 25 26
27 28 29
31 32 33
34 35 36
37 38 39
PROGRAMMING EXAMPLE:
Program: Write a C Program to print the maximum and minimum element in the array.
#include <stdio.h>
int main()
{
int i, n, a[20], max, min;
printf("Enter the number of elements in the array : ");
scanf("%d", &n);
printf("Enter the elements\n");
for(i=0;i<n;i++)
{
printf("a[%d] = ",i);
scanf("%d",&a[i]);
}
max = min = a[0];
for(i=1;i<n;i++)
Pragati Engineering College (Autonomous) Page 11
Programming for Problem Solving using C (R20)
{
if(a[i]<min)
min = a[i];
if(a[i]>max)
max = a[i];
}
printf("\n The smallest element is : %d\n", min);
printf("\n The largest element is : %d\n", max);
return 0;
}
OUTPUT: Enter the number of elements in the array : 5
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
The smallest element is : 1
The largest element is : 5
1. What is an array? How a single dimension and two dimension arrays are declared and initialized?
2. Explain the declaration and initialization of one dimensional and two dimensional arrays with an
example.
3. Write a C program to perform matrix multiplication.
4. Write a C program to addition of two matrices.
5. Write a C program to read N integers into an array A and to find (i)sum of odd numbers (ii) sum of
even numbers.
6. Write a C program to find the largest element and smallest element in an array.
ASSIGNMENT - III
Blooms
Q.No. Question Taxonomy COs
Level
SET 1
What is an array? How a single dimension and two dimension arrays
1 K2 CO3
are declared and initialized?
2 Write a C program to addition of two matrices. K3 CO3
SET 2
1 Explain the declaration and initialization of one dimensional and two K2
CO3
dimensional arrays with an example.
2 Write a C program to perform matrix multiplication. K3 CO3
SET 3
1 Explain the declaration and initialization of one dimensional and two K2 CO3
dimensional arrays with an example.
2 Write a C program to find the largest element and smallest element in K3 CO3
an array.