820 likes | 931 Vues
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.
E N D
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
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
- /* this is a multi-line comment */ Can begin and end on the same line: int area; /* calculated area */
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
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
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
- • 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.
- • cin converts data to the type that matches the variable: int height; cout<< How tall is the room? "; cin >> height;
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;
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
Flowchart Symbol Algorithm InstructionFlowchart Symbol START / RETURN / END / EXIT ENTER / PRINT / WRITE (input/output statement) Variable / Expression (assignment statement) PROCESS (module call)
. اتخاذ القرار Flow lines
Q: Mention types of Logic Structures? Sequential logic structure Selection logic structure Loop logic structure
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
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;
. 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; }
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);
. The output screen : Enter Fahrenheit 95 Centigrade is 35 cout<<“centigrade =“<<centigrade; system(“pause”); return; }
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;
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; }
Making Decisions Control Structures Selection Structure Loop Structure
Selection Structure The if Statement
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."
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
Draw a Flowchart for an if statement with a null false branch
Q:Write a general format for an if Statement with a null false branch? • if (expression) • statement;
if Statement Notes • if (score > 90) • grade = 'A'; • Do not place ; after (expression) • Place statement; on a separate line after (expression), indented:
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.
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
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;
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
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; }
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 .
Q: Write a general format for the if/else statement? • if (expression) • statement1; • else • statement2; General Format:
Q:Draw a flowchart, then write a c++ program that determines whether a number is odd or even. Then show the output screen)
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”;
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
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; }
.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
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
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
Q: Write a general format of if/else if if (expression) statements ; else if (expression) statements ; . // other else ifs else if (expression) statements ; else
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 .
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)
. #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”;
. else if(score>=70) cout<<“C\n”; else if(score>=60) cout<<“D\n”; else cout<<“Fail/n”; system(“pause”); return 0; }
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)
. #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 }