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

Unit 3 18CSS101J Programming for Problem Solving Detailed lecture slides covering all topics in detail as prescribed by the curriculum for the academic term. Important Exam Material.

Typology: Slides

2017/2018

Uploaded on 02/20/2022

vasundhara-jhobta
vasundhara-jhobta 🇺🇸

4.6

(8)

8 documents

1 / 145

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
18CSS101J Programming for Problem Solving
Unit III
SRM
INSTITUTE OF SCIENCE ANDTECHNOLOGY,
CHENNAI.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

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.