Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C Programming PowerPoint Slides, Slides of C programming

Very good and Detailed C Programming Notes

Typology: Slides

2017/2018

Uploaded on 05/16/2018

abhishek-rana-4
abhishek-rana-4 🇮🇳

5

(1)

3 documents

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
© Oxford University Press 2012. All rights reserved.
CHAPTER 11
ARRAYS
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download C Programming PowerPoint Slides and more Slides C programming in PDF only on Docsity!

© Oxford University Press 2012. All rights reserved.

CHAPTER 11

ARRAYS

© Oxford University Press 2012. All rights reserved.

INTRODUCTION

  • An array is a collection of similar data elements.
  • These data elements have the same data type.
  • The elements of the array are stored in consecutive memory locations and are referenced by an

index (also known as the subscript).

  • Declaring an array means specifying three things:

The data type- what kind of values it can store ex, int, char, float

Name- to identify the array

The size- the maximum number of values that the array can hold

  • Arrays are declared using the following syntax.

type name[size];

1 st element 2 nd element 3 rd element 4 th element 5 th element 6 th element 7 th element 8 th element 9 th element 10 th element marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7] marks[8] marks[9]

© Oxford University Press 2012. All rights reserved.

STORING VALES IN ARRAYS Store values in the array Initialize the elements Input values for the elements Assign values to the elements

Initialization of Arrays

Arrays are initialized by writing,

type array_name[size]={list of values};

int marks[5]={90, 82, 78, 95, 88};

int i, marks[10]; for(i=0;i<10;i++) scanf(“%d”, &marks[i]);

Assigning Values

int i, arr1[10], arr2[10]; for(i=0;i<10;i++) arr2[i] = arr1[i];

Inputting Values

CALCULATING THE LENGTH OF THE ARRAY

Length = upper_bound – lower_bound + 1

Where, upper_bound is the index of the last element

and lower_bound is the index of the first element in the array

Marks[0] marks[1] marks[2] marks[3] marks[4 ] marks[5] marks[6 marks[7]] Here, lower_bound = 0, upper_bound = 7 Therefore, length = 7 – 0 + 1 = 8

© Oxford University Press 2012. All rights reserved.

WRITE A PROGRAM TO READ AND DISPLAY N NUMBERS USING AN ARRAY

#include<stdio.h>

#include<conio.h>

int main()

int i=0, n, arr[20];

clrscr();

printf(“\n Enter the number of elements : “);

scanf(“%d”, &n);

for(i=0;i<n;i++)

printf(“\n Arr[%d] = “, i);

scanf(“%d”,&num[i]);

printf(“\n The array elements are “);

for(i=0;i<n;i++)

printf(“Arr[%d] = %d\t”, i, arr[i]);

return 0;

© Oxford University Press 2012. All rights reserved.

DELETING AN ELEMENT FROM THE ARRAY

Algorithm to delete an element from the end of the array

Step 1: Set upper_bound = upper_bound - 1 Step 2: EXIT Step 1: [INITIALIZATION] SET I = POS Step 2: Repeat Steps 3 and 4 while I <= N - 1 Step 3: SET A[I] = A[I + 1] Step 4: SET I = I + 1 [End of Loop] Step 5: SET N = N - 1 Step 6: EXIT

The algorithm DELETE will be declared as DELETE( A, N, POS).

Calling DELETE (Data, 6, 2) will lead to the following processing in the array 45

Data[0] Data[1] Data[2] Data[3] Data[4] Data[5]

Data[0] Data[1] Data[2] Data[3] Data[4] Data[5]

Data[0] Data[1] Data[2] Data[3] Data[4] Data[5] 45

Data[0] Data[1] Data[2] Data[3] Data[4] Data[5] 45

Data[0] Data[1] Data[2] Data[3] Data[4]

© Oxford University Press 2012. All rights reserved.

LINEAR SEARCH LINEAR_SEARCH(A, N, VAL, POS) Step 1: [INITIALIZE] SET POS = - Step 2: [INITIALIZE] SET I = 0 Step 3: Repeat Step 4 while I<N Step 4: IF A[I] = VAL, then SET POS = I PRINT POS Go to Step 6 [END OF IF] [END OF LOOP] Step 5: PRINT “Value Not Present In The Array” Step 6: EXIT BINARY SEARCH

BEG = lower_bound and END = upper_bound

MID = (BEG + END) / 2

If VAL < A[MID], then VAL will be present in the left segment of the array. So,

the value of END will be changed as, END = MID – 1

If VAL > A[MID], then VAL will be present in the right segment of the array. So,

the value of BEG will be changed as, BEG = MID + 1

© Oxford University Press 2012. All rights reserved.

ONE DIMENSIONAL ARRAYS FOR INTER FUNCTION COMMUNICATION 1D Arrays For Inter Function Communication Passing individual elements (^) Passing entire array Passing individual elements Passing entire array

Passing data values

main()

int arr[5] ={1, 2, 3, 4, 5};

func(arr[3]);

void func(int num)

printf("%d", num);

Passing addresses

main()

int arr[5] ={1, 2, 3, 4, 5};

func(&arr[3]);

