> 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; }"> > 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; }">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout << " please inter First output i,j,x,y.

Similar presentations


Presentation on theme: "GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout << " please inter First output i,j,x,y."— Presentation transcript:

1 GE 211 King Saud university Dr. Ahmed Telba

2

3 #include 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; }

4 For loop for ( variable initialization; condition; variable update ) { Code to execute while the condition is true }

5 For loop #include 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"); }

6 //Example: #include 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"); }

7 While loop #include 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”); }

8 Do while // number echoer #include 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; }

9 Do while #include 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"); }

10 If else #include 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”); }

11 switch exampleif-else equivalent switch (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"; }

12

13

14 absabsolute value acosarc cosine asinarc sine atanarc tangent atan2arc tangent, using signs to determine quadrants ceilthe smallest integer not less than a certain value coscosine coshhyperbolic cosine divreturns the quotient and remainder of a division expreturns "e" raised to a given power fabsabsolute value for floating-point numbers floorreturns the largest integer not greater than a given value fmodreturns the remainder of a division frexpdecomposes a number into scientific notation labsabsolute value for long integers ldexpcomputes a number in scientific notation

15 ldiv returns the quotient and remainder of a division, in long integer form lognatural logarithm (to base e) log10common logarithm (to base 10) modfdecomposes a number into integer and fractional parts powreturns a given number raised to another number roundrounds given number to the nearest integer sinsine sinhhyperbolic sine sqrtsquare root tantangent tanhhyperbolic tangent

16 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.)

17 Switch case Switch

18 #include 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; }

19 Nested loop #include 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; }

20 Nested If loop #include 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; }

21 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. }

22 #include // 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; }

23 mathematical operations; #include 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; }

24 While while(condition) { statement(s); } Flow Diagram:

25 #include 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; }

26 If

27 #include 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; }

28 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 }

29 #include 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; }

30 #include 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; }

31 #include 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;}

32 #include 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;}

33 //grading #include using namespace std; int main() { float degree=0; char mark; cout << "Please Enter Your degree:\t" ; cin >> degree; if ((degree = 90)) mark='A'; else if ((degree = 80)) mark='B'; else if ((degree = 70)) mark='C'; else if ((degree = 60)) mark='D'; else if ((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; }

34 Factorial program in c++ using recursion #include 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)); }

35 C++ Functions

36 #include 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

37 #include 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

38 // Creating and using a programmer-defined function. #include 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

39 #include 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

40 Random Number Generation #include using namespace std; int main() { for (int i = 1; i <= 15; ++i) cout << rand() << endl; system("pause"); return 0; } 40

41 Finding Errors in Function Code int 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);

42 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

43 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. computeTaxes computeTaxes to compute the taxes for an employee isPrime isPrime to check whether or not a number is a prime number – A returning value omputeTaxes The computeTaxes function may return with a double number representing the amount of taxes isPrime The isPrime function may return with a Boolean value (true or false)

44 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 library – Character-manipulation functions are declared in library – C++ is shipped with more than 100 standard libraries, some of them are very popular such as and, others are very specific to certain hardware platform, e.g. and

45 45 Example of Using Standard C++ Math Functions #include void main() { // Getting a double value double x; cout << "Please enter a real number: "; cin >> x; // Compute the ceiling and the floor of the real number cout << "The ceil(" << x << ") = " << ceil(x) << endl; cout << "The floor(" << x << ") = " << floor(x) << endl; }

46 46 Example of Using Standard C++ Character Functions #include // input/output handling #include // character type functions void 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

47 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 library does not include a standard function that allows users to round a real number to the i th digits, therefore, we must declare and implement this function ourselves

48 48 How to define a C++ Function? Generally speaking, we define a C++ function in two steps (preferably but not mandatory) function signature – Step #1 – declare the function 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

49 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 ( ) ( ) The function body is simply a C++ code enclosed between { }

50 50 Example of User-defined C++ Function double computeTax(double income) { if (income < 5000.0) return 0.0; double taxes = 0.07 * (income-5000.0); return taxes; }


Download ppt "GE 211 King Saud university Dr. Ahmed Telba. #include using namespace std; int main() { int i, j; double x, y; cout << " please inter First output i,j,x,y."

Similar presentations


Ads by Google