Difference between revisions of "C-and-C++/C3/Arrays/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(2 intermediate revisions by the same user not shown)
Line 26: Line 26:
 
Initialization of an '''array'''.
 
Initialization of an '''array'''.
  
We will do this with the help of an example.
+
Few examples on array.
  
We will also explain some common errors and their solutions.
+
We will also see some common errors and their solutions.
  
 
|-
 
|-
Line 47: Line 47:
  
 
The first element is stored at index''' 0.'''
 
The first element is stored at index''' 0.'''
 
'''<nowiki>eg. pen[5];</nowiki>'''
 
 
'''Here, we are declaring a character array which contains 5 elements.'''
 
 
'''<nowiki>pen[0] to pen[4].</nowiki>'''
 
  
 
There are three types of '''arrays:'''
 
There are three types of '''arrays:'''
Line 64: Line 58:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 6
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 6
| 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 '''single dimensional array.'''
+
| 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 a '''single dimensional array.'''
  
 
The Syntax for this is:  
 
The Syntax for this is:  
  
'''<nowiki>data-type array-name[size];</nowiki>'''
+
'''<nowiki>data-type (name of the array) and size;</nowiki>'''
  
eg.'''<nowiki> char star[5];</nowiki>'''
+
eg.('''<nowiki> char star[5];)</nowiki>'''
 +
Here we have declared an integer array star which contains 5 elements.
 +
The array index will start from star0 to star 4.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 7
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 7
| 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 initialize the array.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|We saw the declaration of an array now we will see the initialize of an array.
  
 
The '''Syntax''' for this is:  
 
The '''Syntax''' for this is:  
  
'''<nowiki>data-type array-name[size]={values};</nowiki>'''
+
'''<nowiki>data-type (name of the array)array-name[size]={elements};</nowiki>'''
  
 
eg. '''<nowiki>int star[3]={1,2,3};</nowiki>'''
 
eg. '''<nowiki>int star[3]={1,2,3};</nowiki>'''
 +
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.
  
 
|-
 
|-
 
| 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 '''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 move to the examples.
  
 
I have already typed the program on the editor.
 
I have already typed the program on the editor.
  
So, I will open it.
+
So, 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 '''array.c'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|Please note that our file name is '''array.c'''
  
 
|-
 
|-
 
| 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;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this program, we will calculate the sum of the elements stored in an array.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this program, we will calculate the sum of the elements stored in an array.
 +
Let me explain the code now.
  
 
|-
 
|-
Line 120: Line 118:
  
 
| 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 and initialized an '''array''' '''star''' with size 3.
 
| 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 and initialized an '''array''' '''star''' with size 3.
 
+
The elements of the array are 4, 5 and 6.
 
|-
 
|-
 
| 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
  
 
'''int sum;'''
 
'''int sum;'''
| 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 a '''variable''' sum.
+
| 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 an '''integer variable sum'''.
  
 
|-
 
|-
Line 131: Line 129:
  
 
'''<nowiki>sum = star[0] + star[1] + star[2]; </nowiki>'''
 
'''<nowiki>sum = star[0] + star[1] + star[2]; </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 add the values and store the result in '''sum'''.
+
| 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 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.
 
|-
 
|-
 
| 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
  
 
'''printf("The sum is %d\n",sum); '''
 
'''printf("The sum is %d\n",sum); '''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we print the result.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we print the sum.
  
 
|-
 
|-
Line 155: Line 155:
 
|-
 
|-
 
| 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 162: Line 162:
 
Type
 
Type
  
'''gcc array.c -o arr'''
+
'''gcc array.c -o array'''
  
 
'''./arr'''
 
'''./arr'''
 
| 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 array.c -o arr'''
+
'''gcc space array.c space -o space array'''
 
+
  
 +
'''Press Enter'''
 
To execute type,
 
To execute type,
  
'''./arr'''
+
'''./array'''
 +
'''Press Enter'''
  
 
|-
 
|-
Line 180: Line 181:
 
| 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,  
 
| 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.'''
+
'''The sum is 15.'''
  
 
|-
 
|-
Line 194: Line 195:
  
 
<nowiki>int star[3]=1,2,3;</nowiki>
 
<nowiki>int star[3]=1,2,3;</nowiki>
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Suppose here, we miss the curly brackets.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Suppose here at line no.4, we miss the curly brackets.
  
 
|-
 
|-
Line 202: Line 203:
 
|-
 
|-
 
| 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;"|  
| 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.
+
| 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 what happens.
  
 
|-
 
|-
 
| 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;"| Come back to our terminal.
  
 
Let us compile as before.
 
Let us compile as before.
 
Let us execute as before.
 
  
 
|-
 
|-
Line 216: Line 215:
  
 
Error
 
Error
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We see an error.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We see errors.
 +
 
 +
Invalid initializer and Expected identifier or (bracket) '(' before numeric constants.
  
Invalid initializer and Expected identifier or '(' before numeric constant.
 
  
 
|-
 
|-
 
| 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;"| { }
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| As arrays must be initialized within curly brackets.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is because arrays must be initialized within curly brackets.
 +
 
 +
|-
 +
| 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;"| Come back to our program.
 +
Let us fix the error.
 +
Type the curly bracket here at line no. 4.
 +
Now click on '''Save'''
 +
 
 +
|-
 +
| 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;"| Let us execute.
 +
Come back to our terminal.
 +
Let us compile as before.
 +
Let us execute as before.
 +
Yes it is working.
  
 
|-
 
|-
 
| 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;"|  
| 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++.
+
| 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 execute the same program in C++.
  
 
|-
 
|-
Line 237: Line 252:
  
 
|-
 
|-
| 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;"|
 +
Now save the file with an extension '''.cpp'''
  
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 press '''shift+ctrl and s''' keys simultaneously on your keyboard.
| 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,
+
  
Click on '''Save as''' option,
+
And
 
+
click on '''Save'''
Save the file with '''.cpp''' extension.
+
  
 
|-
 
|-
Line 256: Line 270:
  
 
'''using namespace std;'''
 
'''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;"| 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;"| Now include the '''using''' statement.
 
+
The declaration and initialization of an array is same in C++.
 +
Hence no need to change anything here.
 
|-
 
|-
 
| 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;"| Type
Line 263: Line 278:
 
'''<nowiki>cout <<</nowiki>'''
 
'''<nowiki>cout <<</nowiki>'''
 
| 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;"| 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:none;padding:0.097cm;"| Delete
 +
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| 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.
 +
  
 
|-
 
|-
 
| 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;"| 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;"| 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;"| Now click on '''Save'''
  
 
|-
 
|-
 
| 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;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Lets execute the program.
+
| 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.
  
 
|-
 
|-
 
| 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;"| Come back to our terminal
  
 
|-
 
|-
Line 286: Line 310:
 
| 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,
  
g++ array.cpp -o arr
+
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
 
To execute
  
./arr
+
./array1
 +
press '''Enter'''
  
 
|-
 
|-
Line 296: Line 325:
  
 
'''Output'''
 
'''Output'''
| 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,
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The output is displayed as,
  
'''The sum is 6.'''
+
'''The sum is 15.'''
 +
We can see that it 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;"| Errors
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Errors
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let see another common error  
+
| 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 another common error  
 +
Come back to our program.
  
 
|-
 
|-
Line 311: Line 342:
 
<nowiki>star[1] + star[2] + star[3];</nowiki>
 
<nowiki>star[1] + star[2] + star[3];</nowiki>
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Suppose here, at line number 7  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Suppose here, at line number 7  
 +
I will type
  
<nowiki>We type star[1] + star[2] + star[3];</nowiki>
+
<nowiki>star[1] + star[2] + star[3];</nowiki>
 +
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;"| Let us execute and see.
+
| 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 the terminal.
+
  
 +
Come back to our terminal.
 +
Let me clear the prompt.
 
|-
 
|-
 
| 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;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us compile and execute as before.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us compile as before.
 
+
Let us execute as before.
 
|-
 
|-
 
| 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
  
 
Output
 
Output
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The output is displayed as:
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We get an unexpected output.
 
+
The sum is ....
+
 
+
We get an unexected output.
+
  
 
This is because array index starts from 0.
 
This is because array index starts from 0.
Line 340: Line 369:
  
 
'''<nowiki>star[1]</nowiki>'''
 
'''<nowiki>star[1]</nowiki>'''
| 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 editor
+
| 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.
 
+
We can see here the array index starts from 1.
Here the index starts from 1
+
  
 
Hence it is giving an error.
 
Hence it is giving an error.
 +
  
 
|-
 
|-
 
| 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 tutorial we learned,
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us fix the error.
 +
Type 0 here 1 and 2.
 +
Click on '''Save'''
 +
 
 +
 
 +
 
 +
|-
 +
| 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;"| 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.
 +
|-
 +
| 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;"| Let us summarise.
 +
In this tutorial we learned,
  
 
Arrays.
 
Arrays.
Line 356: Line 404:
 
To initialize Single Dimensional Array.
 
To initialize Single Dimensional Array.
  
eg. '''<nowiki>int star[3]={1,2,3}</nowiki>'''
+
eg. '''<nowiki>int star[3]={4,5,6}</nowiki>'''
 +
 
 +
To add the elements of an array.
 +
 
 +
eg. sum = star[0] + star[1] + star[2];
  
 
|-
 
|-
Line 399: Line 451:
 
It is supported by the National Mission on Education through ICT, MHRD, Government of India
 
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
+
More information on this Mission is available at the link shown below: http://spoken-tutorial.org\NMEICT-Intro
  
 
|-
 
|-

Latest revision as of 17:27, 6 February 2014

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