Difference between revisions of "C-and-C++/C4/Working-With-Structures/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 8: Line 8:
 
|-
 
|-
 
| 00.01
 
| 00.01
|Welcome to the spoken tutorial on Loops in C and C++  
+
|Welcome to the spoken-tutorial on Structures in C and C++.
  
 
|-
 
|-
Line 15: Line 15:
  
 
|-
 
|-
| 00.09
+
| 00.08
|for loop,
+
|What is a Structure.
  
 
|-
 
|-
 
| 00.10
 
| 00.10
|while loop and
+
|Declaration of a structure
 
+
|-
+
| 00.12
+
|do…while loop.
+
  
 
|-
 
|-
 
| 00.13
 
| 00.13
| We will do this with the help of some examples.
+
|We will do this through an example
  
 
|-
 
|-
| 00.17
+
| 00.15
|We will also see some common errors and their solutions.
+
| To record this tutorial, I am using,
  
 
|-
 
|-
| 00.21
+
| 00.18
| To record this tutorial, I am using
+
|Ubuntu Operating System version 11.04,  
  
 
|-
 
|-
| 00.24
+
| 00.22
|Ubuntu Operating System version 11.04
+
| gcc and g++ Compiler version 4.6.1
  
 
|-
 
|-
|00.28
+
| 00.28
| gcc and g++ Compiler version 4.6.1 on Ubuntu.
+
|Let us start with an introduction to Structure
 
+
  
 
|-
 
|-
|00.34
+
|00.31
|Let us start with the introduction to loops.  
+
|When one or more variables are grouped under one name it is known as structure.  
  
 
|-
 
|-
|00.38
+
|00.37
|Loops are used to execute a group of instructions repeatedly.  
+
|Strucutre is used to group different data into one object.  
 +
  
 
|-
 
|-
| 00.44
+
|00.42
|Depending on the purpose they are divided into three types:
+
|It is called as compound data-type.
  
 
|-
 
|-
| 00.48
+
| 00.45
|while loop
+
|It is used to group related information together.
 
+
  
 
|-
 
|-
 
| 00.49
 
| 00.49
|do…..while loop and
+
| Now we will see the syntax to declare a structure
 
+
|-
+
| 00.51
+
|for loop
+
  
 
|-
 
|-
 
| 00.52
 
| 00.52
|Let us start with the while loop first
+
|Here the keyword struct tells the compiler that a structure is declared
 
+
|-
+
| 00.56
+
|A while loop tests the condition in the beginning
+
  
 
|-
 
|-
 
| 00.59
 
| 00.59
|The structure is  
+
|strcut_name is the name of the structure.
  
 
|-
 
|-
| 01.01
+
| 01.02
|while ( condition )
+
|eg. struct employee;
  
 
|-
 
|-
|01.03
+
| 01.04
|within the bracket statement block
+
|You can give any name. 
  
 
|-
 
|-
 
| 01.07
 
| 01.07
|Now move on to do….while loop
+
|Now we will see how to declare a structure variable.  
  
 
|-
 
|-
| 01.09
+
|01.10
|A do..while loop is executed at least once before the condition could be validated.
+
|The syntax for this is  
  
 
|-
 
|-
| 01.15
+
| 01.13
|The structure is
+
|struct struct_name and  struct_var;
  
 
|-
 
|-
 
| 01.17
 
| 01.17
|do (within the brackets) statement block
+
|struct_var is the variable of type struc_name
 
+
  
 
|-
 
|-
|01.20
+
| 01.21
|after the bracket the while ( condition )
+
|example, struct employee addr;
  
|-
 
|01.23
 
|You can see that the condition is checked at the end.
 
  
 
|-
 
|-
| 01.27
+
| 01.26
|Now,let us see an example on while loop and do...while loop
+
|addr is the variable of type employee.  
  
 
|-
 
|-
| 01.32
+
|01.30
|I have already typed the code on the editor.
+
|Let us move on to our example
  
 
|-
 
|-
| 01.35
+
|01.33
|Let me open it.  
+
|I have already typed the program on the editor so let me open it.
  
 
|-
 
