Difference between revisions of "C-and-C++/C3/Working-With-2D-Arrays/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title of script''': '''Author: Ashwini Patil''' '''Keywords: ''' {| style="border-spacing:0;" | style="border-top:0.002cm solid #000000;border-bottom:0.002cm solid #0000…')
 
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
'''Title of script''':  
+
'''Title of script''': Working with 2D arrays
  
 
'''Author: Ashwini Patil'''
 
'''Author: Ashwini Patil'''
  
'''Keywords: '''
+
'''Keywords: ''': 2D arrays, Matrix addition, video tutorial()
  
  
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 an 2D '''array.'''
+
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.04,
+
'''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.'''
 
'''0 1 2 3'''
 
 
  
 
{| style="border-spacing:0;"
 
{| style="border-spacing:0;"
Line 63: Line 60:
  
 
|}
 
|}
'''We can visualize a 2-D array in the form of a matrix.'''
 
 
'''Matrix is composed of multiple rows and horizontal columns.'''
 
 
 
'''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;"| Let us see how to declare '''2 dimensional array'''
+
| 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 how to declare an 2D '''array''' through an example.
+
| 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 on the editor.
+
I have already typed the program.
  
So, I will open it.
+
Let me open it.
 
+
Let me explain the program.
+
  
 
|-
 
|-
 
| 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;"| Here we have declared num1 with 3rows and 4columns
+
| 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.
 
+
We have considered 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;"| Here we take elements of the matrix '''num2''' 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;"|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;"| Here we display the matrix num2  
+
| 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;"| Here, we add the num1 matrix and the num2 matrix
+
| 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
  
Then we display the result
+
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 2darr
+
gcc space 2d-array.c space -o space arr
 +
and '''Press enter'''
  
 
To execute type,
 
To execute type,
  
./2darr
+
./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
| 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 execute the same program in C++.
+
 
 +
 
 +
Then Enter the values
 +
| 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;"| On the editor
+
| 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;"| Come back to our program.
+
| 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;"|  
+
| 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;"| I will change a few things here.
+
| 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;"| Click on '''File''' menu
+
| 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
  
Click on '''Save as''' option
+
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| First click on '''File''' menu,
+
| 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++.
  
Click on '''Save as''' option,
+
I have already made the program.
  
Save the file with '''.cpp''' extension.
+
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;"| Type
+
| 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;"| 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.
 +
 
 +
 
  
'''iostream'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| First lets change the '''header file''' as '''iostream'''
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
  
'''using namespace std;'''
+
'''<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;"| Let us include the '''using''' statement.
+
| 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;"| Type
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
  
'''<nowiki>cout <<</nowiki>'''
+
'''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;"| Now replace the '''printf''' statement with the '''cout''' statement.
+
| 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.
  
Here, '''\t''' mean horizontal tab that is equivalent to 4 spaces.
+
|-
 +
| 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;"| 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;"| Type
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
  
'''cin >>'''
+
'''cout'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Replace the '''scanf''' statement with the '''cin''' statement.
+
 
 +
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;"| Click on Save
+
| 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;"| Now click on '''Save'''
+
 
 +
'''/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;"|  
+
| 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;"| Let us execute the program and see
+
| 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;"| Come back to the terminal
+
| 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 355:
 
| 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 arr
+
'''g++ space array.cpp space -o space arr1'''
  
To execute
+
'''Press Enter'''
  
./arr
+
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
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here the output is displayed as,
+
  
'''The sum is 6.'''
+
 
 +
Then Enter the values
 +
| 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 438:
  
  
| 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 following link  
+
| 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 480:
  
 
| 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  
  

Latest revision as of 11:33, 31 January 2014

Title of script: Working with 2D arrays

Author: Ashwini Patil

Keywords: : 2D arrays, Matrix addition, video tutorial()


Visual Cue
Narration
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.

1 2 3 4
5 6 7 8
9 10 11 12

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


Then Enter the values

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

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.



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


Then Enter the values

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

  • It summarises the Spoken Tutorial project
  • If you do not have good bandwidth, you can download and watch it


* Watch the video available at the link shown below
  • It summarises the Spoken Tutorial project
  • If you do not have good bandwidth, you can download and watch it


Slide 5

Spoken Tutorial Workshops

The Spoken Tutorial Project Team

  • Conducts workshops using spoken tutorials
  • Gives certificates for those who pass an online test
  • For more details, please write to contact@spoken-tutorial.org


The Spoken Tutorial Project Team * Conducts workshops using spoken tutorials
  • Gives certificates for those who pass an online test
  • For more details, please write to contact at spoken hyphen tutorial dot org


Slide 6

Acknowledgement

  • 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
  • More information on this Mission is available at
  • http://spoken-tutorial.org/NMEICT-Intro


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
    More information on this Mission is available at the link shown below
  • spoken hyphen tutorial dot org slash NMEICT hyphen Intro


Remain on previous slide

No slide for this part

This is Ritwik Joshi from IIT Bombay.

Thank you for joining.

Contributors and Content Editors

Ashwini