C-and-C++/C3/Arrays/English

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of script: Arryas in C and C++

Author: Ashwini Patil

Keywords: arrays, single dimensional, multi dimensional, video tutorial()



Visual Cue
Narration
Slide 1 Welcome to the spoken-tutorial on Arrays in C and C++.
Slide 2 In this tutorial we will learn,

What is an array.

Declaration of an array.

Initialization of an array.

Few examples on array.

We will also see some common errors and their solutions.

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-5 Let us start with the introduction to Array.

Array is the collection of data or elements of same data-type.

Array index starts from 0.

The first element is stored at index 0.

There are three types of arrays:

  1. Single dimensional array.
  2. Two dimensional array.
  3. Multi-dimensional array.

We will be discussing single dimensional array in this tutorial.

Slide 6 Let us see how to declare a single dimensional array.

The Syntax for this is:

data-type (name of the array) and size;

eg.( char star[5];) Here we have declared an integer array star which contains 5 elements. The array index will start from star0 to star 4.

Slide 7 We saw the declaration of an array now we will see the initialize of an array.

The Syntax for this is:

data-type (name of the array)array-name[size]={elements};

eg. int star[3]={1,2,3}; Here we have declared an integer element star with size 3. The elements of the array are 1, 2 and 3. Here the index will start from star0 to star 2.

On the editor Now let us move to the examples.

I have already typed the program on the editor.

So, let me open it.

Point to array.c Please note that our file name is array.c
In this program, we will calculate the sum of the elements stored in an array.

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 star[3]={1,2,3};


Here, we have declared and initialized an array star with size 3.

The elements of the array are 4, 5 and 6.

Highlight

int sum;

Then we have declared an integer variable sum.
Highlight

sum = star[0] + star[1] + star[2];

Here we add the elements of the array and store the result in sum.

Note that 4 will be stored at index 0. 5 will be stored at index 1 and 6 will be stored at index 2.

Highlight

printf("The sum is %d\n",sum);

Then we print the sum.
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 array

./arr

To compile type,

gcc space array.c space -o space array

Press Enter To execute type,

./array Press Enter

Highlight

Output

Here the output is displayed as,

The sum is 15.

Errors Now let us see some common errors which we can come accross.
On the editor Come back to our program.
Delete {}

int star[3]=1,2,3;

Suppose here at line no.4, we miss the curly brackets.
Click on Save Click on Save.
Let us see what happens.
On the terminal Come back to our terminal.

Let us compile as before.

Highlight

Error

We see errors.

Invalid initializer and Expected identifier or (bracket) '(' before numeric constants.


{ } This is because arrays must be initialized within curly brackets.
Come back to our program.

Let us fix the error. Type the curly bracket here at line no. 4. Now click on Save

Let us execute.

Come back to our terminal. Let us compile as before. Let us execute as before. Yes it is working.

Now we will execute the same program in C++.
On the editor Come back to our program.
I will change a few things here.

Now save the file with an extension .cpp

First press shift+ctrl and s keys simultaneously on your keyboard.

And click on Save

Type

iostream

Let us change the header file as iostream.
Type

using namespace std;

Now include the using statement.

The declaration and initialization of an array is same in C++. Hence no need to change anything here.

Type

cout <<

Now replace the printf statement with the cout statement.


Delete Delete the format specifier and backslash n.

Now delete the comma and type two opening angle brackets. Delete the bracket here. Again type two opening angle brackets and within the double quotes type backslash n.


Click on Save Now click on Save
Let us execute.
On the terminal Come back to our terminal
Type

g++ array.cpp -o arr

Type

./arr

To compile type,

g++ space array.cpp space -o space array1

Here we have array1 because we don't want to overwrite the output parameter array for the file array.c

Now press Enter

To execute

./array1 press Enter

Highlight

Output

The output is displayed as,

The sum is 15. We can see that it is similar to our C code.

Errors Now we will see another common error

Come back to our program.

At line 7

Type:

star[1] + star[2] + star[3];

Suppose here, at line number 7

I will type

star[1] + star[2] + star[3]; Click on Save

On the terminal Let us execute.

Come back to our terminal. Let me clear the prompt.

Let us compile as before.

Let us execute as before.

Highlight

Output

We get an unexpected output.

This is because array index starts from 0.

Highlight

star[1]

Come back to our program.

We can see here the array index starts from 1.

Hence it is giving an error.


Summary Let us fix the error.

Type 0 here 1 and 2. Click on Save


Summary Let us execute.

Come back to our terminal. Let us compile as before. execute as before.

Yes it is working.

Now we will go back to our slides.

Summary Let us summarise.

In this tutorial we learned,

Arrays.

To declare Single Dimensional Array.

To initialize Single Dimensional Array.

eg. int star[3]={4,5,6}

To add the elements of an array.

eg. sum = star[0] + star[1] + star[2];

Assignment As an assignment,

Write a program to calculate the difference of the elements stored in an array.

Slide 11

http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial

About the Spoken Tutorial Project

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 12

Spoken Tutorial Workshops

The Spoken Tutorial Project Team

Conducts workshops using spoken tutorials

Gives certificates to those who pass an online test

For more details, please write to

contact@spoken-tutorial.org

Slide 13


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 the link shown below: http://spoken-tutorial.org\NMEICT-Intro

Ashwini Patil from IIT Bombay

Thank You for joining

Contributors and Content Editors

Ashwini