|-
 
|01.37
 
|01.37
|Note that our file name is while.c.
+
|Note that our filename is structure.c.
  
 
|-
 
|-
 
|01.41
 
|01.41
|Today we are going to learn addition of first 10 numbers using while loop.
+
|In this program we will calculate the total marks of three subjects using structure
  
 
|-
 
|-
|01.47
+
|01.48
|Let me explain the code now.
+
|Let me explain the code now.  
  
 
|-
 
|-
|01.49
+
|01.51
 
|This is our header file.
 
|This is our header file.
  
 
|-
 
|-
| 01.51
+
| 01.53
|Inside the main function we have declared two integer variables x and y and initialized to 0.
+
|Here we have declared a structure as student.
  
 +
|-
 +
| 01.57
 +
| Then we have declared three integer variables as english, maths and science. 
  
  
 
|-
 
|-
| 01.59
+
| 02.03
| This is our while loop.  
+
|Variables defined under the structure are called as members of the structure.  
  
|
 
 
|-
 
|-
| 02.02
+
|02.09
| The condition of the while loop is x is less than or equal to 10.  
+
|This is our main function .
  
  
 
|-
 
|-
|02.06
+
| 02.11
|Here the value of x is added to the value of y.
+
| Here we have declared an integer variable total.
 
+
  
 
|-
 
|-
| 02.10
+
| 02.16
| The value obtained after the addition is stored in y.
+
| Now we have declared a structure variable stud, stud is the variable of type student,it is used to access and modify the structure members
  
 
|-
 
|-
| 02.15
+
| 02.28
| Then we print the value of y
+
| Here we have modified the members 
  
 
|-
 
|-
| 02.18
+
| 02.31
| Here  x is incremented.  
+
|By assigning them values as 75, 70 and 65.  
  
 
|-
 
|-
| 02.20
+
| 02.37
| That means the variable x is increased by one.
+
|Here we calculate the total of three subjects.  
  
 
|-
 
|-
| 02.25
+
| 02.41
|And this is our return statement.
+
|Then we print the result.  
  
 
|-
 
|-
| 02.27
+
| 02.44
|Now,let us execute the program.  
+
|This is our return statement.  
  
 
|-
 
|-
| 02.30
+
| 02.46
| Please open the terminal window by pressing Ctrl, Alt and T keys simultaneously on your keyboard.
+
|Now click on Save
  
 
|-
 
|-
| 02.39
+
| 02.48
|Type gcc space while dot c space hypen o space while
+
|Let us execute the program
 
+
|-
+
| 02.45
+
|Press Enter
+
  
  
 
|-
 
|-
| 02.47
+
| 02.50
|Type ./ (dot slash) while .Press Enter
+
|Please open the terminal window by pressing
  
 
|-
 
| 02.52
 
| The output is displayed.
 
  
 
|-
 
|-
 
| 02.54
 
| 02.54
| Now lets us see the working of while loop.
+
| Ctrl, Alt and T keys simultaneously on your keyboard.  
  
 
|-
 
|-
| 02.57
+
| 02.59
|Let me resize the window.
+
| To compile type gcc space structure.c space hyphen o space struct and press Enter
 
+
|-
+
| 03.00
+
|Here, first the  value of x and y is 0
+
  
  
 
|-
 
|-
| 03.04
+
| 03.12
|This is our while condition.Here we check whether x is less than or equal to 10 which means the values of x will be from 0 to 10
+
|To execute type (dot slash)./struct. Press Enter
  
 
|-
 
|-
| 03.15
+
| 03.17
|Then we  add y plus x (i.e)  0 plus 0 we get 0. We print the value of y, here we get 0. Then x is incremented which means now the value of x will be 1
+
|The output is displayed as:
  
|-
 
| 03.33
 
|Then we  will check the condition again, 1 is less than or equal to 10, if the condition is true then we will add the values, y (i.e ) 0 plus x that is 1. 0 plus 1 is 1
 
  
 
|-
 
|-
| 03.50
+
| 03.20
|We print the value as 1.Again x is incremented.
+
|Total is 210
  
 
|-
 
