C-and-C++/C3/Working-With-2D-Arrays/English

From Script | Spoken-Tutorial
Revision as of 11:33, 31 January 2014 by Ashwini (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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