Upcoming SlideShare
×

# Foundations of Programming Part II

330 views

Published on

Published in: Education, Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
330
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
4
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Foundations of Programming Part II

1. 1. Jayesh Joy Meher Anand
2. 2. Recap <ul><li>Display using cout<< , read input using cin>> </li></ul><ul><li>Declare variables of different types </li></ul><ul><li>Control flow: </li></ul><ul><ul><li>if, else if, else </li></ul></ul><ul><ul><li>switch case </li></ul></ul><ul><ul><li>for </li></ul></ul><ul><ul><li>while </li></ul></ul>
3. 3. Arrays <ul><li>Collection of variables of the same data type </li></ul><ul><li>Saves trouble of remembering ‘n’ number of related variables of the same type </li></ul><ul><li>Name and size of array given during declaration </li></ul><ul><li>Eg:- int apples[10] – Creates a set of 10 integers. The set is called apples </li></ul><ul><li>Indices vary between 0 and n-1. </li></ul><ul><li>apples[0], apples[1] … apples[8], apples[9] </li></ul>
4. 4. Multidimensional arrays <ul><li>2-D arrays are similar to matrices in mathematics </li></ul><ul><li>Referencing as follows: m [x] [y] </li></ul><ul><li>x -> Row no. </li></ul><ul><li>y -> Column no. </li></ul><ul><li>Declaration:- int m [10][20] ; </li></ul><ul><ul><li>10 rows, 20 columns </li></ul></ul>
5. 5. Example
6. 6. Structures <ul><li>Limitations of array – set of variables of same type </li></ul><ul><li>Structures used to overcome this limitation </li></ul><ul><li>Eg:- struct student { </li></ul><ul><li>string name; </li></ul><ul><li>int age; </li></ul><ul><li>float marks[10]; </li></ul><ul><li>}; </li></ul>
7. 7. Structures (contd…) <ul><li>Example declaration:- student stdnt1; </li></ul><ul><li>Access variables using ‘.’ operator </li></ul><ul><li>Ex:- stdnt1.name , stdnt1.marks[2] </li></ul>
8. 8. Functions <ul><li>Segment of code designed to accomplish a certain task </li></ul><ul><li>Similar to mathematical functions </li></ul><ul><li>Takes in some input, does some computation and gives some output </li></ul><ul><li>Why use functions? </li></ul><ul><ul><li>Avoid repetitions </li></ul></ul><ul><ul><li>Easier to read and fix </li></ul></ul>
9. 9. Functions (contd…) <ul><li>A function may take in parameters and may return a value </li></ul><ul><li>The type of each parameter has to be specified </li></ul><ul><li>The type of the return value must also be specified </li></ul>
10. 10. Function declarations <ul><li>int func ( int a, float b ) </li></ul><ul><li>{ </li></ul><ul><li>//Some computation to be performed here </li></ul><ul><li>return x; //’x’ is of type integer </li></ul><ul><li>} </li></ul><ul><li>void return type is used when the function does not return anything </li></ul><ul><li>void print_hello () </li></ul><ul><li>{ cout<<“HELLO”; } </li></ul>
11. 11. Example
12. 12. Principles of Recursion <ul><li>Analogous to recurrence relations in mathematics </li></ul><ul><li>Solution depends on solutions to smaller instances of the same problem </li></ul><ul><li>It also means that a function can call itself </li></ul><ul><li>Ex:- Factorial (n) = ( Factorial (n-1) ) * n </li></ul><ul><li>Here Factorial (n) is a problem and Factorial (n-1) is a smaller sub-problem </li></ul>
13. 13. Example