|-
| 03.55
+
| 03.22
|Now the value of x is 2.We check the condition again. 2 is less than or equal to 10, if the condition is true then we will add the values,(i.e ) 1 plus 2 which will give 3.
+
|Now we will execute the same program in C++
  
 
|-
 
|-
| 04.11
+
| 03.26
|We print the value as 3. Like this it will go on upto x is less than or equal to 10
+
|Come back to our program.  
|-
+
| 04.20
+
| Now, we will see the same program using do….while loop
+
  
 
|-
 
|-
| 04.24
+
| 03.28
| Here is our program
+
|I will edit the same code
  
 
|-
 
|-
| 04.26
+
| 03.30
|Note that our file name is do hypen while.c
+
|First press shift, Ctrl add S keys simultaneously on the keyboard
  
 
|-
 
|-
| 04.31
+
| 03.37
|This part is already explained in the previous program.So lets us move on to a do...while loop
+
|Now save the file with an extension .cpp
  
 
|-
 
|-
| 04.38
+
| 03.41
|Here first the body of the loop will be executed  and then the condition is checked.The value of x is added to the value of y and the value obtained after the addition is totalled y
+
| and click on save
  
 
|-
 
|-
| 04.52
+
| 03.43
|The logic is same as in while program
+
| Lets change the header file as iostream
  
 
|-
 
|-
| 04.55
+
| 03.47
|Now lets us execute the program
+
|Now include the using statement
  
 
|-
 
|-
| 04.58
+
| 03.53
|Come back to our terminal
+
|and click on save
  
 
|-
 
|-
| 05.00
+
| 03.56
|Type gcc space do hypen while dot c space hypen o space do.Press Enter
+
|Structure declaration in C++ is same as in C
  
 
|-
 
|-
| 05.08
+
| 04.01
|Type dot slash do. Press Enter
+
|So no need to change anything here
  
 
|-
 
|-
| 05.12
+
| 04.05
|We can see that the output is similiar  to our while program
+
|At the end we will replace the printf statement with the cout  statement.
  
 
|-
 
|-
| 05.16
+
| 04.12
|Now, lets us see the working of do...while loop
+
|Delete the format specifier and (backslash) \n
  
 
|-
 
|-
| 05.20
+
| 04.15
|Let me resize the window
+
|Now delete the comma
  
 
|-
 
|-
| 05.22
+
| 04.17
|Here the value x and y is 0
+
|Type two opening angle brackets.
  
 
|-
 
|-
| 05.25
+
| 04.20
|We add those values then we will get 0
+
|Here delete the closing bracket
  
 
|-
 
|-
| 05.29
+
| 04.22
|Now the value of y is 0. We print the value as 0
+
|and type two opening angle brackets
  
 
|-
 
|-
| 05.33
+
| 04.25
|Then x is incremented by 1 which means now the value of x is 1, then the condition will be checked.
+
|And within the double quotes type \n
  
 
|-
 
|-
| 05.40
+
| 04.29
| You can see that the body of loop is executed first. Anyhow if the condition is false then also we will get the values that is 0.
+
|Now click on Save
  
 
|-
 
|-
| 05.52
+
| 04.31
|Now, here we will check whether 1 is less than or equal to 10
+
|Let us execute the program
 
+
  
 
|-
 
|-
| 05.56
+
| 04.33
|The condition is true again we will add the values. Now 0 plus 1. Then we will print the value of y as 1
+
|Come back to our terminal
  
 
|-
 
|-
| 06.05
+
| 04.35
|Again x will be incremented.Now the value of x is 2. Then we check 2 is  less than or equal to 10
+
|To compile type g++ space structure.cpp space hyphen o space struct1
  
 
|-
 
|-
| 06.15
+
| 04.46
|We will go back here
+
| Here we have struct1 because we dont want to overwrite the output parameters struct for the file structure.c
  
 
|-
 
|-
| 06.17
+
| 04.55
|Then we will add the values 1 plus 2 is 3
+
|Now press Enter
  
 
|-
 
|-
| 06.20
+
| 04.57
| We print the value of y as 3
+
|To execute type (dot slash) ./struct1 Press Enter
  
