C-and-C++/C3/Working-With-2D-Arrays/Gujarati
From Script | Spoken-Tutorial
Time' | Narration |
00:02 | Welcome to the spoken-tutorial on 2dimensional Arrays in C and C++. |
00:08 | In this tutorial we will learn, |
00:11 | What is a 2dimensional array. |
00:13 | We will do this through an example. |
00:16 | To record this tutorial, I am using |
00:19 | Ubuntu Operating System version 11.10, |
00:23 | gcc and g++ Compiler version 4.6.1 on Ubuntu |
00:30 | Let us start with the introduction to 2 dimensional Array |
00:34 | 2-D arrays are stored in a row column matrix. |
00:38 | The left index indicates the row. |
00:42 | The right index indicates the column. '
|
00:45 | Starting index of a matrix or array in C and C++ is always 0. |
00:52 | Here we see a 2 dimensional array |
00:55 | In a row column matrix. Starting index is0
|
01:01 | Now let us see how to declare a 2dimensional array |
01:05 | The Syntax for this is: |
01:07 | data-type arr_name[row] [col]; |
01:13 | eg. Here we have declare a 2 dimensional array num[2][3]; |
01:22 | Now let us see an example |
01:24 | I have already typed the program |
01:26 | Let me open it.
|
01:28 | Note that our file name is 2d-array.c |
01:34 | In this program we will calculate, the sum of the elements of the 2dimensional arrays. |
01:41 | Let me explain the code now. |
01:44 | This is our header file. |
01:47 | This is our main function. |
01:49 | Here, we have declared variable i and j. |
01:53 | Then we have declared num1 with 3rows and 4columns |
01:59 | And num2 again with 3rows and 4columns
|
02:04 | num1 and num2 are 2dimensional arrays |
02:08 | Here we take elements of the matrix num1 as input from the user. |
02:14 | The elements are stored row-wise. |
02:17 | We have considered i for rows and j for columns. |
02:23 | This for loop will check the condition that i runs from 0 to 2. |
02:28 | This for loop will check the condition that j runs from 0 to 3. |
02:34 | Similarly, here we take elements of the matrix num2 as input from the user.
|
02:41 | Here we display the matrix num1 |
02:44 | Here %3d is used to align the matrix on the terminal. |
02:50 | Now here we display the matrix num2 |
02:53 | Then we add the num1 matrix and the num2 matrix |
02:57 | And display the result |
03:00 | This is our return statement. |
03:02 | Now, click on Save. |
03:05 | Let us execute the program. |
03:08 | Please open the terminal window by pressing Ctrl, Alt and T keys simultaneously on your keyboard. |
03:15 | To compile type, |
03:17 | gcc 2d-array.c -o arr and press enter |
03:29 | To execute type, ./ arr now press enter. |
03:34 | Here we see enter the element of 3 into 4 array num1. |
03:40 | I will enter the values now |
03:52 | Now we can see enter the element of 3 into 4 array num2 |
03:58 | I will enter the values. |
04:11 | The output is displayed, |
04:13 | Here we can see the num1 matrix. |
04:17 | here we can see the num2 matrix. |
04:20 | And this is the sum of num1 and num2 |
04:24 | Now we will see how to execute the same program in C++. |
04:30 | I have already named the program. I will open it and explain. |
04:34 | This is the program for 2dimensional arrays in C++. |
04:39 | Note that our file name is 2d-array.cpp'. The extension is .cpp. |
04:48 | Let me explain the code now. This is our header file as iostream |
04:53 | This is our using statement. |
04:56 | This is our main function
|
04:58 | Here we have coutfunction. As we use cout to print the output in C++. |
05:06 | Then we have cin function, we use cin' to read a line in C++ |
05:14 | Here we use \t it means horizontal tab that is equivalent to 4 spaces. |
05:21 | Rest of the code is similar to our c code .Now click on Save' |
05:27 | Let us execute |
05:29 | Come back to our terminal |
05:31 | Let me clear the prompt. |
05:34 | To compile type, g++ 2d- array.cpp -o arr and press enter. |
05:47 | To execute type, ./arr1. Now press enter. |
05:53 | Here we see enter the element of 3 into 4 array num1. |
05:58 | I will enter the values. |
06:08 | Now we see enter the elements of 3 into 4 array num2. |
06:14 | I will give the values as |
06:24 | The output is displayed , We can see the num1 matrx, num2 matrix. |
06:32 | And this is the sum of num1 and num2. |
06:36 | This brings us to the end of this tutorial. |
06:39 | Come back to our slides. Let us summarises |
06:43 | In this tutprial we learnt, |
06:45 | To add elements in a 2D array. |
06:49 | To print 2D array. |
06:51 | And To calculate the sum of 2D array. |
06:55 | As an assignment, |
06:56 | Write a program that takes two 2D arrays as input from the user. |
07:02 | Subtract them and find the result. |
07:05 | Watch the video available at the link shown below |
07:08 | It summarises the Spoken Tutorial project |
07:11 | If you do not have good bandwidth, you can download and watch it
|
07:16 | The Spoken Tutorial Project Team |
07:18 | Conducts workshops using spoken tutorials |
07:21 | Gives certificates for those who pass an online test |
07:26 | For more details, please write to contact at spoken hyphen tutorial dot org
|
07:33 | Spoken Tutorial Project is a part of the Talk to a Teacher project |
07:37 | It is supported by the National Mission on Education through ICT, MHRD, Government of India |
07:44 | More information on this Mission is available at the link shown below.
|
07:49 | The script is contributed by Ritwik Joshi , this is Ashwini Patil from IIT Bombay.
Thank you for watching |