Difference between revisions of "C-and-C++/C3/Arrays/English"
(Created page with ''''Title of script''': Arryas in C and C++ '''Author: '''Ashwini Patil '''Keywords: arrays, single dimensional, multi dimensional, video tutorial() ''' {| style="border-spac…') |
|||
Line 4: | Line 4: | ||
'''Keywords: arrays, single dimensional, multi dimensional, video tutorial() ''' | '''Keywords: arrays, single dimensional, multi dimensional, video tutorial() ''' | ||
+ | |||
{| style="border-spacing:0;" | {| style="border-spacing:0;" | ||
− | + | | style="border-top:0.002cm solid #000000;border-bottom:0.002cm solid #000000;border-left:0.002cm solid #000000;border-right:none;padding:0.097cm;"| <center>'''Visual Cue'''</center> | |
− | + | | style="border:0.002cm solid #000000;padding:0.097cm;"| <center>'''Narration'''</center> | |
|- | |- |
Revision as of 13:22, 3 May 2013
Title of script: Arryas in C and C++
Author: Ashwini Patil
Keywords: arrays, single dimensional, multi dimensional, video tutorial()
|
|
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. We will do this with the help of an example. We will also explain 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. eg. pen[5]; Here, we are declaring a character array which contains 5 elements. pen[0] to pen[4]. There are three types of arrays:
We will be discussing single dimensional array in this tutorial. |
Slide 6 | Let us see how to declare single dimensional array.
The Syntax for this is: data-type array-name[size]; eg. char star[5]; |
Slide 7 | Let us see how to initialize the array.
The Syntax for this is: data-type array-name[size]={values}; eg. int star[3]={1,2,3}; |
On the editor | Now let us see how to declare an 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 array.c |
In this program, we will calculate the sum of the elements stored in an array. | |
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. |
Highlight
int sum; |
Then we have declared a variable sum. |
Highlight
sum = star[0] + star[1] + star[2]; |
Here we add the values and store the result in sum. |
Highlight
printf("The sum is %d\n",sum); |
Then we print 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 array.c -o arr
./arr |
Highlight
Output |
Here the output is displayed as,
The sum is 6. |
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, we miss the curly brackets. |
Click on Save | Click on Save. |
Let us execute. | |
On the terminal | Come back to the terminal.
Let us compile as before. Let us execute as before. |
Highlight
Error |
We see an error.
Invalid initializer and Expected identifier or '(' before numeric constant. |
{ } | As arrays must be initialized within curly brackets. |
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 |
Let us change the header file as iostream. |
Type
using namespace std; |
Include the using statement. |
Type
cout << |
Now replace the printf statement with the cout statement. |
Click on Save | click on Save |
Lets execute the program. | |
On the terminal | Come back to the terminal |
Type
g++ array.cpp -o arr Type ./arr |
To compile type,
g++ array.cpp -o arr To execute ./arr |
Highlight
Output |
Here the output is displayed as,
The sum is 6. |
Errors | Now let see another common error |
At line 7
Type: star[1] + star[2] + star[3]; |
Suppose here, at line number 7
We type star[1] + star[2] + star[3]; |
On the terminal | Let us execute and see.
Come back to the terminal. |
Let us compile and execute as before. | |
Highlight
Output |
The output is displayed as:
The sum is .... We get an unexected output. This is because array index starts from 0. |
Highlight
star[1] |
Come back to the editor
Here the index starts from 1 Hence it is giving an error. |
Summary | In this tutorial we learned,
Arrays. To declare Single Dimensional Array. To initialize Single Dimensional Array. eg. int star[3]={1,2,3} |
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
|
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 |
Ashwini Patil from IIT Bombay
Thank You for joining |