1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator...

81
1 2/6/1435 h Wednesday Lecture 8 Lecture 8

Transcript of 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator...

Page 1: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

1

2/6/1435 h Wednesday

Lecture 8Lecture 8

Page 2: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

2

Q: What is a %(modulus) operator ?

1.1. % (modulus) operator computes % (modulus) operator computes the remainder resulting from the remainder resulting from integer divisioninteger division

cout << 13 % 5; // displays 3cout << 13 % 5; // displays 3

2.2. % requires integers for both % requires integers for both operandsoperands

cout << 13 % 5.0; // errorcout << 13 % 5.0; // error

Page 3: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

3

Q:What are Comments?1.1. Used to document parts of the Used to document parts of the

programprogram

2.2. Are ignored by the compilerAre ignored by the compiler

3.3. Single-Line Comments Begin with // Single-Line Comments Begin with // through to the end of line:through to the end of line:

4.4. Multi-Line CommentsMulti-Line Comments Begin with /*, end with */Begin with /*, end with */ Can span multiple lines: exampleCan span multiple lines: example

Page 4: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

4

- /* this is a multi-line/* this is a multi-line

commentcomment

*/*/

Can begin and end on the same Can begin and end on the same line:line:

int area; int area;

/* calculated area *//* calculated area */

Page 5: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

5

Q:When does / (division) operator performs integer division

1.1. If both operands are integersIf both operands are integerscout << 13 / 5; // displays 2cout << 13 / 5; // displays 2cout << 91 / 7; // displays 13cout << 91 / 7; // displays 13Q:When does the / have a result as Q:When does the / have a result as a floating pointa floating point

2.2. If either operand is floating point, If either operand is floating point, the result is floating pointthe result is floating point

Example:Example:cout<< 13/ 5.0; //displays 2.6cout<< 13/ 5.0; //displays 2.6cout<<91.0/ 7; //displays 13.0cout<<91.0/ 7; //displays 13.0

Page 6: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

6

Named ConstantsQ: Define a named constant(constant variable)?

1.1. It is a variable whose content cannot be It is a variable whose content cannot be changed during program executionchanged during program execution

2.2. Used for representing constant values with Used for representing constant values with descriptive names:descriptive names:

3.3. Often named in uppercase lettersOften named in uppercase letters

const double TAX_RATE = 0.0675;const int NUM_STATES = 50;

Page 7: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

7

The cin ObjectQ:What is a cin object?

1.1. Standard input objectStandard input object

2.2. Like cout, requires iostream fileLike cout, requires iostream file

3.3. Used to read input from keyboardUsed to read input from keyboard

4.4. Information retrieved from cin with Information retrieved from cin with >>>>

5.5. Input is stored in one or more Input is stored in one or more variables variables

Page 8: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

8

-6.6. Can be used to input more than Can be used to input more than

one value:one value:cin >> height >> width;cin >> height >> width;

7.7. Multiple values from keyboard must Multiple values from keyboard must be separated by spacesbe separated by spaces

8.8. Order is important: first value Order is important: first value entered goes to first variable, etc.entered goes to first variable, etc.

Page 9: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

9

-

9.9. cincin converts data to the type that converts data to the type that matches the variable:matches the variable:

int height;int height;cout<< How tall is the room? ";cout<< How tall is the room? ";

cin >> height;cin >> height;

Page 10: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

10

Displaying a PromptQ: What is a prompt?

1.1. A prompt is a message that instructs the A prompt is a message that instructs the user to enter data.user to enter data.

2.2. You should always use You should always use coutcout to display to display a prompt before each a prompt before each cincin statement. statement.

cout << "How tall is the room? ";cout << "How tall is the room? ";cin >> height;cin >> height;

Page 11: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

11

Algorithms and FlowchartsQ: Define an algorithm?

Algorithm – A set of instructions Algorithm – A set of instructions explaining how processing a explaining how processing a module/task leading to a solution is module/task leading to a solution is donedone

Q: Define a flowchart?Q: Define a flowchart?

A Flowchart is :A Flowchart is : Visual illustration of an algorithmVisual illustration of an algorithm Represent logical flow of data through Represent logical flow of data through

a functiona function

Page 12: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

12

Flowchart SymbolAlgorithm InstructionAlgorithm Instruction Flowchart SymbolFlowchart Symbol

START / RETURN / END / EXITSTART / RETURN / END / EXIT

ENTER / PRINT / WRITEENTER / PRINT / WRITE(input/output statement)(input/output statement)