|-
 
| 06.23
 
|Like this the conditions will be checked till the value of x will be less than or equal to 10
 
  
|-
 
| 06.30
 
|And this is our return statement.
 
  
 
|-
 
|-
| 06.33
+
| 05.03
|Note that here the while condition ends with the semicolon
+
|The output is displayed as:
  
 
|-
 
|-
| 06.38
+
| 05.05
|In while loop the condition does not end with the semicolon.
+
|Total is 210
  
 
|-
 
|-
| 06.43
+
| 05.08
|Now lets us see how to execute these programs in C++
+
|You can see that the output is same as our C code
  
 
|-
 
|-
| 06.48
+
| 05.12
|This is our while program in C++
+
|Now come back to our slides.
  
 
|-
 
|-
| 06.52
+
| 05.14
|The logic and implementation are same as in our C program
+
|We will summarize now. In this tutorial we learned,
  
 
|-
 
|-
| 06.56
+
| 05.18
|There are a few changes like the header file as iostream in place of stdio.h
+
|Structure.  
 
+
  
 
|-
 
|-
| 07.04
+
| 05.19
|We have included the using statement here using namespace std and here we have use the cout function in place of printf function
+
|Syntax of a structure.
  
 
|-
 
|-
| 07.16
+
| 05.20
|The structure of while loop is same as in our C program.
+
|eg. struct struct_name;
  
|-
 
| 07.21
 
|Lets us execute the program
 
  
 
|-
 
|-
| 07.23
+
| 05.23
|Come back to a terminal
+
|To access members of a structure.
  
 
|-
 
|-
| 07.25
+
| 05.25
|Let me clear the prompt
+
|Eg: stud.maths = 75;
 
+
  
 
|-
 
|-
| 07.28
+
| 05.30
|To execute type g++ space do , g++ space while dot cpp space hypen o space while1. Press Enter
+
|And to add the structure variables.  
  
 
|-
 
|-
| 07.41
+
| 05.33
|Type dot slash while1.Press Enter
+
|Eg: total = stud.english+ stud.maths + stud.science;
  
|-
 
| 07.46
 
|You can see that the output is similiar to our while program in C.
 
  
 
|-
 
|-
| 07.51
+
| 05.40
|Now let us see the do... while program in C++
+
|As an assignment,
  
 
|-
 
|-
| 07.55
+
| 05.41
|Come back to the Text editor
+
|Write a program to diplay  records of an employee.
  
 
|-
 
|-
| 07.57
+
| 05.44
|Here also there are similiar changes like the header file,the using statement and the cout function
+
|Like name, address, designation, and salary.
  
 
|-
 
|-
| 08.06
+
| 05.49
|Rest of the things are similiar
+
 
+
|-
+
| 08.09
+
|Lets us execute the program.Come back to our terminal
+
 
+
|-
+
| 08.13
+
|Type g++ space do hypen while dot cpp space hypen o space do1. Press Enter
+
 
+
|-
+
| 08.23
+
|Type dot slash do1.Press Enter
+
 
+
|-
+
| 08.26
+
|We can see that the output is similiar to our do...while program in C.
+
 
+
|-
+
| 08.32
+
|Now we will see some common errors and their solutions
+
 
+
|-
+
| 08.35
+
|Come back to our text editor
+
 
+
|-
+
| 08.38
+
|Suppose here I will not increment the value of x.Click on Save. Let us see what happens
+
 
+
|-
+
| 08.46
+
|Come back to the terminal. Let me clear the prompt
+
 
+
|-
+
| 08.50
+
|Lets us execute the program. Press the uparrow key twice. Again press the uparrow key
+
 
+
|-
+
| 09.00
+
|The output is displayed. We can see number of zeros, this is because the loop does not have the terminating condition . It is known as infinite loop
+
 
+
 
+
|-
+
| 09.13
+
|Infinite loop can cause the system to become unresponsive. It causes the program to consume all the prossess time but it can be terminated
+
 
+
|-
+
| 09.25
+
|Come back to our program let us fix the error. Type x++ and a semicolon.
+
 
