Docsity

Unit 3 18CSS101J Programming for Problem Solving, Slides of Programming for Engineers

Partial preview of the text

Download Unit 3 18CSS101J Programming for Problem Solving and more Slides Programming for Engineers in PDF only on Docsity!

18CSS101J – Programming for Problem Solving

Unit III

SRM

INSTITUTE OF SCIENCE ANDTECHNOLOGY,

CHENNAI.

SRM INSTITUTE OF SCIENCE AND TECHNOLOGY,

CHENNAI.

UNIT III

Initializing and accessing of 2D Array – Initializing multidimensional

Array – Array programs 2D – Array contiguous memory – Array

advantages and Limitations – Array construction for real-time

application common programming errors – String Basics – String

Declaration and Initialization – String Functions: gets(), puts(),

getchar(), putchar(), printf() - String Functions: atoi, strlen, strcat

strcmp – String Functions: sprintf, sscanf, strrev, strcpy, strstr, strtok –

Arithmetic characters on strings.

 Two Dimensional Array requires Two Subscript Variables  Two Dimensional Array stores the values in the form of matrix.  One Subscript Variable denotes the “ Row ” of a matrix.  Another Subscript Variable denotes the “ Column ” of a matrix.

3.1.2 INITIALIZING OF 2D ARRAY An array of two dimensions can be declared as follows: data_type array_name[size1][size2]; Here data_type is the name of some type of data, such as int. Also, size1 and size2 are sizes of the array’s first and second dimensions respectively.

We have declared an array of size 3 X 2, It contain overall 6 elements.

Row 1 : { 1 , 4 },

Row 2 : { 5 , 2 },

Row 3 : { 6 , 5 }

We have initialized each row independently

a[0][0] = 1

A[0][1] = 4

Method 2 : COMBINE ASSIGNMENT

Initialize all Array elements but initialization is much

straight forward. All values are assigned sequentially and

row-wise

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

So here it automatically assigns that number 3 has row and

number 2 has column.

Method 3: SELECTIVE ASSIGNMENT

int a[3][2] = { { 1 },

{ 5 , 2 }, { 6 } };

Now we have again going with the way 1 but we are removing some

of the elements from the array. In this case we have declared and

initialized 2-D array like this

Consider the below program –

#include <stdio.h> int main() { int i, j; int a[3][2] = { { 1 }, { 5, 2 }, { 6 }}; for (i = 0; i < 3; i++) { for (j = 0; j < 2; j++) { printf("%d ", a[i][j]); } printf("\n"); } return 0; }

Output :

You can initialize a three dimensional array in a similar way like a two dimensional array. Here's an example,

int test[2][3][4] = { { {3, 4, 2, 3}, {0, -3, 9, 11}, {9, 1, 6, 2} }, { {13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9} } };

In this program we mention size1 – row, size2 – column, size3 – number of elements in that array.

C Program to store values entered by the user in a three-dimensional array

and display it.

#include <stdio.h>

int main()

{

int i, j, k, test[2][3][2]; // this array can store 12 elements printf("Enter 12 values: \n"); for(i = 0; i < 2; ++i) { for (j = 0; j < 3; ++j) { for(k = 0; k < 2; ++k ) { scanf("%d", &test[i][j][k]); }

OUTPUT:

Enter 12 values:

Displaying Values: test[0][0][0] = 1 test[0][0][1] = 2 test[0][1][0] = 3 test[0][1][1] = 4 test[0][2][0] = 5 test[0][2][1] = 6 test[1][0][0] = 7 test[1][0][1] = 8 test[1][1][0] = 9 test[1][1][1] = 10 test[1][2][0] = 11 test[1][2][1] = 12

3.3 ARRAY PROGRAMS – 2D

a) Basic 2d array program b) Store and display values

c) Sum of two matrices using Two dimensional arrays d) Transpose of Matrix e) Multiplication of 2d matrices.