Variable / ExpressionVariable / Expression(assignment statement)(assignment statement)

PROCESSPROCESS(module call)(module call)

Page 13: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

13

.

القرار القرار اتخاذ اتخاذ

Flow linesFlow lines

Page 14: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

14

Q: Mention types of Logic Structures?

1.1. Sequential logic structureSequential logic structure

2.2. Selection logic structureSelection logic structure

3.3. Loop logic structureLoop logic structure

Page 15: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

15

1. Sequential Logic StructureQ: Write an algorithm ,design a

flowchart then write a program that represents a sequential logic structure

AlgorithmAlgorithm

1.1. Enter any three Enter any three numbers :A,B,Cnumbers :A,B,C

2.2. Sum = A+B+CSum = A+B+C

3.3. Mult = A*B*CMult = A*B*C

4.4. Print Sum, MultPrint Sum, Mult

5.5. EndEnd

Start

END

Enter A,B,C

PRINT NAME,Sum, Mult

Sum = A+B+CMult = A*B*C

Page 16: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

16

Program 4

Write a c++ program that read any three integer numbers and prints

their sum and multiplication

#include<iostream>#include<iostream>

using namespace std;using namespace std;

void main( )void main( )

{{

int a, b, c, sum, mult;int a, b, c, sum, mult;

cout<<“Enter any three numbers”<<endl;cout<<“Enter any three numbers”<<endl;

cin>>a>>b>>c;cin>>a>>b>>c;

Page 17: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

17

.sum=a+b+c;sum=a+b+c;

mult=a*b*c;mult=a*b*c;

cout<<“sum=“<<sum<<endl<<“mult=“<<mult<<endl;cout<<“sum=“<<sum<<endl<<“mult=“<<mult<<endl;

system(“pause”);system(“pause”);

return;return;

}}

The output screen:Enter any three numbers768Sum=21Mul= 336

Page 18: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

18

Program 5Write a c++ program that converts the temperature degree from Fahrenheit to Centigrade

#include<iostream>#include<iostream>

using namespace std;using namespace std;

void main( )void main( )

{{

int centigrade, Fahrenheit;int centigrade, Fahrenheit;

cout<<“enter Fahrenheit ”;cout<<“enter Fahrenheit ”;

cin>> Fahrenheit;cin>> Fahrenheit;

centigrade = 5/9(fahrenheit-32);centigrade = 5/9(fahrenheit-32);

Page 19: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

19

.cout<<“centigrade =“<<centigrade;cout<<“centigrade =“<<centigrade;

system(“pause”);system(“pause”);

return;return;

}}

The output screen :Enter Fahrenheit 95Centigrade is 35

Page 20: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

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

using namespace std;using namespace std;

int main()int main()

{{

int length, width, areaint length, width, areaCout<<“this program calculates the area of a Cout<<“this program calculates the area of a

rectangle\n”;rectangle\n”;

Cout<<“enter the length of the rectangle”;Cout<<“enter the length of the rectangle”;

Cin>>length;Cin>>length;

Page 21: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

21

0cout<<“enter the width of a cout<<“enter the width of a

rectangle”;rectangle”;

cin>>width;cin>>width;

Area = length*width;Area = length*width;cout<<“ the area of the rectangle is: “<< cout<<“ the area of the rectangle is: “<<

area<<endl;area<<endl;

system(“pause”);system(“pause”);

return 0;return 0;

} }

Program output with example of inputthis program calculates the area of a rectangleenter the length of the rectangle 10[enter]Enter the width of a rectangle 5 [enter]The area of the rectangle is: 50

Page 22: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

Making Decisions

Control StructuresControl Structures

1.1.Selection StructureSelection Structure

2.2.Loop StructureLoop Structure

Page 23: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

23

1. Selection Structure

The if StatementThe if Statement

Page 24: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

24

What is an if Statement?

It allows statements to be It allows statements to be conditionally executed or skipped conditionally executed or skipped overover

Example:Example: "If it is raining, take an umbrella.""If it is raining, take an umbrella." "If it is cold outside, wear a coat.""If it is cold outside, wear a coat."

Page 25: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

25

Q: .Mention Types of IF statements

1.1. If statement with a null false If statement with a null false branchbranch

2.2. The if/else StatementThe if/else Statement

3.3. Nested if StatementsNested if Statements

4.4. The if/else if StatementThe if/else if Statement

Page 26: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

26

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

Page 27: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

27

Flowchart for Evaluating a Decision

Page 28: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

28