+
|-
+
| 09.31
+
|Click on Save. Lte us execute again. Come back to terminal.
+
 
+
|-
+
| 09.36
+
|Press the uparrow key
+
 
+
|-
+
| 09.41
+
|Yes, it is working
+
 
+
|-
+
| 09.43
+
|This bring us to the end of the tutorial. We will move back to our slides.
+
 
+
|-
+
| 09.49
+
|Let us summarize
+
 
+
 
+
|-
+
| 09.50
+
|In this tutorial we learned,
+
 
+
|-
+
| 09.52
+
|while loop
+
 
+
|-
+
| 09.53
+
|example. while(x is less than or equal to10)
+
 
+
|-
+
| 09.57
+
|do….while loop
+
 
+
|-
+
| 09.58
+
|example. do statement block and
+
 
+
 
+
|-
+
| 10.02
+
|while condition at the end
+
 
+
|-
+
| 10.04
+
|As an assignment
+
 
+
|-
+
| 10.06
+
|Write a program to print the following using for loops
+
 
+
|-
+
| 10.10
+
|0 to 9
+
 
+
|-
+
| 10.12
+
|The syntax of the for loop is
+
 
+
|-
+
| 10.15
+
|for (variable initialization; variable condition;and variable increment or decrement)
+
 
+
|-
+
| 10.24
+
|And here will be the body of the loop
+
 
+
 
+
 
+
 
+
 
+
|-
+
| 10.27
+
 
|Watch the video available at the link shown below
 
|Watch the video available at the link shown below
  
 
|-
 
|-
| 10.30
+
| 05.52
 
|It summarizes the Spoken Tutorial project  
 
|It summarizes the Spoken Tutorial project  
  
 
|-
 
|-
| 10.32
+
| 05.54
 
|If you do not have good bandwidth, you can download and watch it  
 
|If you do not have good bandwidth, you can download and watch it  
  
 
|-
 
|-
| 10.36
+
| 05.59
 
|The Spoken Tutorial Project Team  
 
|The Spoken Tutorial Project Team  
  
 
|-
 
|-
| 10.38
+
| 06.01
 
|Conducts workshops using spoken tutorials  
 
|Conducts workshops using spoken tutorials  
  
 
|-
 
|-
| 10.41
+
|06.04
 
|Gives certificates to those who pass an online test  
 
|Gives certificates to those who pass an online test  
  
 
|-
 
|-
| 10.45
+
| 06.08
 
|For more details, please write to, contact@spoken-tutorial.org  
 
|For more details, please write to, contact@spoken-tutorial.org  
  
 
|-
 
|-
| 10.50
+
| 06.15
 
|Spoken Tutorial Project is a part of Talk to a Teacher project  
 
|Spoken Tutorial Project is a part of Talk to a Teacher project  
  
 
|-
 
|-
| 10.55
+
| 06.18
 
|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
  
 
|-
 
|-
| 11.01
+
| 06.25
 
|More information on this Mission is available at the link shown below
 
|More information on this Mission is available at the link shown below
  
 
|-
 
|-
| 11.05
+
| 06.29
|This script as been contributed by Dhawal Goyal. This is Ashwini Patil from IIT Bombay signing off  
+
| This is Ashwini Patil from IIT Bombay signing off  
  
 
|-
 
|-
| 11.12
+
| 06.33
|Thank You for joining.
+
|Thank You for watching.

Revision as of 11:58, 22 November 2013

Time Narration


00.01 Welcome to the spoken-tutorial on Structures in C and C++.
00.06 In this tutorial we will learn,
00.08 What is a Structure.
00.10 Declaration of a structure
00.13 We will do this through an example
00.15 To record this tutorial, I am using,
00.18 Ubuntu Operating System version 11.04,
00.22 gcc and g++ Compiler version 4.6.1
00.28 Let us start with an introduction to Structure
00.31 When one or more variables are grouped under one name it is known as structure.
00.37 Strucutre is used to group different data into one object.


