Foundations of Programming Part II
Click here to load reader
-
Upload
vnit-acm-student-chapter -
Category
Education
-
view
214 -
download
0
Transcript of Foundations of Programming Part II
![Page 1: Foundations of Programming Part II](https://reader038.fdocuments.us/reader038/viewer/2022100603/55945eb81a28ab0b4e8b45b8/html5/thumbnails/1.jpg)
Jayesh JoyMeher Anand
![Page 2: Foundations of Programming Part II](https://reader038.fdocuments.us/reader038/viewer/2022100603/55945eb81a28ab0b4e8b45b8/html5/thumbnails/2.jpg)
Re c a p
Display using cout<< , read input using cin>> Declare variables of different types Control flow:
if, else if, else switch case for while
![Page 3: Foundations of Programming Part II](https://reader038.fdocuments.us/reader038/viewer/2022100603/55945eb81a28ab0b4e8b45b8/html5/thumbnails/3.jpg)
Ar r a y s
Collection of variables of the same data type Saves trouble of remembering ‘n’ number of
related variables of the same type Name and size of array given during
declaration Eg:- int apples[10] – Creates a set of 10
integers. The set is called apples Indices vary between 0 and n-1. apples[0], apples[1] … apples[8], apples[9]
![Page 4: Foundations of Programming Part II](https://reader038.fdocuments.us/reader038/viewer/2022100603/55945eb81a28ab0b4e8b45b8/html5/thumbnails/4.jpg)
Mu l t i d i me n s i o n a l a r r a y s
2-D arrays are similar to matrices in mathematics
Referencing as follows: m [x] [y] x -> Row no. y -> Column no. Declaration:- int m [10][20] ;
10 rows, 20 columns
![Page 5: Foundations of Programming Part II](https://reader038.fdocuments.us/reader038/viewer/2022100603/55945eb81a28ab0b4e8b45b8/html5/thumbnails/5.jpg)
Ex a mp l e
![Page 6: Foundations of Programming Part II](https://reader038.fdocuments.us/reader038/viewer/2022100603/55945eb81a28ab0b4e8b45b8/html5/thumbnails/6.jpg)
S t r u c t u r e s
Limitations of array – set of variables of same type
Structures used to overcome this limitation Eg:- struct student {
string name;int age;float marks[10];
};
![Page 7: Foundations of Programming Part II](https://reader038.fdocuments.us/reader038/viewer/2022100603/55945eb81a28ab0b4e8b45b8/html5/thumbnails/7.jpg)
S t r u c t u r e s ( c o n t d …)
Example declaration:- student stdnt1; Access variables using ‘.’ operator Ex:- stdnt1.name , stdnt1.marks[2]
![Page 8: Foundations of Programming Part II](https://reader038.fdocuments.us/reader038/viewer/2022100603/55945eb81a28ab0b4e8b45b8/html5/thumbnails/8.jpg)
F u n c t i o n s
Segment of code designed to accomplish a certain task
Similar to mathematical functions Takes in some input, does some computation
and gives some output Why use functions?
Avoid repetitions Easier to read and fix
![Page 9: Foundations of Programming Part II](https://reader038.fdocuments.us/reader038/viewer/2022100603/55945eb81a28ab0b4e8b45b8/html5/thumbnails/9.jpg)
F u n c t i o n s ( c o n t d …)
A function may take in parameters and may return a value
The type of each parameter has to be specified The type of the return value must also be
specified
![Page 10: Foundations of Programming Part II](https://reader038.fdocuments.us/reader038/viewer/2022100603/55945eb81a28ab0b4e8b45b8/html5/thumbnails/10.jpg)
F u n c t i o n d e c l a r a t i o n sint func ( int a, float b )
{
//Some computation to be performed here
return x; //’x’ is of type integer
}
void return type is used when the function does not return anythingvoid print_hello (){ cout<<“HELLO”; }
![Page 11: Foundations of Programming Part II](https://reader038.fdocuments.us/reader038/viewer/2022100603/55945eb81a28ab0b4e8b45b8/html5/thumbnails/11.jpg)
Ex a mp l e
![Page 12: Foundations of Programming Part II](https://reader038.fdocuments.us/reader038/viewer/2022100603/55945eb81a28ab0b4e8b45b8/html5/thumbnails/12.jpg)
Pr i n c i p l e s o f Re c u r s i o n
Analogous to recurrence relations in mathematics
Solution depends on solutions to smaller instances of the same problem
It also means that a function can call itself Ex:- Factorial (n) = ( Factorial (n-1) ) * n Here Factorial (n) is a problem and Factorial
(n-1) is a smaller sub-problem
![Page 13: Foundations of Programming Part II](https://reader038.fdocuments.us/reader038/viewer/2022100603/55945eb81a28ab0b4e8b45b8/html5/thumbnails/13.jpg)
Ex a mp l e