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

From Script | Spoken-Tutorial
Revision as of 16:23, 18 December 2013 by Ashwini (Talk | contribs)

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 an 2D 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.04,

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.

0 1 2 3


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

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.

Slide 5 Let us see how to declare 2 dimensional array

The Syntax for this is:

data-type arr_name[row] [col];

eg. int num[2][3];

On the editor Now let us see how to declare an 2D array through an example.

I have already typed the program on the editor.

So, I will open it.

Let me explain the program.

Point to array.c Note that our file name is 2d-array.c
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];

Here we have declared num1 with 3rows and 4columns

And num2 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]);

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.

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 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]);

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
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");

}

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");

}

Here, we add the num1 matrix and the num2 matrix

Then we 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 the terminal

Type

gcc array.c -o arr

./arr

To compile type,

gcc 2d-array.c -o 2darr

To execute type,

./2darr

Now let us see how to execute the same program in C++.
On the editor Come back to our program.
I will change a few things here.
Click on File menu

Click on Save as option

First click on File menu,

Click on Save as option,

Save the file with .cpp extension.

Type

iostream

First lets change the header file as iostream
Type

using namespace std;

Let us include the using statement.
Type

cout <<

Now replace the printf statement with the cout statement.

Here, \t mean horizontal tab that is equivalent to 4 spaces.

Type

cin >>

Replace the scanf statement with the cin statement.
Click on Save Now click on Save
Let us execute the program and see
On the terminal Come back to the terminal
Type

g++ array.cpp -o arr

Type

./arr

To comple type,

g++ array.cpp -o arr

To execute

./arr

Here the output is displayed as,

The sum is 6.

Summary In this turoial we learnt,

To add elements in a 2D array.

To print 2D array.

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 following link
  • 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
  • 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