Q:Write a general format for an if Statement with a null false branch?

if (expression)statement;

Page 29: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

29

if Statement Notes

1.1. Do not place ; after (Do not place ; after (expressionexpression))2.2. Place Place statementstatement; on a separate ; on a separate

line after (line after (expressionexpression), indented:), indented:if (score > 90)grade = 'A';

Page 30: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

30

Q:How an if Statement being evaluated

To evaluate:To evaluate:

if (if (expressionexpression))

statementstatement;;

1.1. If the If the expressionexpression is is truetrue, then , then statementstatement is executed.is executed.

2.2. If the If the expressionexpression is is falsefalse, then , then statementstatement is is skipped.skipped.

Page 31: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

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

Page 32: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

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;

Page 33: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

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 100Your average is 100 congratulation! This is a high score

Page 34: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

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 { }:

Q:Write a general format of the Expanding Q:Write a general format of the Expanding ifif Statement Statement

if (expression)if (expression)

{{

statement1;statement1;

statement2;statement2;

}}

if (score > 90){

grade = 'A'; cout << "Good Job!\n";

}

Page 35: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

35

2. The if/else StatementQ:Define an if/else statement?

1.1. Provides two possible paths of Provides two possible paths of executionexecution

2.2. Performs one statement if the Performs one statement if the expressionexpression is true, otherwise is true, otherwise performs another statement .performs another statement .

Page 36: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

36

Q: Write a general format for the if/else statement?

General Format:General Format:

if (expression)statement1;

elsestatement2;

Page 37: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

37

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

Page 38: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

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”;

Page 39: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

39

elsecout<<number<<“ is odd \n”;system(“pause”)return o;}

pProgram output with a sample of inputenter any number and I will tell you if it is even or odd17[enter]17 is oddPress any key to continue

Page 40: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

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

Page 41: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

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 inputenter salary 5000[enter]tax is 1500

Page 42: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

42

3. Nested if StatementsQ: Define a Nested if Statements

An if statement that is nested inside An if statement that is nested inside another if statementanother if statement

Q: When does Nested if statement Q: When does Nested if statement used?used?

Nested if statements can be used Nested if statements can be used to test more than one conditionto test more than one condition

Page 43: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

43

4. The if/else if StatementQ:What is an if/else if Statement

1.1. It tests a series of conditions until one is It tests a series of conditions until one is found to be truefound to be true

2.2. Often simpler than using nested Often simpler than using nested if/elseif/else statementsstatements

Page 44: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

44

Q: Write a general format of if/else if if (if (expressionexpression))

statements statements ; ; else if (else if (expressionexpression))

statements statements ; ; . . // other else ifs// other else ifs

else if (else if (expressionexpression)) statements statements ; ;

else else

Page 45: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

45

Q: Draw a flowchart for a Nested if statement..

score>=90

score>=80

B

A

F

Score>=70

Score>=60

C

D

y

y

y

y

n

n

n

n

Page 46: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

46

Program 10

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

Page 47: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

47

.#include<iostream>#include<iostream>

using namespace std;using namespace std;

int main()int main()

{{

int score;int score;

cout<<“enter score: “;cout<<“enter score: “;

cin>>score;cin>>score;

if (score>=90)if (score>=90)

cout<<“A\n”;cout<<“A\n”;

else if(score>=80)else if(score>=80)

cout<<“B\n”;cout<<“B\n”;

Page 48: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

48

.else if(score>=70)else if(score>=70)

cout<<“C\n”;cout<<“C\n”;

else if(score>=60)else if(score>=60)

cout<<“D\n”;cout<<“D\n”;

elseelse

cout<<“Fail/n”;cout<<“Fail/n”;

system(“pause”);system(“pause”);

return 0;return 0;

}}

Page 49: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

49

Switch Statement

Q:Define switch statement?Q:Define switch statement?

It is used to select a statement from several It is used to select a statement from several alternatives.alternatives.

Program 11Program 11Q: Using Switch statement write a c++ program that enters a score Q: Using Switch statement write a c++ program that enters a score

and displays the following grades:and displays the following grades:Score >= 90 displays 'A'Score >= 90 displays 'A'Score >= 80 displays 'B'Score >= 80 displays 'B'Score >= 70 displays 'C'Score >= 70 displays 'C'Score >= 60 displays 'D'Score >= 60 displays 'D'Score < 60 displays (Fail)Score < 60 displays (Fail)

Page 50: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

50

.##include<iostream>include<iostream>

using namespace std;using namespace std;

int main( )int main( )