void func(int *num)

printf("%d", num);

Passing the entire array

main()

int arr[5] ={1, 2, 3, 4, 5};

func(arr);

moid func(int arr[5])

int i;

for(i=0;i<5;i++)

printf("%d", arr[i]);

© Oxford University Press 2012. All rights reserved.

TWO DIMENSIONAL ARRAYS

  • (^) A two dimensional array is specified using two subscripts where one subscript denotes row and

the other denotes column.

  • (^) C looks a two dimensional array as an array of a one dimensional array. First Dimension Second Dimension

A two dimensional array is declared as:

data_type array_name[row_size][column_size];

Therefore, a two dimensional mXn array is an

array that contains m*n data elements and each

element is accessed using two subscripts, i and j

where i<=m and j<=n

int marks[3][5]

Rows/Columns Col 0 Col 1 Col2 Col 3 Col 4 Row 0 Marks[0][0] Marks[0][1] Marks[0][2] Marks[0][3] Marks[0][4] Row 1 Marks[1][0]^ Marks[1][1]^ Marks[1][2]^ Marks[1][3]^ Marks[1][4] Row 2 Marks[2][0] Marks[2][1] Marks[2][2] Marks[2][3] Marks[2][4] Two Dimensional Array

© Oxford University Press 2012. All rights reserved.

TWO DIMENSIONAL ARRAYS CONTD..

  • (^) A two dimensional array is initialized in the same was as a single dimensional array is initialized. For example, int marks[2][3]={90, 87, 78, 68, 62, 71}; int marks[2][3]={{90,87,78},{68, 62, 71}}; Write a program to print the elements of a 2D array
  • #include<stdio.h>
  • (^) #include<conio.h>
  • (^) main()
  • {
  • (^) int arr[2][2] = {12, 34, 56,32};
  • (^) int i, j;
  • (^) for(i=0;i<2;i++)
  • (^) {
  • printf("\n");
  • (^) for(j=0;j<2;j++)
  • (^) printf("%d\t", arr[i][j]);
  • }
  • (^) return 0;
  • (^) }

© Oxford University Press 2012. All rights reserved.

TWO DIMENSIONAL ARRAYS FOR INTER FUNCTION COMMUNICATION Passing individual elements (^) Passing a row 2D Array for Inter Function Communication Passing the entire 2D array

There are three ways of passing parts of the two dimensional array to a function. First, we can pass

individual elements of the array. This is exactly same as we passed element of a one dimensional

array.

Passing a row

main()

int arr[2][3]= ( {1, 2, 3}, {4, 5, 6} };

func(arr[1]);

void func(int arr[])

int i;

for(i=0;i<5;i++)

printf("%d", arr[i] * 10);

Passing the entire 2D array

To pass a two dimensional array to a function, we use the array name as the actual parameter.

(The same we did in case of a 1D array). However, the parameter in the called function must

indicate that the array has two dimensions.

© Oxford University Press 2012. All rights reserved.

MULTI DIMENSIONAL ARRAYS

  • A multi dimensional array is an array of arrays.
  • Like we have one index in a single dimensional array, two indices in a two dimensional array, in

the same way we have n indices in a n-dimensional array or multi dimensional array.

  • (^) Conversely, an n dimensional array is specified using n indices.
  • (^) An n dimensional m1 x m2 x m3 x ….. mn array is a collection m1m2m3* ….. *mn elements.
  • (^) In a multi dimensional array, a particular element is specified by using n subscripts as A[I1][I2] [I3]…[In], where, I1<=M1 I2<=M2 I3 <= M3 ……… In <= Mn

© Oxford University Press 2012. All rights reserved.

PROGRAM TO READ AND DISPLAY A 2X2X2 ARRAY

#include<stdio.h>

int main()

{ int array1[3][3][3], i, j, k;

printf(“\n Enter the elements of the matrix”);

printf(“\n ******************************”);

for(i=0;i<2;i++)

{ for(j=0;j<2;j++)

for(k=0;k<2;k++)

printf(“\n array[%d][ %d][ %d] = ”, i, j, k);

scanf(“%d”, &array1[i][j][k]);

printf(“\n The matrix is : “);

printf(“\n *********************************”)l

for(i=0;i<2;i++)

{ printf(“\n\n”);

for(j=0;j<2;j++)

printf(“\n”);

for(k=0;k<2;k++)

printf(“\t array[%d][ %d][ %d] = %d”, i, j, k, array1[i][j][k]);

© Oxford University Press 2012. All rights reserved.

SPARSE MATRIX CONTD.

  • (^) In the second variant of a sparse matrix, elements with a non-zero value can appear only on the

diagonal or immediately above or below the diagonal. This type of matrix is also called a

tridiagonal matrix.

  • (^) In a tridiagonal matrix, Ai,j = 0 where | i – j| > 1. Therefore, if elements are present on
  • (^) the main diagonal the, it contains non-zero elements for i=j. In all there will be n elements
  • (^) diagonal below the main diagonal, it contains non zero elements for i=j+1. In all there will be n-

elements

  • (^) diagonal above the main diagonal, it contains non zero elements for i=j-1. In all there will be n-

elements