Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lets Play with arrays Singh Tripty

Similar presentations


Presentation on theme: "Lets Play with arrays Singh Tripty"— Presentation transcript:

1 Lets Play with arrays Singh Tripty
We will start with 1-D

2 Row X Column

3

4 To Calculate Average Using Arrays
Program for while loop for loop and arrays To Calculate Average Using Arrays ARR[0] ARR[1] ARR[2] …… ARR[n]

5

6

7 Output Enter total number of elements(1 to 100): 8
Enter Number 1: 23.4 Enter Number 2: -34.5 Enter Number 3: 50 Enter Number 4: 33.5 Enter Number 5: 55.5 Enter Number 6: 43.7 Enter Number 7: 5.7 Enter Number 8: -66.5 This program takes n number of elements from user and stores it in array arr[]. To find the largest element, the first two elements of array are checked and largest of these two element is placed in arr[0]. Then, the first and third elements are checked and largest of these two element is placed in arr[0]. This process continues until and first and last elements are checked. Finally, the largest element of an array will be in arr[0] position. Check out these related examples:

8 How to pass arrays to a function in C Programming?
Program to Calculate Standard Deviation To calculate the standard deviation, calculateSD() function is created. The array containing 10 elements is passed to the function and this function calculates the standard deviation and returns it to the main() function.

9

10

11

12 Now 2-D Matrix C Program to Add Two Matrix Using Multi-dimensional Arrays This program takes two matrices of order r*c and stores it in two-dimensional array. Then, the program adds these two matrices and displays it on the screen.

13

14

15

16

17 Program to Multiply Two Matrices
To multiply two matrices, the number of columns of first matrix should be equal to the number of rows to second matrix. This program displays the error until the number of columns of first matrix is equal to the number of rows of second matrix.

18

19

20

21

22 Another example Problem Description
This C Program finds the sum of the main & opposite diagonal elements of a MxN Matrix. The program accepts an MxN matrix. Then adds main diagonal of matrix as well as the opposite diagonal of the matrix. Problem Solution 1. Declare a matrix, taking order as input from users and define all its elements. 2. Declare two variable to store sum of each diagonal elements. 3. Run a for loop wherein the main diagonal element is given by index (i, i) where i is the iterator and opposite diagonal element is given by index(i, total_rows(m)-i-1). 4. The two variables are initialized to 0, which are summed up by diagonal elements. 5. Print the sum of diagonal elements.

23

24

25 Enter the order of the matix 2 2 Enter the co-efficients of the matrix
Runtime Test Cases Enter the order of the matix 2 2 Enter the co-efficients of the matrix 40 30 38 90 The given matrix is 90 The sum of the main diagonal elements is = 130 The sum of the off diagonal elements is = 68

26 C Program to Multiply two Matrices by Passing Matrix to a Function
To understand this example, you should have the knowledge of following C programming topics: C Programming Arrays C Programming Multidimensional Arrays How to pass arrays to a function in C Programming? This program asks the user to enter the size of the matrix (rows and column). Then, it asks the user to enter the elements of those matrices and finally adds and displays the result. To perform this task three functions are made: To takes matrix elements from user enterData() To multiply two matrix multiplyMatrices() To display the resultant matrix after multiplication display()

27

28

29

30

31

32 Finding transpose of a matrix
The transpose of a matrix is a new matrix whose rows are the columns of the original. (This makes the columns of the new matrix the rows of the original). Here is a matrix and its transpose: The superscript "T" means "transpose

33

34

35


Download ppt "Lets Play with arrays Singh Tripty"

Similar presentations


Ads by Google