Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016
Increment/decrement operators Incrementing (adding 1 to) and decrementing (subtracting 1 from) a variable are so common that they have their own operators in C. There are actually two version of each operator -- a prefix version and a postfix version.
Increment/decrement operators (Cont…) #include using namespace std; int main() { int x = 5, y = 5; cout << x << " " << y << endl; cout << ++x << " " << --y << endl; // prefix cout << x << " " << y << endl; cout << x++ << " " << y-- << endl; // postfix cout << x << " " << y << endl; return 0; }
Increment/decrement operators (Cont…)
Using Predefined Functions in a Program Function (Subprogram): set of instructions When activated, it accomplishes a task main executes when a program is run Other functions execute only when called C++ includes a wealth of functions Predefined functions are organized as a collection of libraries called header files
Using Predefined Functions in a Program (Cont.) Header file may contain several functions To use a predefined function, you need the name of the appropriate header file You also need to know: 1.Function name 2.Number of parameters required 3.Type of each parameter 4.What the function is going to do
Using Predefined Functions in a Program (Cont.) To use pow (power), include cmath Two numeric parameters Syntax: pow(x,y) = x y x and y are the arguments or parameters In pow(2,3), the parameters are 2 and 3
Examples
Examples (Cont…)
Examples #include using namespace std; float main() { int i, j, k; float average ; cout<< "Enter 3 numbers \n"; cin>> i >> j >> k; average = (i+j+k) / 3; cout<< "Average = "<< average << "\n"; return 0; } #include using namespace std; int main() { int width, length, area ; cout<< "Please Enter width & Length of a Rectangle \n"; cin>> width >> length; area = width * length; cout<< "Area of rectangle = " << area << "\n"; return 0 ; }
Examples (Cont…) #include using namespace std; int main() { const int a=5; int b=2; b = a + b; cout<< b << endl; return 0; }
#include using namespace std; int main() { int a=5, b=2 ; a += b; // a = a + b; cout<< a << "\n"; return 0; } #include using namespace std; int main() { int a, b, c, d; a = * (3 + 4); cout<< "Value of a = " << a << endl; b = 5 * % 4; cout<< "Value of b = " << b << endl; c = 5 * 2 % (7 - 4); cout<< "Value of c = " << c << endl; d = 2 * 5 * * 5 + 7; cout<< "Value of d = " << d << endl; return 0; }
#include using namespace std; int main() { int x; cout<< "Enter the value of X \n"; cin>>x; x+=5; cout<< x << "\n"; return 0; } #include using namespace std; int main() { int x; cout<< "Enter the value of X \n"; cin>>x; x*=10; cout<< x << "\n"; return 0; }
#include using namespace std; double main() { double x; cout<< “Enter the value of X \n"; cin>>x; x-=5; cout<< x << "\n"; return 0; } #include using namespace std; double main() { double x; cout<< “Enter the value of X \n"; cin>>x; x/=5; cout<< x << "\n"; return 0; }
#include using namespace std; int main() { int x; cout<< "Enter the number you need to increment it \n"; cin>>x; x++; cout<< x << "\n"; return 0; } #include using namespace std; int main() { int x, y = 10; cout<< "Enter the value of X \n"; cin>>x; cout<< ++x+y << "\n" ; y++; cout<< x << "\n" << y << "\n"; return 0; }
#include using namespace std; int main() { int x, y; cout<< "Enter the values of the two numbers \n"; cin>> x >> y; cout<< x++ + y << "\n"; cout<< x << "\n" << y << "\n"; return 0; } #include using namespace std; int main() { int x; cout<< "Enter the number you need to decrement it \n"; cin>>x; x--; cout<< x << "\n"; return 0; }
#include using namespace std; int main() { int x, y; cout<< "Enter the values of the two numbers \n"; cin>> x >> y; cout<< x-- + y << "\n"; cout<< x << "\n" << y << "\n" ; return 0; } #include using namespace std; int main() { int x, y; cout<< "Enter the values of the two numbers \n"; cin>> x >> y; cout<< --x * y << "\n"; cout<< x << "\n" << y << "\n" ; return 0; }
Examples (Cont…) #include using namespace std; int main() { int number1, sum; float number2; cout<< "Enter the values of number1 & number2 = " ; cin>> number1>> number2; sum = number1 + number2; cout<< " Sum = " << sum << endl; return 0; } #include using namespace std; int main() { float sum =50.0, count=5.0; int avg; avg = sum / count; cout<< avg << "\n"; return 0; }