00.42 It is called as compound data-type.
00.45 It is used to group related information together.
00.49 Now we will see the syntax to declare a structure
00.52 Here the keyword struct tells the compiler that a structure is declared
00.59 strcut_name is the name of the structure.
01.02 eg. struct employee;
01.04 You can give any name.
01.07 Now we will see how to declare a structure variable.
01.10 The syntax for this is
01.13 struct struct_name and struct_var;
01.17 struct_var is the variable of type struc_name
01.21 example, struct employee addr;


01.26 addr is the variable of type employee.
01.30 Let us move on to our example
01.33 I have already typed the program on the editor so let me open it.
01.37 Note that our filename is structure.c.
01.41 In this program we will calculate the total marks of three subjects using structure
01.48 Let me explain the code now.
01.51 This is our header file.
01.53 Here we have declared a structure as student.
01.57 Then we have declared three integer variables as english, maths and science.


02.03 Variables defined under the structure are called as members of the structure.
02.09 This is our main function .


02.11 Here we have declared an integer variable total.
02.16 Now we have declared a structure variable stud, stud is the variable of type student,it is used to access and modify the structure members
02.28 Here we have modified the members
02.31 By assigning them values as 75, 70 and 65.
02.37 Here we calculate the total of three subjects.
02.41 Then we print the result.
02.44 This is our return statement.
02.46 Now click on Save
02.48 Let us execute the program


02.50 Please open the terminal window by pressing


02.54 Ctrl, Alt and T keys simultaneously on your keyboard.
02.59 To compile type gcc space structure.c space hyphen o space struct and press Enter


03.12 To execute type (dot slash)./struct. Press Enter
03.17 The output is displayed as:


03.20 Total is 210
03.22 Now we will execute the same program in C++
03.26 Come back to our program.
03.28 I will edit the same code
03.30 First press shift, Ctrl add S keys simultaneously on the keyboard
03.37 Now save the file with an extension .cpp
03.41 and click on save
03.43 Lets change the header file as iostream
03.47 Now include the using statement
03.53 and click on save
03.56 Structure declaration in C++ is same as in C
04.01 So no need to change anything here
04.05 At the end we will replace the printf statement with the cout statement.
04.12 Delete the format specifier and (backslash) \n
04.15 Now delete the comma
04.17 Type two opening angle brackets.
04.20 Here delete the closing bracket
04.22 and type two opening angle brackets
04.25 And within the double quotes type \n
04.29 Now click on Save
04.31 Let us execute the program
04.33 Come back to our terminal
04.35 To compile type g++ space structure.cpp space hyphen o space struct1
04.46 Here we have struct1 because we dont want to overwrite the output parameters struct for the file structure.c
04.55 Now press Enter
04.57 To execute type (dot slash) ./struct1 Press Enter


05.03 The output is displayed as:
05.05 Total is 210
05.08 You can see that the output is same as our C code
05.12 Now come back to our slides.
05.14 We will summarize now. In this tutorial we learned,
05.18 Structure.
05.19 Syntax of a structure.
05.20 eg. struct struct_name;


05.23 To access members of a structure.
05.25 Eg: stud.maths = 75;
05.30 And to add the structure variables.
05.33 Eg: total = stud.english+ stud.maths + stud.science;


05.40 As an assignment,
05.41 Write a program to diplay records of an employee.
05.44 Like name, address, designation, and salary.
05.49 Watch the video available at the link shown below
05.52 It summarizes the Spoken Tutorial project
05.54 If you do not have good bandwidth, you can download and watch it
05.59 The Spoken Tutorial Project Team
06.01 Conducts workshops using spoken tutorials
06.04 Gives certificates to those who pass an online test
06.08 For more details, please write to, contact@spoken-tutorial.org
06.15 Spoken Tutorial Project is a part of Talk to a Teacher project
06.18 It is supported by the National Mission on Education through ICT, MHRD, Government of India
06.25 More information on this Mission is available at the link shown below
06.29 This is Ashwini Patil from IIT Bombay signing off
06.33 Thank You for watching.

Contributors and Content Editors

Kavita salve, Krupali, PoojaMoolya, Pratik kamble, Sandhya.np14, Sneha