ge 211 king saud university dr. ahmed telba. #include using namespace std; int main() { int i, j;...

50
GE 211 King Saud university Dr. Ahmed Telba

Upload: vivien-short

Post on 31-Dec-2015

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

GE 211 King Saud university

Dr. Ahmed Telba

Page 2: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout
Page 3: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

#include <iostream> using namespace std; int main() { int i, j; double x, y; cout << " please inter First output i,j,x,y "<< endl; cin >> i >> j >> x >> y; cout << i << ',' << j << ',' << x << ',' << y << endl; cout << " please inter Second output i,j,x,y "<< endl; cin >> x >> y >> i >> j; cout << "Second output" << endl; cout << i << ',' << j << ',' << x << ',' << y << endl;system("pause"); return 0;}

Page 4: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

For loop

• for ( variable initialization; condition; variable update ) {

• Code to execute while the condition is true• }

Page 5: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

For loop #include <iostream>

using namespace std; // So the program can see cout and endl

int main(){ // The loop goes while x < 10, and x increases by one every loop for ( int x = 0; x < 10; x++ ) { // Keep in mind that the loop condition checks // the conditional statement before it loops again. // consequently, when x equals 10 the loop breaks. // x is updated before the condition is checked. cout<< x <<endl; }system("pause"); }

Page 6: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

• //Example:• #include <iostream>

• using namespace std; // So the program can see cout and endl

• int main()• {• // The loop goes while x < 10, and x increases by one every loop• for ( int x = 0; x < 10; x++ ) {• // Keep in mind that the loop condition checks • // the conditional statement before it loops again.• // consequently, when x equals 10 the loop breaks.• // x is updated before the condition is checked. • cout<< x <<endl;• }• system("pause");• }

Page 7: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

While loop • #include <iostream>

• using namespace std; // So we can see cout and endl

• int main()• { • int x = 0; // Don't forget to declare variables• • while ( x < 10 ) { // While x is less than 10 • cout<< x <<endl;• x++; // Update x so the condition can be met eventually• }• system(“pause”);• }

Page 8: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

Do while • // number echoer

• #include <iostream>• using namespace std;

• int main ()• {• unsigned long n;• do {• cout << "Enter number (0 to end): ";• cin >> n;• cout << "You entered: " << n << "\n";• } while (n != 0);• return 0;• }

Page 9: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

Do while • #include <iostream>

• using namespace std;

• int main()• {• int x;

• x = 0;• do {• // "Hello, world!" is printed at least one time• // even though the condition is false• cout<<"Hello, world!\n";• } while ( x != 0 );• system("pause");• }

Page 10: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

If else • #include <iostream>

• using namespace std;•• int main() // Most important part of the program!• {• int age; // Need a variable...• • cout<<"Please input your age: "; // Asks for age• cin>> age; // The input is put in age• cin.ignore(); // Throw away enter• if ( age < 100 ) { // If the age is less than 100• cout<<"You are pretty young!\n"; // Just to show you it works...• }• else if ( age == 100 ) { // I use else just to show an example • cout<<"You are old\n"; // Just to show you it works...• }• else {• cout<<"You are really old\n"; // Executed if no other statement is• }• system(“pause”);• }

Page 11: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

switch example if-else equivalentswitch (x) { case 1: cout << "x is 1"; break; case 2: cout << "x is 2"; break; default: cout << "value of x unknown"; }

if (x == 1) {cout << "x is 1"; } else if (x == 2) { cout << "x is 2"; } else { cout << "value of x unknown"; }

Page 12: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout
Page 13: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout
Page 14: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

abs absolute value

acos arc cosine

asin arc sine

atan arc tangent

atan2 arc tangent, using signs to determine quadrants

ceil the smallest integer not less than a certain value

cos cosine

cosh hyperbolic cosine

div returns the quotient and remainder of a division

exp returns "e" raised to a given power

fabs absolute value for floating-point numbers

floor returns the largest integer not greater than a given value

fmod returns the remainder of a division

frexp decomposes a number into scientific notation

labs absolute value for long integers

ldexp computes a number in scientific notation

Page 15: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

ldiv returns the quotient and remainder of a division, in long integer form

log natural logarithm (to base e)

log10 common logarithm (to base 10)

modf decomposes a number into integer and fractional parts

pow returns a given number raised to another number

round rounds given number to the nearest integer

sin sine

sinh hyperbolic sine

sqrt square root

tan tangent

tanh hyperbolic tangent

Page 16: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

C++ Operators:

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. C++ is rich in built-in operators and provides following type of operators:

Arithmetic Operators ( +, -, \, *, ++, --)

Relational (personal) Operators (==, !=, >. <, >=, <=)

Logical Operators (&&, ||, ! )

Bitwise Operators (& |, ^, ~, <<, >>) Assignment Operators (=, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=) Misc Operators ( sizeof, & cast, comma, conditional etc.)

Page 17: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

Switch case

• Switch

Page 18: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

#include <iostream>using namespace std; int main (){ // local variable declaration: char grade = 'D';

switch(grade) { case 'A' : cout << "Excellent!" << endl; break; case 'B' : case 'C' : cout << "Well done" << endl; break; case 'D' : cout << "You passed" << endl; break; case 'F' : cout << "Better try again" << endl; break; default : cout << "Invalid grade" << endl; } cout << "Your grade is " << grade << endl; return 0;}

Page 19: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

Nested loop • #include <iostream>• using namespace std;• • int main ()• {• // local variable declaration:• int a = 100;• int b = 200;• • switch(a) {• case 100: • cout << "This is part of outer switch" << endl;• switch(b) {• case 200:• cout << "This is part of inner switch" << endl;• }• }• cout << "Exact value of a is : " << a << endl;• cout << "Exact value of b is : " << b << endl;• • return 0;• }

Page 20: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

Nested If loop • #include <iostream>• using namespace std;• • int main ()• {• // local variable declaration:• int a = 100;• int b = 200;• • // check the boolean condition• if( a == 100 )• {• // if condition is true then check the following• if( b == 200 )• {• // if condition is true then print the following• cout << "Value of a is 100 and b is 200" << endl;• }• }• cout << "Exact value of a is : " << a << endl;• cout << "Exact value of b is : " << b << endl;• • return 0;• }

Page 21: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

If else

• if(boolean_expression 1)• {• // Executes when the boolean expression 1 is true• }• else if( boolean_expression 2)• {• // Executes when the boolean expression 2 is true• }• else if( boolean_expression 3)• {• // Executes when the boolean expression 3 is true• }• else • {• // executes when the none of the above condition is true.• }

Page 22: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

#include <iostream> // student grading using namespace std; int main (){ int mark; // local variable declaration: cout << "mark is= " << endl; cin>> mark ; // check the boolean condition if( mark >= 90 ) { // if condition is true then print the following cout << "Value of a is A" << endl; } else if(mark >= 80 ) { // if else if condition is true cout << "Value of a is B" << endl; } else if( mark >= 70 ) { // if else if condition is true cout << "Value of a is c" << endl; } else if( mark >= 60 ) { // if else if condition is true cout << "Value of a is D" << endl; } else { // if none of the conditions is true cout << " you got F " << endl; } // cout << " value of mark is : " << a << endl;system("pause"); return 0;}

Page 23: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

mathematical operations;

• #include <iostream>• #include <cmath>• using namespace std;• • int main ()• {• // number definition:• short s = 10;• int i = -1000;• long l = 100000;• float f = 230.47;• double d = 200.374;• • // mathematical operations;• cout << "sin(d) :" << sin(d) << endl;• cout << "abs(i) :" << abs(i) << endl;• cout << "floor(d) :" << floor(d) << endl;• cout << "sqrt(f) :" << sqrt(f) << endl;• cout << "pow( d, 2) :" << pow(d, 2) << endl;• system("pause");• return 0;• }

Page 24: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

While

• while(condition)• {• statement(s);• }

Flow Diagram:

Page 25: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

• #include <iostream>• using namespace std;• • int main ()• {• // Local variable declaration:• int a = 10;

• // while loop execution• while( a < 20 )• {• cout << "value of a: " << a << endl;• a++;• }• • return 0;• }

Page 26: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

• If

Page 27: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

• #include <iostream>• using namespace std;• • int main ()• {• // local variable declaration:• int a = 10;• • // check the boolean condition• if( a < 20 )• {• // if condition is true then print the following• cout << "a is less than 20;" << endl;• }• cout << "value of a is : " << a << endl;• • return 0;• }

Page 28: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

If else if(boolean _ expression){ // statement(s) will execute if the boolean expression is true}else{ // statement(s) will execute if the boolean expression is false}

Page 29: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

• #include <iostream>• using namespace std;• • int main ()• {• // local variable declaration:• int a = 100;• • // check the boolean condition• if( a < 20 )• {• // if condition is true then print the following• cout << "a is less than 20;" << endl;• }• else• {• // if condition is false then print the following• cout << "a is not less than 20;" << endl;• }• cout << "value of a is : " << a << endl;• • return 0;• }

Page 30: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

• #include <iostream>• using namespace std;• • int main ()• {• // local variable declaration:• int a = 100;• • // check the boolean condition• if( a == 10 )• {• // if condition is true then print the following• cout << "Value of a is 10" << endl;• }• else if( a == 20 )• {• // if else if condition is true• cout << "Value of a is 20" << endl;• }• else if( a == 30 )• {• // if else if condition is true • cout << "Value of a is 30" << endl;• }• else• {• // if none of the conditions is true• cout << "Value of a is not matching" << endl;• }• cout << "Exact value of a is : " << a << endl;• • return 0;• }

Page 31: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

• #include <iostream>• using namespace std;• int main()• {• float a,b;• char x;• cout << "Enter Number1:\t" ;• cin >> a;• cout << "Enter Number2:\t" ;• cin >> b;• cout << "Enter the operator\t";• cin >> x;

• cout << endl << endl;

• cout << "Result:\t";

• if (x=='+') { cout << a+b ;}• else if (x=='-') { cout << a-b;}• else if (x=='*') { cout << a*b;}• else if (x=='/') { cout << a/b;}• else { cout << "Bad Command";}

• cout << endl;• system("pause");• return 0;}

Page 32: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

• #include <iostream>• using namespace std;

• int main()• {• float a,b;• char x;

• cout << "Enter Number1:\t" ;• cin >> a;• cout << "Enter Number2:\t" ;• cin >> b;• cout << "Enter the operator\t";• cin >> x;• cout << endl << endl;• cout << "Result:\t";• switch (x) {• case '+':• cout << a+b ;• break;• case '-':• cout << a-b;• break;• case '*':• cout << a*b;• break;• case'/':• cout << a/b;• break;• default:• cout << "Bad Command";• }

• cout << endl;• system("pause");• return 0;}

Page 33: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

• //grading • #include <iostream>• using namespace std;

• int main()• {• float degree=0;• char mark;• cout << "Please Enter Your degree:\t" ;• cin >> degree;

• if ((degree <=100) && (degree>= 90))• mark='A';• else if ((degree <90) && (degree>= 80))• mark='B';• else if ((degree <80) && (degree>= 70))• mark='C';• else if ((degree <70) && (degree>= 60))• mark='D';• else if ((degree <60) || (degree>= 0))• mark='F';• else if((degree >100) || (degree < 0)) {• cout << "False degree" << endl;return 0;• }• else {cout << "Bad command" << endl;• return 0 ;}• cout << endl;• cout << "Your Mark:\t" << mark ;• cout << endl;• system("pause");• return 0;• }

Page 34: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

Factorial program in c++ using recursion

• #include <iostream>• using namespace std;• • long factorial(int);• • int main()• {• int n;• long f;• • cout <<"Enter an integer to find factorial\n";• cin>> n; • • if (n < 0)• cout<<"Negative integers are not allowed.\n";• else• {• f = factorial(n);• cout<<" n="<<n << "f="<<f<<endl;• }• system("pause");• return 0;• }• • long factorial(int n)• {• if (n == 0)• return 1;• else• return(n * factorial(n-1));• • }

Page 35: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

C++ Functions

Page 36: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

• #include <iostream>• using namespace std;• int absolute (int);// function prototype for absolute()• int main(){ • int num, answer; • cout << "Enter an integer (0 to stop): "; • cin >> num;• while (num!=0){ • answer = absolute(num); • cout << "The absolute value of " << num • << " is: " << answer << endl;• cin >> num; }• return 0; } • // Define a function to take absolute value of an integer • int absolute(int x){ • if (x >= 0) return x; • else return -x; }

36

Page 37: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

• #include <iostream>• using namespace std;• double total_second(int, double ,double );• int main(){• cout << total_second(1,1.5, 2) << endl;• system("pause");• return 0;• }• double total_second( int hour, double minutes, • double second)• {• return hour*3600 + minutes * 60 + second;• system("pause");• }

37

Page 38: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

• // Creating and using a programmer-defined function.• #include <iostream.h>• • int square( int ); // function prototype• • int main()• {• // loop 10 times and calculate and output • // square of x each time• for ( int x = 1; x <= 10; x++ ) • cout << square( x ) << " "; // function call• • cout << endl;• system("pause");• return 0; // indicates successful termination• • } // end main• • // square function definition returns square of an integer • int square( int y ) // y is a copy of argument to function• { • return y * y; // returns square of y as an int • • } // end function square 38

Page 39: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

• #include<iostream.h>

• int square(int); // prototype• int cube(int); // prototype• int main()• { int i;• for (int i=1;i<=10;i++){

• cout<< i<< "square=" << square(i) << endl;• cout<< i<< "cube=" <<cube(i) << endl;• } // end for• system("pause");• return 0;• } // end main function• int square(int y) //function definition• {• return y*y; // returned Result• }

• int cube(int y) //function definition• {• return y*y*y; // returned Result• }

39

Page 40: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

Random Number Generation

• #include <iostream>• #include <string>• #include <cstdlib>• using namespace std;• int main() {• for (int i = 1; i <= 15; ++i)• cout << rand() << endl;• system("pause");• return 0;• }

40

Page 41: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

Finding Errors in Function Codeint sum(int x, int y){

int result;result = x+y;

}this function must return an integer value as indicated in the header

definition (return result;) should be added-----------------------------------------------------------------------------------------int sum (int n){ if (n==0)

return 0;else

n+sum(n-1);}the result of n+sum(n-1) is not returned; sum returns an improper

result, the else part should be written as:- else return n+sum(n-1);

Page 42: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

42

Agenda

• What is a function?• Types of C++ functions:

– Standard functions– User-defined functions

• C++ function structure– Function signature– Function body

• Declaring and Implementing C++ functions

• Sharing data among functions through function parameters– Value parameters– Reference parameters– Const reference

parameters• Scope of variables

– Local Variables– Global variable

Page 43: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

43

Functions and subprograms• The Top-down design appeoach is based on dividing the main

problem into smaller tasks which may be divided into simpler tasks, then implementing each simple task by a subprogram or a function

• A C++ function or a subprogram is simply a chunk of C++ code that has– A descriptive function name, e.g.

• computeTaxescomputeTaxes to compute the taxes for an employee • isPrimeisPrime to check whether or not a number is a prime number

– A returning value• The computeTaxesomputeTaxes function may return with a double number

representing the amount of taxes• The isPrimeisPrime function may return with a Boolean value (true or false)

Page 44: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

44

C++ Standard Functions• C++ language is shipped with a lot of functions which

are known as standard functions• These standard functions are groups in different

libraries which can be included in the C++ program, e.g.– Math functions are declared in <math.h> library– Character-manipulation functions are declared in

<ctype.h> library– C++ is shipped with more than 100 standard libraries,

some of them are very popular such as <iostream.h> and <stdlib.h>, others are very specific to certain hardware platform, e.g. <limits.h> and <largeInt.h>

Page 45: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

45

Example of Using Standard C++ Math Functions

#include <iostream.h>#include <math.h>void main(){

// Getting a double valuedouble x;cout << "Please enter a real number: ";cin >> x;// Compute the ceiling and the floor of the real numbercout << "The ceil(" << x << ") = " << ceil(x) << endl;cout << "The floor(" << x << ") = " << floor(x) << endl;

}

Page 46: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

46

Example of Using Standard C++ Character Functions

#include <iostream.h> // input/output handling#include <ctype.h> // character type functionsvoid main(){

char ch;cout << "Enter a character: ";cin >> ch; cout << "The toupper(" << ch << ") = " << (char) toupper(ch) << endl;cout << "The tolower(" << ch << ") = " << (char) tolower(ch) << endl;if (isdigit(ch))

cout << "'" << ch <<"' is a digit!\n";else

cout << "'" << ch <<"' is NOT a digit!\n";}

Explicit casting

Page 47: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

47

User-Defined C++ Functions

• Although C++ is shipped with a lot of standard functions, these functions are not enough for all users, therefore, C++ provides its users with a way to define their own functions (or user-defined function)

• For example, the <math.h> library does not include a standard function that allows users to round a real number to the ith digits, therefore, we must declare and implement this function ourselves

Page 48: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

48

How to define a C++ Function?

• Generally speaking, we define a C++ function in two steps (preferably but not mandatory)– Step #1 – declare the function signaturefunction signature in either a

header file (.h file) or before the main function of the program

– Step #2 – Implement the function in either an implementation file (.cpp) or after the main function

Page 49: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

49

What is The Syntactic Structure of a C++ Function?

• A C++ function consists of two parts– The function header, and– The function body

• The function header has the following syntax<return value> <name> (<parameter list>)<return value> <name> (<parameter list>)

• The function body is simply a C++ code enclosed between { }

Page 50: GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout

50

Example of User-definedC++ Function

double computeTax(double income){ if (income < 5000.0) return 0.0; double taxes = 0.07 * (income-5000.0); return taxes;}