{{

char choice;char choice;

cout<<“enter A or B or C”cout<<“enter A or B or C”

cin>>choice;cin>>choice;

switch(choice)e switch(choice)e

{{

case’A’:cout<<“you entered A \n”;case’A’:cout<<“you entered A \n”;

break;break;

case’B’:cout<<“you entered B \n”;case’B’:cout<<“you entered B \n”;

break;break;

case’C’:cout<<“you entered C \n”;case’C’:cout<<“you entered C \n”;

break;break;

default:cout<<“you did not entered A or B or C”default:cout<<“you did not entered A or B or C”

}}

system(“pause”);system(“pause”);

return 0return 0

}}

Page 51: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

51

2. Loop Structure

First we have to define a First we have to define a countercounter

Q:Define a counter?Q:Define a counter?

It is a variable that is incremented or It is a variable that is incremented or decremented according to the loop decremented according to the loop counter.counter.

Q: When do we initialize the Q: When do we initialize the counter?counter?

The counter must be initialized The counter must be initialized before entering the loop.before entering the loop.

Page 52: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

52

.

Q: Define a loop?Q: Define a loop?

It is a control structure that causes It is a control structure that causes statements to be executed more statements to be executed more than oncethan once

Q: Mention the types of loop Q: Mention the types of loop structure?structure?

1.1. WhileWhile

2.2. Do-whileDo-while

3.3. forfor

Page 53: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

53

1. While Loop StructureQ: Write a general format for a while loop Q: Write a general format for a while loop

structure?structure?while (expression) Statements;

Q:Design a flowchart that represents a while loop structure?

Loop body

expression

true

false

Page 54: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

54

Q: How does while loop work?

1.1. The expression is evaluatedThe expression is evaluated

2.2. If it is true, the statements in the body If it is true, the statements in the body of the loop is executedof the loop is executed

3.3. The expression is evaluated again and The expression is evaluated again and again till it becomes false then the loop again till it becomes false then the loop stopsstops

Page 55: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

55

Program 12 Q:Using while loop write a c++ program that displays numbers from 1-10, and their squares?##include<iostream>include<iostream>

using namespace std;using namespace std;

int main( )int main( )

{{

int count = 1;int count = 1;

while(count<=10)while(count<=10)

{{

cout<< count<<“ “<<count*count;cout<< count<<“ “<<count*count;

count++count++

}}

system(“pause”);system(“pause”);

return 0;return 0;

}}

Page 56: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

56

Program 13Write a c++ program that allows the user to enter scores for 4 subjects; then displays the average?#include<iostream>#include<iostream>

using namespace std;using namespace std;

int main( )int main( )

{{

sum = 0;sum = 0;

float average;float average;

float score;float score;

cout<<“Enter the score1”;cout<<“Enter the score1”;

cin>> score1;cin>> score1;

cout<<“Enter the score2”;cout<<“Enter the score2”;

cin>> score2;cin>> score2;

cout<<“Enter the score3”;cout<<“Enter the score3”;

cin>> score3;cin>> score3;

Page 57: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

57

.

cout<<“Enter the score4”;cout<<“Enter the score4”;

cin>> score4;cin>> score4;

sum = score1+score2score3+score4;sum = score1+score2score3+score4;

average = sum/average = sum/

cout<<“average is”<<average<<endl;cout<<“average is”<<average<<endl;

system(“pause”);system(“pause”);

return 0;return 0;

} }

Page 58: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

58

Program 14Using while loop, write a c++ program that allows the user to enter scores for 4 subjects; then displays the average?

#include<iostream>#include<iostream>

using namespace std;using namespace std;

int main( )int main( )

{{

int count = 1;int count = 1;

sum = 0;sum = 0;

float average;float average;

float score;float score;

while(count<=4)while(count<=4)

{{

cout<<“Enter the score”;cout<<“Enter the score”;

Page 59: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

59

.cin>> score;cin>> score;

sum = sum+score;sum = sum+score;

counter =counter +1;counter =counter +1;

}}

cout<<endl;cout<<endl;

average = sum/4;average = sum/4;

cout<<“average is”<<average<<endl;cout<<“average is”<<average<<endl;

system(“pause”);system(“pause”);

return 0;return 0;

} }

--------------------------------------------------------------------------------------------------------------------------------------------------

Page 60: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

60

2. DO-While loop

Q: Define do-while loop?Q: Define do-while loop?

It tests the condition after executing the body of the loop.It tests the condition after executing the body of the loop.

