1 / 81

2/6/1435 h Wednesday

2/6/1435 h Wednesday. Lecture 8. Q: What is a %(modulus) operator ?. % (modulus) operator computes the remainder resulting from integer division cout << 13 % 5; // displays 3 % requires integers for both operands cout << 13 % 5.0; // error.

Télécharger la présentation

2/6/1435 h Wednesday

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 2/6/1435 h Wednesday Lecture 8

  2. Q: What is a %(modulus) operator ? • % (modulus) operator computes the remainder resulting from integer division cout << 13 % 5; // displays 3 • % requires integers for both operands cout << 13 % 5.0; // error

  3. Q:What are Comments? Used to document parts of the program Are ignored by the compiler Single-Line Comments Begin with // through to the end of line: Multi-Line Comments Begin with /*, end with */ Can span multiple lines: example

  4. - /* this is a multi-line comment */ Can begin and end on the same line: int area; /* calculated area */

  5. Q:When does/ (division) operator performs integer division • If both operands are integers cout << 13 / 5; // displays 2 cout << 91 / 7; // displays 13 Q:When does the / have a result as a floating point • If either operand is floating point, the result is floating point Example: cout<< 13/ 5.0; //displays 2.6 cout<<91.0/ 7; //displays 13.0

  6. Named ConstantsQ: Define a named constant(constant variable)? • const double TAX_RATE = 0.0675; • const intNUM_STATES = 50; It is a variable whose content cannot be changed during program execution Used for representing constant values with descriptive names: Often named in uppercase letters

  7. The cin ObjectQ:What is a cin object? • Standard input object • Like cout, requires iostream file • Used to read input from keyboard • Information retrieved from cin with >> • Input is stored in one or more variables

  8. - • Can be used to input more than one value: cin >> height >> width; • Multiple values from keyboard must be separated by spaces • Order is important: first value entered goes to first variable, etc.

  9. - • cin converts data to the type that matches the variable: int height; cout<< How tall is the room? "; cin >> height;

  10. Displaying a PromptQ: What is a prompt? A prompt is a message that instructs the user to enter data. You should always use cout to display a prompt before each cin statement.cout << "How tall is the room? ";cin >> height;

  11. Algorithms and FlowchartsQ: Define an algorithm? Algorithm – A set of instructions explaining how processing a module/task leading to a solution is done Q: Define a flowchart? A Flowchart is : • Visual illustration of an algorithm • Represent logical flow of data through a function

  12. Flowchart Symbol Algorithm InstructionFlowchart Symbol START / RETURN / END / EXIT ENTER / PRINT / WRITE (input/output statement) Variable / Expression (assignment statement) PROCESS (module call)

  13. . اتخاذ القرار Flow lines

  14. Q: Mention types of Logic Structures? Sequential logic structure Selection logic structure Loop logic structure

  15. Sequential Logic StructureQ: Write an algorithm ,design a flowchart then write a program that represents a sequential logic structure Start • Algorithm • Enter any three numbers :A,B,C • Sum = A+B+C • Mult = A*B*C • Print Sum, Mult • End Enter A,B,C • Sum = A+B+C • Mult = A*B*C PRINT NAME, Sum, Mult END

  16. Program 4Write a c++ program that read any three integer numbers and prints their sum and multiplication #include<iostream> using namespace std; void main( ) { int a, b, c, sum, mult; cout<<“Enter any three numbers”<<endl; cin>>a>>b>>c;

  17. . The output screen: Enter any three numbers 7 6 8 Sum=21 Mul= 336 sum=a+b+c; mult=a*b*c; cout<<“sum=“<<sum<<endl<<“mult=“<<mult<<endl; system(“pause”); return; }

  18. Program 5Write a c++ program that converts the temperature degree from Fahrenheit to Centigrade #include<iostream> using namespace std; void main( ) { int centigrade, Fahrenheit; cout<<“enter Fahrenheit ”; cin>> Fahrenheit; centigrade = 5/9(fahrenheit-32);

  19. . The output screen : Enter Fahrenheit 95 Centigrade is 35 cout<<“centigrade =“<<centigrade; system(“pause”); return; }

  20. Program 6Q: Write a c++ program that calculates the rectangle’s area and displays the value on the screenNote: the program ask the user to enter the length and width #include<iostream> using namespace std; int main() { int length, width, area Cout<<“this program calculates the area of a rectangle\n”; Cout<<“enter the length of the rectangle”; Cin>>length;

  21. 0 Program output with example of input this program calculates the area of a rectangle enter the length of the rectangle 10[enter] Enter the width of a rectangle 5 [enter] The area of the rectangle is: 50 cout<<“enter the width of a rectangle”; cin>>width; Area = length*width; cout<<“ the area of the rectangle is: “<< area<<endl; system(“pause”); return 0; }

  22. Making Decisions Control Structures Selection Structure Loop Structure

  23. Selection Structure The if Statement

  24. What is an if Statement? It allows statements to be conditionally executed or skipped over Example: • "If it is raining, take an umbrella." • "If it is cold outside, wear a coat."

  25. Q: .Mention Types of IF statements If statement with a null false branch The if/else Statement Nested if Statements The if/else if Statement

  26. Draw a Flowchart for an if statement with a null false branch

  27. Flowchart for Evaluating a Decision

  28. Q:Write a general format for an if Statement with a null false branch? • if (expression) • statement;

  29. if Statement Notes • if (score > 90) • grade = 'A'; • Do not place ; after (expression) • Place statement; on a separate line after (expression), indented:

  30. Q:How an if Statement being evaluated To evaluate: if (expression) statement; • If the expression is true, then statementis executed. • If the expressionis false, then statement is skipped.

  31. Program 7Q:Write a c++ program that get three test score and display the average, thenIf the average is greater than 95 display(congratulation! This is a high score)----------------------------------------------------------P if else

  32. The Program is:#include<iostream>using namespace std;int main(){int score1, score2, score3<<endl;double average; cout<<“enter test scores:”;cin>> score1>> score2>> score3;average = (score1+score2+score3)/3;

  33. Cout<<“your average is:”<<average<<endl;if (average is >95)cout<<“congratulation! This is a high score\n”;system(“pause”);return 0;} Program output Enter test scores: 80 90 70[enter] Your average is 80 ---------------------------------------------------------------------------- Program output with another example of data: Enter test scores: 100 100 100 Your average is 100 congratulation! This is a high score

  34. Expanding the if StatementQ:Define an expanding if statement?To execute more than one statement as part of an if statement, and we have to enclose them in { }: • if (score > 90) • { • grade = 'A'; • cout << "Good Job!\n"; • } Q:Write a general format of the Expanding if Statement if (expression) { statement1; statement2; }

  35. The if/else StatementQ:Define an if/else statement? Provides two possible paths of execution Performs one statement if the expression is true, otherwise performs another statement .

  36. Q: Write a general format for the if/else statement? • if (expression) • statement1; • else • statement2; General Format:

  37. Q:Draw a flowchart, then write a c++ program that determines whether a number is odd or even. Then show the output screen)

  38. Program 8#include<iostream>using namespace std;int main(){int number;cout<<“enter any number and I will tell you if it is even or odd \n”;cin>>number;if (number % 2 ==0)cout<<number <<“is even \n”;

  39. elsecout<<number<<“ is odd \n”;system(“pause”)return o;}p Program output with a sample of input enter any number and I will tell you if it is even or odd 17[enter] 17 is odd Press any key to continue

  40. Program 9no Q:Write a c++ program to enter a salary, if the salary is >= 5000 then tax is 30%,otherwise tax is 10%#include<iostream>using namespace std;int main(){int salary;float tax;cout<<“enter salary”;cin>>salary;if (salary>=5000) { tax = salary*0.3; cout<<“tax is:”<<tax; }

  41. .else { tax = salary*0.1; cout<<“tax is :”<<tax;} system(“pause”); return 0; } Program output with a sample of input enter salary 3500[enter] tax is 350 ------------------------------------------------------------- another sample of input enter salary 5000[enter] tax is 1500

  42. Nested if StatementsQ: Define a Nested if Statements An if statement that is nested inside another if statement Q: When does Nested if statement used? Nested if statements can be used to test more than one condition

  43. The if/else if StatementQ:What is an if/else if Statement It tests a series of conditions until one is found to be true Often simpler than using nested if/else statements

  44. Q: Write a general format of if/else if if (expression) statements ; else if (expression) statements ; . // other else ifs else if (expression) statements ; else

  45. Q: Draw a flowchart for a Nested if statement score>= 90 n score>= 80 n y Score>= 70 n A y Score>= 60 n B y C y D F .

  46. Program 10 no Q: Using IF statement write a c++ program that enters a score and displays the following grades:Score >= 90 displays 'A'Score >= 80 displays 'B'Score >= 70 displays 'C'Score >= 60 displays 'D'Score <60 displays (Fail)

  47. . #include<iostream> using namespace std; int main() { int score; cout<<“enter score: “; cin>>score; if (score>=90) cout<<“A\n”; else if(score>=80) cout<<“B\n”;

  48. . else if(score>=70) cout<<“C\n”; else if(score>=60) cout<<“D\n”; else cout<<“Fail/n”; system(“pause”); return 0; }

  49. Switch Statement Q:Define switch statement? It is used to select a statement from several alternatives. Program 11 Q: Using Switch statement write a c++ program that enters a score and displays the following grades:Score >= 90 displays 'A'Score >= 80 displays 'B'Score >= 70 displays 'C'Score >= 60 displays 'D'Score < 60 displays (Fail)

  50. . #include<iostream> using namespace std; int main( ) { char choice; cout<<“enter A or B or C” cin>>choice; switch(choice)e { case’A’:cout<<“you entered A \n”; break; case’B’:cout<<“you entered B \n”; break; case’C’:cout<<“you entered C \n”; break; default:cout<<“you did not entered A or B or C” } system(“pause”); return 0 }

More Related