Difference between revisions of "C-and-C++/C3/Working-With-2D-Arrays/English"
Line 19: | Line 19: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we will learn, | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we will learn, | ||
− | What is | + | What is a 2Dimensional '''array.''' |
We will do this with the help of an example. | We will do this with the help of an example. | ||
Line 27: | Line 27: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To record this tutorial, I am using | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To record this tutorial, I am using | ||
− | '''Ubuntu Operating System''' version 11. | + | '''Ubuntu Operating System''' version 11.10, |
'''gcc''' and '''g++''' '''Compiler''' version 4.6.1 on Ubuntu | '''gcc''' and '''g++''' '''Compiler''' version 4.6.1 on Ubuntu | ||
Line 40: | Line 40: | ||
'''The right index indicates the column.''' | '''The right index indicates the column.''' | ||
− | |||
− | |||
− | |||
{| style="border-spacing:0;" | {| style="border-spacing:0;" | ||
Line 63: | Line 60: | ||
|} | |} | ||
− | |||
− | |||
− | |||
− | |||
'''Starting index of a matrix or array in C and C++ is always 0.''' | '''Starting index of a matrix or array in C and C++ is always 0.''' | ||
− | + | Here we see a 2dimensional array in a row column matrix. | |
+ | Starting index is 0. | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 5 | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 5 | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us see how to declare '''2 dimensional array''' |
The Syntax for this is: | The Syntax for this is: | ||
− | '''<nowiki>data-type arr_name[row] [col];</nowiki>''' | + | '''<nowiki>data-type arr_name[row] and [col];</nowiki>''' |
− | eg.'''<nowiki> int num[2][3];</nowiki>''' | + | eg.here we have declared a 2dimensional array num with 2rows and 3columns.'''<nowiki> int num[2][3];</nowiki>''' |
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the editor | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the editor | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us see | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us see an example. |
− | I have already typed the program | + | I have already typed the program. |
− | + | Let me open it. | |
− | + | ||
− | Let me | + | |
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Point to array.c | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Point to array.c | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Note that our file name is '''2d-array.c''' | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Note that our file name is '''2d-array.c''' | ||
− | + | In this program we will calculate the sum of the elements of the 2dimensional arrays. | |
+ | Let me explain the code now. | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | ||
Line 118: | Line 111: | ||
'''<nowiki>int num1[3][4],num2[3][4];</nowiki>''' | '''<nowiki>int num1[3][4],num2[3][4];</nowiki>''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we have declared num1 with 3rows and 4columns |
− | And num2 with 3rows and 4columns | + | And num2 again with 3rows and 4columns |
|- | |- | ||
Line 132: | Line 125: | ||
'''<nowiki>scanf("%d", &num1[i][j]); </nowiki>''' | '''<nowiki>scanf("%d", &num1[i][j]); </nowiki>''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we take elements of the matrix '''num1''' as input from the user. | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|num1 and num2 are 2dimensional arrays. |
+ | Here we take elements of the matrix '''num1''' as input from the user. | ||
The elements are stored row-wise. | The elements are stored row-wise. | ||
− | We have considered i for rows | + | We have considered i for rows and j for columns. |
− | + | ||
− | + | ||
This for loop will check the condition that i runs from 0 to 2. | This for loop will check the condition that i runs from 0 to 2. | ||
Line 154: | Line 146: | ||
'''<nowiki>scanf("%d", &num2[i][j]);</nowiki>''' | '''<nowiki>scanf("%d", &num2[i][j]);</nowiki>''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|Similarly here we take elements of the matrix '''num2''' as input from the user. |
− | + | ||
− | + | ||
Line 176: | Line 166: | ||
'''}''' | '''}''' | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we display the matrix num1 | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we display the matrix num1 | ||
− | + | Here %3d is used to align the matrix on the terminal. | |
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | ||
Line 193: | Line 183: | ||
'''}''' | '''}''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|Now here we display the matrix num2 |
|- | |- | ||
Line 211: | Line 201: | ||
'''}''' | '''}''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we add the num1 matrix and the num2 matrix |
− | + | And display the result | |
|- | |- | ||
Line 231: | Line 221: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Press '''Ctrl, Alt and T keys ''' | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Press '''Ctrl, Alt and T keys ''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Please open the terminal window by pressing '''Ctrl, Alt''' and '''T''' keys simultaneously. | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Please open the terminal window by pressing '''Ctrl, Alt''' and '''T''' keys simultaneously on your keyboard. |
|- | |- | ||
Line 243: | Line 233: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile type, | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile type, | ||
− | gcc 2d-array.c -o | + | gcc space 2d-array.c space -o space arr |
+ | and '''Press enter''' | ||
To execute type, | To execute type, | ||
− | ./ | + | ./arr |
+ | Now '''Press enter''' | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Enter values from 1 to 12 |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now | + | |
+ | |||
+ | Then Enter the values from 12 to 1 | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we see: | ||
+ | |||
+ | '''Enter the element of 3X4 array num1''' | ||
+ | |||
+ | I will enter the values now. | ||
+ | |||
+ | Now we can see, | ||
+ | |||
+ | '''Enter the elements of 3X4 array num2:''' | ||
+ | |||
+ | I will enter the values. | ||
+ | |||
+ | The output is displayed. | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight num1 matrix |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we can see the num1 matrix. |
+ | |||
+ | |- | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight num2 matrix | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we can see the num2 matrix. | ||
+ | |||
+ | |- | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the sum. | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| And this is sum of num1 and num2 | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I will | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we will see how to execute the same program in C++. |
+ | |||
+ | I have already made the program. | ||
+ | |||
+ | I will oepn it and explain. | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Point the cursor to the filename |
− | + | '''2d-array.cpp''' | |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt s|- |
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Enter values from 1 to 12 | ||
− | |||
− | + | Then Enter the values from 12 to 1 | |
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we see: | ||
+ | |||
+ | '''Enter the element of 3X4 array num1''' | ||
+ | |||
+ | I will enter the values now. | ||
+ | |||
+ | Now we can see, | ||
+ | |||
+ | '''Enter the elements of 3X4 array num2:''' | ||
+ | |||
+ | I will enter the values. | ||
+ | |||
+ | The output is displayed. | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight num1 matrix |
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we can see the num1 matrix. | ||
− | + | |- | |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight num2 matrix |
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we can see the num2 matrix. | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the sum. |
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| And this is sum of num1 and num2 | ||
+ | |||
+ | |- | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we will see how to execute the same program in C++. | ||
+ | |||
+ | I have already made the program. | ||
+ | |||
+ | I will oepn it and explain. | ||
+ | |||
+ | |-olid #000000;padding:0.097cm;"| This is the program for 2D arrays in C++ | ||
+ | |||
+ | Note that our file name is 2d-array.cpp | ||
+ | |||
+ | The extension is .cpp. | ||
+ | |||
+ | Let me explain the code now. | ||
+ | |||
+ | |||
− | |||
− | |||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight |
− | '''<nowiki> | + | '''<nowiki>include<iostream></nowiki>''' |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our header file as “iostream”. |
− | + | |- | |
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | ||
+ | |||
+ | '''using namespace std''' | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our “ using” statement. | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight |
− | ''' | + | '''int main()''' |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our main function. |
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | |
+ | '''cout''' | ||
+ | |||
+ | and''' ''' | ||
+ | |||
+ | '''cin''' | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have cout function as we use cout to print the ouput in C++ | ||
+ | |||
+ | Then we have “cin ''function. | ||
+ | |||
+ | We use cin to read a line in C++. | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | |
+ | '''/t''' | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we use '''''/t''''' it means horizontal tab that is equivalent to 4 spaces. | ||
+ | |||
+ | Rest of the code is similar to our C code. | ||
+ | |||
+ | |- | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on '''Save''' | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now click on save. | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute come back to our terminal. |
+ | |||
+ | Let me clear the prompt. | ||
|- | |- | ||
Line 319: | Line 394: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To comple type, | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To comple type, | ||
− | g++ array.cpp -o | + | '''g++ space array.cpp space -o space arr1''' |
− | + | '''Press Enter''' | |
− | ./ | + | To execute type |
+ | |||
+ | '''./arr1''' | ||
+ | |||
+ | '''Press Enter''' | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Enter values from 1 to 12 |
− | + | ||
− | '''The | + | |
+ | Then Enter the values from 12 to 1 | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we see: | ||
+ | |||
+ | '''Enter the element of 3X4 array num1''' | ||
+ | |||
+ | I will enter the values. | ||
+ | |||
+ | Now we see, | ||
+ | |||
+ | '''Enter the elements of 3X4 array num2:''' | ||
+ | |||
+ | I will give the values as | ||
+ | |||
+ | '''The output is displayed. ''' | ||
+ | |||
+ | |- | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | ||
+ | |||
+ | num1 matrix | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We can see the num1 matrix. | ||
+ | |||
+ | |- | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | ||
+ | |||
+ | num2 matrix | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The num2 matrix. | ||
+ | |||
+ | |- | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | ||
+ | |||
+ | The sum | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| And this is sum of num1 and num2 | ||
+ | |||
+ | This brings us to the end of this tutorial. | ||
+ | |||
+ | Come back to our slides | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Summary | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Summary | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this turoial we learnt, | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us summarize: |
+ | |||
+ | In this turoial we learnt, | ||
To add elements in a 2D array. | To add elements in a 2D array. | ||
To print 2D array. | To print 2D array. | ||
+ | |||
+ | And | ||
To calculate the sum of 2D array. | To calculate the sum of 2D array. | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Assignment | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Assignment |
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Write a program that takes two 2D arrays as input. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Write a program that takes two 2D arrays as input. | ||
Line 359: | Line 477: | ||
− | | style="background-color:transparent;border-top:none;border-bottom:none;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| * Watch the video available at the | + | | style="background-color:transparent;border-top:none;border-bottom:none;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| * Watch the video available at the link shown below |
* It summarises the Spoken Tutorial project | * It summarises the Spoken Tutorial project | ||
Line 401: | Line 519: | ||
| style="background-color:transparent;border-top:none;border-bottom:none;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Spoken Tutorial Project is a part of the Talk to a Teacher project | | style="background-color:transparent;border-top:none;border-bottom:none;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Spoken Tutorial Project is a part of the Talk to a Teacher project | ||
− | * It is supported by the National Mission on Education through ICT, MHRD, Government of India <br/> More information on this Mission is available at | + | * It is supported by the National Mission on Education through ICT, MHRD, Government of India <br/> More information on this Mission is available at the link shown below |
* spoken hyphen tutorial dot org slash NMEICT hyphen Intro | * spoken hyphen tutorial dot org slash NMEICT hyphen Intro | ||
Revision as of 11:29, 31 January 2014
Title of script: Working with 2D arrays
Author: Ashwini Patil
Keywords: : 2D arrays, Matrix addition, video tutorial()
|
| ||||||||||||
Slide 1 | Welcome to the spoken-tutorial on 2dimensional Arrays in C and C++. | ||||||||||||
Slide 2 | In this tutorial we will learn,
What is a 2Dimensional array. We will do this with the help of an example. | ||||||||||||
Slide 3 | To record this tutorial, I am using
Ubuntu Operating System version 11.10, gcc and g++ Compiler version 4.6.1 on Ubuntu | ||||||||||||
Slide 4 | Let us start with the introduction to 2 dimensional Array
2-D arrays are stored in a row column matrix. The left index indicates the row. The right index indicates the column.
Starting index of a matrix or array in C and C++ is always 0. Here we see a 2dimensional array in a row column matrix. Starting index is 0. | ||||||||||||
Slide 5 | Now let us see how to declare 2 dimensional array
The Syntax for this is: data-type arr_name[row] and [col]; eg.here we have declared a 2dimensional array num with 2rows and 3columns. int num[2][3]; | ||||||||||||
On the editor | Now let us see an example.
I have already typed the program. Let me open it. | ||||||||||||
Point to array.c | Note that our file name is 2d-array.c
In this program we will calculate the sum of the elements of the 2dimensional arrays. Let me explain the code now. | ||||||||||||
Highlight
#include <stdio.h> |
This is our header file. | ||||||||||||
Highlight
int main() |
This is our main function. | ||||||||||||
Highlight
int i,j;
|
Here, we have declared variable i and j. | ||||||||||||
Highlight
int num1[3][4],num2[3][4]; |
Then we have declared num1 with 3rows and 4columns
And num2 again with 3rows and 4columns | ||||||||||||
Highlight
printf("Enter the elememts of 3X4 array num1\n"); for(i=0; i<3; i++) for(j=0; j<4; j++) scanf("%d", &num1[i][j]); |
num1 and num2 are 2dimensional arrays.
Here we take elements of the matrix num1 as input from the user. The elements are stored row-wise. We have considered i for rows and j for columns. This for loop will check the condition that i runs from 0 to 2. This for loop will check the condition that j runs from 0 to 3. | ||||||||||||
Highlight
printf("Enter the elememts of 3X4 array num2\n"); for(i=0; i<3; i++) for(j=0; j<4; j++) scanf("%d", &num2[i][j]); |
Similarly here we take elements of the matrix num2 as input from the user.
| ||||||||||||
Highlight
printf("The 3X4 array num1 is\n"); for(i=0; i<3; i++) { for(j=0; j<4; j++) printf("%3d ", num1[i][j]); printf("\n"); } |
Here we display the matrix num1
Here %3d is used to align the matrix on the terminal. | ||||||||||||
Highlight
printf("The 3X4 array num2 is\n"); for(i=0; i<3; i++) { for(j=0; j<4; j++) printf("%3d ", num2[i][j]); printf("\n"); } |
Now here we display the matrix num2 | ||||||||||||
Highlight
printf("The sum of num1 and num2 is\n"); for(i=0; i<3; i++) { for(j=0; j<4; j++) printf("%3d ", (num1[i][j] + num2[i][j])); printf("\n"); } |
Then we add the num1 matrix and the num2 matrix
And display the result | ||||||||||||
Highlight
return 0; |
This is our return statement. | ||||||||||||
Click on Save | Now, click on Save. | ||||||||||||
Let us execute the program. | |||||||||||||
Press Ctrl, Alt and T keys | Please open the terminal window by pressing Ctrl, Alt and T keys simultaneously on your keyboard. | ||||||||||||
On the terminal
Type gcc array.c -o arr ./arr |
To compile type,
gcc space 2d-array.c space -o space arr and Press enter To execute type, ./arr Now Press enter | ||||||||||||
Enter values from 1 to 12
|
Here we see:
Enter the element of 3X4 array num1 I will enter the values now. Now we can see, Enter the elements of 3X4 array num2: I will enter the values. The output is displayed. | ||||||||||||
Highlight num1 matrix | Here we can see the num1 matrix. | ||||||||||||
Highlight num2 matrix | Here we can see the num2 matrix. | ||||||||||||
Highlight the sum. | And this is sum of num1 and num2 | ||||||||||||
Now we will see how to execute the same program in C++.
I have already made the program. I will oepn it and explain. | |||||||||||||
Point the cursor to the filename
2d-array.cpp |
- | Enter values from 1 to 12
|
Here we see:
Enter the element of 3X4 array num1 I will enter the values now. Now we can see, Enter the elements of 3X4 array num2: I will enter the values. The output is displayed. | ||||||||||
Highlight num1 matrix | Here we can see the num1 matrix. | ||||||||||||
Highlight num2 matrix | Here we can see the num2 matrix. | ||||||||||||
Highlight the sum. | And this is sum of num1 and num2 | ||||||||||||
Now we will see how to execute the same program in C++.
I have already made the program. I will oepn it and explain. | |||||||||||||
Highlight
include<iostream> |
This is our header file as “iostream”. | ||||||||||||
Highlight
using namespace std |
This is our “ using” statement. | ||||||||||||
Highlight
int main() |
This is our main function. | ||||||||||||
Highlight
cout and cin |
Here we have cout function as we use cout to print the ouput in C++
Then we have “cin function. We use cin to read a line in C++. | ||||||||||||
Highlight
/t |
Here we use /t it means horizontal tab that is equivalent to 4 spaces.
Rest of the code is similar to our C code. | ||||||||||||
Click on Save | Now click on save. | ||||||||||||
On the terminal | Let us execute come back to our terminal.
Let me clear the prompt. | ||||||||||||
Type
g++ array.cpp -o arr Type ./arr |
To comple type,
g++ space array.cpp space -o space arr1 Press Enter To execute type ./arr1 Press Enter | ||||||||||||
Enter values from 1 to 12
|
Here we see:
Enter the element of 3X4 array num1 I will enter the values. Now we see, Enter the elements of 3X4 array num2: I will give the values as The output is displayed. | ||||||||||||
Highlight
num1 matrix |
We can see the num1 matrix. | ||||||||||||
Highlight
num2 matrix |
The num2 matrix. | ||||||||||||
Highlight
The sum |
And this is sum of num1 and num2
This brings us to the end of this tutorial. Come back to our slides | ||||||||||||
Summary | Let us summarize:
In this turoial we learnt, To add elements in a 2D array. To print 2D array. And To calculate the sum of 2D array. | ||||||||||||
Assignment | Write a program that takes two 2D arrays as input.
Subtract them and find the result. | ||||||||||||
Slide 4
About the Spoken Tutorial Project
|
* Watch the video available at the link shown below
| ||||||||||||
Slide 5
Spoken Tutorial Workshops The Spoken Tutorial Project Team
|
The Spoken Tutorial Project Team * Conducts workshops using spoken tutorials
| ||||||||||||
Slide 6
Acknowledgement
|
Spoken Tutorial Project is a part of the Talk to a Teacher project
| ||||||||||||
Remain on previous slide
No slide for this part |
This is Ritwik Joshi from IIT Bombay.
Thank you for joining. |