Nothing Special   »   [go: up one dir, main page]

0% found this document useful (0 votes)
80 views14 pages

PPSC Unit 3 Lesson Notes

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 14

Programming for Problem Solving using C (R20)

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].

Initialization of One-Dimensional Arrays: Arrays can be Initialized at the time of declaration.


Syntax: type array[size]={list of elements separated by comas};
int a[5]={1,2,3,4,5}

here a[0]=1, a[1]=2, a[2]=3, a[3]=4, a[4]=5.


Arrays are zero based i.e., 5 elements in array a[5] are numbered from 0-4. The sub-script specifies
single element of an array. Which is an Integer in square bracket.

Pragati Engineering College (Autonomous) Page 1


Programming for Problem Solving using C (R20)

Example : int marks[5]={90,87,76,69,82};


While initializing the array at the time of declaration, the programmer may omit the size of the array. For
example, int marks[]= {98, 97, 90};

Fig: Index representation of an array of elements

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.

//Example for accessing from the array


void main()
{
int i, marks[10];
for (i=0; i<10; i++)
printf(“%d”,marks[i]);
}

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

Pragati Engineering College (Autonomous) Page 2


Programming for Problem Solving using C (R20)

//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

//Example 3:Sum of elements in the array


#include <stdio.h>
void main()
{
int a[100];
int i, n, sum=0;
printf("\n\nFind sum of all elements of array:\n");
printf("Input the number of elements to be stored in the array :");
scanf("%d",&n);
printf("Input %d elements in the array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&a[i]);
}
for(i=0; i<n; i++)
{
sum += a[i];
}
Pragati Engineering College (Autonomous) Page 3
Programming for Problem Solving using C (R20)

printf("Sum of all elements stored in the array is : %d\n\n", sum);


}

Output: Find sum of all elements of array:


Input the number of elements to be stored in the array :3
Input 3 elements in the array :
element - 0 : 2
element - 1 : 5
element - 2 : 8
Sum of all elements stored in the array is : 15

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.

Declaring 2-D Array:


data_type array_name[row_size][column_size];
For example, if we want to store the marks obtained by three students in three different subjects, we
can declare a two dimensional array as:

int marks[3][3];

Pragati Engineering College (Autonomous) Page 4


Programming for Problem Solving using C (R20)
Fig. Matrix representation of 2D-Array of elements

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: To access an element of a two-dimensional array,


you must specify the index number of both the row and column.
This statement accesses the value of the element in the first row (0) and third column (2) of
the matrix array.

int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };


printf("%d", matrix[0][2]); // Outputs 2

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]);
}
}

Pragati Engineering College (Autonomous) Page 5


Programming for Problem Solving using C (R20)
EXAMPLE 1: ACCESSING OF 2D ARRAY
//Program: Write a program to print the elements of a 2D array.
#include <stdio.h>
int main()
{
int a[3][3] = {12, 34, 56,32,89,23,44,67,99};
int i, j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t", a[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT:
12 34 56
32 89 23
44 67 99

//Program: Write a program find the transpose of a matrix


#include <stdio.h>
int main()
{
int m, n, i, j, matrix[10][10], transpose[10][10];
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of matrix\n");
for (i = 0; i < m; i++)
for(j = 0; j < n; j++)
scanf("%d",&matrix[i][j]);
for (i = 0; i < m; i++)
for( j = 0 ; j < n ; j++ )
transpose[j][i] = matrix[i][j];
printf("Transpose of entered matrix :-\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
printf("%d\t",transpose
[i][j]); printf("\n");
}
}

Pragati Engineering College (Autonomous) Page 6


Programming for Problem Solving using C (R20)
Output:
Enter the number of rows and columns of matrix
2
2
Enter the elements of matrix 1 2
34
Transpose of entered matrix :-
1 3
2 4

Example: Matrix Addition


#include <stdio.h>
int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);

printf("Enter the elements of second matrix\n");


for (c = 0; c < m; c++)
for (d = 0 ; d < n; d++)
scanf("%d ", &second[c][d]);

printf("Sum of entered matrices:-\n");

for (c = 0; c < m; c++)


{
for (d = 0 ; d < n; d++)
{
sum[c][d] = first[c][d] + second[c][d];
printf("%d\t", sum[c][d]);
}
printf("\n");
}
return 0;
}

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]);
}
}

printf("multiply of the matrix=\n");


for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
Pragati Engineering College (Autonomous) Page 8
Programming for Problem Solving using C (R20)
}
}
//for printing result
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}
Output:

enter the number of row=3


enter the number of column=3
enter the first matrix element=
111
222
333
enter the second matrix element=
111
222
333
multiply of the matrix=
666
12 12 12
18 18 18

Multi Dimensional arrays:


A list of items can be represented with one variable name using more than two subscripts and such a
variable is called Multi – dimensional array. C allows for arrays of two or more dimensions. A two-
dimensional (2D) array is an array of arrays. A three-dimensional (3D) array is an array of arrays of
arrays.
The Fig. below describes the 3D Array

Fig: 3D Array Conceptual View

Pragati Engineering College (Autonomous) Page 9


Programming for Problem Solving using C (R20)

Fig: 3D Array Memory Map.

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

Initialization of 3D Array example :


#include<stdio.h>
main()
{
int i, j, k;
int arr[3][3][3]=
{
{ {11, 12, 13}, {14, 15, 16}, {17, 18, 19} },
{ {21, 22, 23}, {24, 25, 26}, {27, 28, 29} },
{ {31, 32, 33}, {34, 35, 36}, {37, 38, 39} }
};
Pragati Engineering College (Autonomous) Page 10
Programming for Problem Solving using C (R20)
printf(":::3D Array Elements:::\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
printf("%d\t",arr [i][j][k]);
printf("\n");
}
printf("\n");
}
}

Output: 3D Array Elements:::


11 12 13
14 15 16
17 18 19

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

//EXAMPLE: SORTING OF ARRAY ELEMENTS


#include <stdio.h>
void main()
{
int arr1[100];
int n, i, j, tmp;
printf("\n\nsort elements of array in ascending order :\n ");
printf("Input the size of array : ");
scanf("%d", &n);
printf("Input %d elements in the array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(arr1[j] <arr1[i])
{
tmp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = tmp;
}
}
}
Pragati Engineering College (Autonomous) Page 12
Programming for Problem Solving using C (R20)
printf("\nElements of array in sorted ascending order:\n");
for(i=0; i<n; i++)
{
printf("%d ", arr1[i]);
}
printf("\n\n");
}

Output: sort elements of array in ascending order :


Input the size of array : 5
element - 0 : 2
element - 1 : 7
element - 2 : 4
element - 3 : 5
element - 4 : 9

Elements of array in sorted ascending order:


2 4 5 7 9

PPSC PREVIOUS QUESTIONS

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.

Pragati Engineering College (Autonomous) Page 13


Programming for Problem Solving using C (R20)

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.

Pragati Engineering College (Autonomous) Page 14

You might also like