Q:Draw a flowchart for the do-while loop?Q:Draw a flowchart for the do-while loop?

Body of the loop

expression

true

false

Page 61: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

61

Q: Write a general format for do-while loop?DoDo

{{

Statement 1;Statement 1;

Statement 2 ;Statement 2 ;

..

..

While(condition)While(condition)

Q:Using flowcharts , compare between [While] and [Do..While] Q:Using flowcharts , compare between [While] and [Do..While] control loopcontrol loop

Q: Define an infinite loop?Q: Define an infinite loop?

It is a loop that never finishedIt is a loop that never finished

Page 62: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

62

Program 15Q: Using do-while loop, write a c++ program that prints numbers from 1-10##include<iostream>include<iostream>

using namespace std;using namespace std;

int main( )int main( )

{{

int counter = 1int counter = 1

DoDo

{{

cout<<counter<<“ “;cout<<counter<<“ “;

count++count++

}}

while ( counter<=10)while ( counter<=10)

system(“pause”);system(“pause”);

return 0;return 0;

}}

Page 63: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

63

3. For Loop

Q: Write a general format of the For loop?Q: Write a general format of the For loop?

For(For(initial value; expression; updating statement)initial value; expression; updating statement)

------------------------------------------------------------------------------------------------------------------------------------------------

Program 16Program 16

Q: WQ: Write a c++ program that displays the numbers from 1-10 using for loop?rite a c++ program that displays the numbers from 1-10 using for loop?

#include<iostream>#include<iostream>

using namespace std;using namespace std;

Int main( )Int main( )

{{

for(int counter = 1; counter<=10; counter++)for(int counter = 1; counter<=10; counter++)

cout<<counter<<endl;cout<<counter<<endl;

System (“pause”);System (“pause”);

return 0;return 0;

}}

Page 64: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

64

Program 17 Q: Write a c++ program that displays the numbers from 10-1 using for loop?

#include<iostream>#include<iostream>

using namespace std;using namespace std;

int main( )int main( )

{{

for(int counter = 10; counter<=1; counter++)for(int counter = 10; counter<=1; counter++)

cout<<counter<<endl;cout<<counter<<endl;

system {“pause”);system {“pause”);

return 0;return 0;

}}

Page 65: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

65

Functions

Q: Define a function?Q: Define a function?

1.1. A function is a collection of statements that is to be A function is a collection of statements that is to be performed.performed.

2.2. The definition includes:The definition includes:- Return typeReturn type- Function nameFunction name- Parameter listsParameter lists- Body of the functionBody of the function

Q: Write a general format of defining a function?Q: Write a general format of defining a function?

Return type Function name (parameter lists)Return type Function name (parameter lists)

{{

Body of the function;Body of the function;

}}

Page 66: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

66

Q: Mention the benefit of using functions?

The function divides a big problems to smaller ones.The function divides a big problems to smaller ones.

Q:What is a void data type function ?Q:What is a void data type function ?

It is a function that returns no value.It is a function that returns no value.

Q: Write a general format of calling a function?Q: Write a general format of calling a function?

Function name( )Function name( )

Program 18Program 18

Q: Write a c++ program that have the following functions:Q: Write a c++ program that have the following functions:

1.1. MainMain

2.2. FirstFirst

3.3. SecondSecond

Then show the out put screenThen show the out put screen

Page 67: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

67

.#include<iostream>#include<iostream>

using namespace std;using namespace std;

void First( )void First( )

{{

cout<<“Now iam inside the first function”<<endl;cout<<“Now iam inside the first function”<<endl;

}}

void second( )void second( )

{{

cout<<“Now iam inside the second function”<<endl;cout<<“Now iam inside the second function”<<endl;

}}

int main( )int main( )

{{

cout<<“Iam in the main function”<<endl;cout<<“Iam in the main function”<<endl;

Page 68: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

68

.First( ); // call a first functionFirst( ); // call a first function

Second( ); // call the second functionSecond( ); // call the second function

cout<<“Back to the main function”<<endl;cout<<“Back to the main function”<<endl;

system(“pause”);system(“pause”);

return 0;return 0;

}}

----------------------------------------------------------------------------------------------------------------------------

Iam in the main function Now Iam inside the first function Now Iam inside the second functionBack to the main function

Page 69: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

69

Program 19 Q; Using a function sum(), write a c++ program that add any two integer numbers#include<iostream>#include<iostream>

using namespace std;using namespace std;

int sum(int, int);int sum(int, int);

int main( )int main( )

{{

int val1= 50,val2=90, Total;int val1= 50,val2=90, Total;

Total=sum(val1,val2); Total=sum(val1,val2);

cout<<“the sum of val1 and val2 is : “<<Total<<endl;cout<<“the sum of val1 and val2 is : “<<Total<<endl;

system(“pause”);system(“pause”);

return 0;return 0;

}}

Page 70: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

70

.

Int sum(num1, num2)Int sum(num1, num2)

{{

return num1+num2;return num1+num2;

}}

----------------------------------------------------------------------------------------------------------------------------------------------------

Page 71: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

71

.

Program 20Program 20

Q:Write a c++ program using a function square( ), to Q:Write a c++ program using a function square( ), to calculate the square of any integer number entered calculate the square of any integer number entered by the userby the user

#include<iostream>#include<iostream>

using namespace std:using namespace std:

int main( )int main( )

{{

int a;int a;

cin>>a;cin>>a;

cout<<“square of”<<a<<“is”<<square(a)<<endl;cout<<“square of”<<a<<“is”<<square(a)<<endl;

system(“pause”);system(“pause”);

return 0;return 0;

}}

Page 72: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

72

.int square(int a);int square(int a);

{{

return a*a;return a*a;

}}

--------------------------------------------------------

Page 73: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

73

Arrays

Q: Define an array?Q: Define an array?

An array is a variable that can store multiple values of the An array is a variable that can store multiple values of the same type.same type.

We have to deal with:We have to deal with:

1.1. One dimensional arrayOne dimensional array

2.2. Two dimensional arrayTwo dimensional array

Q:Write a general format of declaration of one Q:Write a general format of declaration of one dimensional array?dimensional array?

Data type Array Name [Array Size];

Page 74: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

74

Q:Write a general format declaring two dimensional array?

.. Data type Array name [number of rows][number of columns];

Program 21Q: Write a c++ program that enable you to enter any 8 integers and display them in one dimensional array form

#include<iostream>using namespace std;int main{int A[8];

Page 75: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

75

.for(int i=1; i<=8; i++)for(int i=1; i<=8; i++)

{{

cout <<“enter A[i]”<<endl;cout <<“enter A[i]”<<endl;

cin>>A[i];cin>>A[i];

}}

for(int i=1; i<=8; i++)for(int i=1; i<=8; i++)

{{

cout A[i];cout A[i];

}}

system(“pause”)system(“pause”)

return 0;return 0;

}}

Page 76: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

76

Program 22Q:Write a c++ program that displays the sum of the elements of the array defined as:int sum [4] = {34, 30, 20, 56};

#include<iostream>#include<iostream>

using namespace std;using namespace std;

int main( )int main( )

{{

int s[4]={34, 30, 20, 56};int s[4]={34, 30, 20, 56};

int sum = 0;int sum = 0;

for(int i=1; i<=4;i++)for(int i=1; i<=4;i++)

sum = sum+s[i];sum = sum+s[i];

Page 77: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

77

cout<<“sum is: “<<sum;cout<<“sum is: “<<sum;system(“pause”);system(“pause”);return 0;return 0;}}

----------------------------------------------------------------------------------------

Page 78: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

78

Program 23Write a c++ program that calculates the sum of two given arrays

#include<iostream>#include<iostream>

using namespace std;using namespace std;

void main()void main()int a[4]={2,4,6,8}, i,b[4]={1,3,5,6},c[4];int a[4]={2,4,6,8}, i,b[4]={1,3,5,6},c[4];

for(i=0;i<=3;i++)for(i=0;i<=3;i++)

{c[i]=a[i]+b[i];{c[i]=a[i]+b[i];

cout<<c[i]<<" ";cout<<c[i]<<" ";

}}

system (“pause”)system (“pause”)

returnreturn

}}

Page 79: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

79

Pointers

Q:Define pointers?Q:Define pointers?

Pointers are special type of variables that do not Pointers are special type of variables that do not contain values, but contain addresses of these contain values, but contain addresses of these values.values.

Q: Write a general format of declaring pointers?Q: Write a general format of declaring pointers?

<variable type> * <variable name>;

Example:int *xptr

Page 80: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

80

Q: How can we assign address to pointer variable/

Assigning an address to a pointer variable:Assigning an address to a pointer variable:int *intptr;int *intptr;intptr = &num;intptr = &num;

Memory layout:Memory layout:

num intptr25 0x4a00

address of num: 0x4a00

Page 81: 1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.

81

.

التوفيق التوفيق وبالله وبالله