Download presentation
Presentation is loading. Please wait.
Published byMartin Curtis Modified over 9 years ago
1
1 2/6/1435 h Wednesday Lecture 8
2
2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division cout << 13 % 5; // displays 3 2. % requires integers for both operands cout << 13 % 5.0; // error
3
3 Q:What are Comments? 1. Used to document parts of the program 2. Are ignored by the compiler 3. Single-Line Comments Begin with // through to the end of line: 4. Multi-Line Comments Begin with /*, end with */ Begin with /*, end with */ Can span multiple lines: example Can span multiple lines: example
4
4 - /* this is a multi-line comment comment*/ Can begin and end on the same line: int area; /* calculated area */
5
5 Q:When does / (division) operator performs integer division 1. 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 2. 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
6 Named Constants Q: Define a named constant(constant variable)? 1. It is a variable whose content cannot be changed during program execution 2. Used for representing constant values with descriptive names: 3. Often named in uppercase letters const double TAX_RATE = 0.0675; const int NUM_STATES = 50;
7
7 The cin Object Q:What is a cin object? 1. Standard input object 2. Like cout, requires iostream file 3. Used to read input from keyboard 4. Information retrieved from cin with >> 5. Input is stored in one or more variables
8
8 - 6. Can be used to input more than one value: cin >> height >> width; 7. Multiple values from keyboard must be separated by spaces 8. Order is important: first value entered goes to first variable, etc.
9
9 - 9. cin converts data to the type that matches the variable: int height; cout<< How tall is the room? "; cin >> height;
10
10 Displaying a Prompt Q: What is a prompt? 1. A prompt is a message that instructs the user to enter data. 2. You should always use cout to display a prompt before each cin statement. cout > height;
11
11 Algorithms and Flowcharts Q: 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
12 Flowchart Symbol Algorithm Instruction Flowchart Symbol START / RETURN / END / EXIT ENTER / PRINT / WRITE (input/output statement) Variable / Expression (assignment statement) PROCESS (module call)
13
13. اتخاذ القرار Flow lines
14
14 Q: Mention types of Logic Structures? 1. Sequential logic structure 2. Selection logic structure 3. Loop logic structure
15
15 1. Sequential Logic Structure Q: Write an algorithm,design a flowchart then write a program that represents a sequential logic structure Algorithm Algorithm 1. Enter any three numbers :A,B,C 2. Sum = A+B+C 3. Mult = A*B*C 4. Print Sum, Mult 5. End Start END Enter A,B,C PRINT NAME, Sum, Mult Sum = A+B+C Mult = A*B*C
16
16 Program 4 Write 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
17.sum=a+b+c;mult=a*b*c;cout<<“sum=“<<sum<<endl<<“mult=“<<mult<<endl;system(“pause”);return;} The output screen: Enter any three numbers 7 6 8 Sum=21 Mul= 336
18
18 Program 5 Write 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
19. cout<<“centigrade =“<<centigrade; system(“pause”);return;} The output screen : Enter Fahrenheit 95 Centigrade is 35
20
20 Program 6 Q: Write a c++ program that calculates the rectangle’s area and displays the value on the screen Note: 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
21 0 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; } 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
22
Making Decisions Control Structures 1. Selection Structure 2. Loop Structure
23
23 1. Selection Structure The if Statement The if Statement
24
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
25 Q:.Mention Types of IF statements 1. If statement with a null false branch 2. The if/else Statement 3. Nested if Statements 4. The if/else if Statement
26
26 Draw a Flowchart for an if statement with a null false branch
27
27 Flowchart for Evaluating a Decision
28
28 Q:Write a general format for an if Statement with a null false branch? if (expression) statement;
29
29 if Statement Notes 1. Do not place ; after (expression) 2. Place statement; on a separate line after (expression), indented: if (score > 90) grade = 'A';
30
30 Q:How an if Statement being evaluated To evaluate: if (expression) statement; statement; 1. If the expression is true, then statement is executed. 2. If the expression is false, then statement is skipped.
31
31 Program 7 Q:Write a c++ program that get three test score and display the average, then If the average is greater than 95 display(congratulation! This is a high score) ---------------------------------------------------------- P if else
32
32 The Program is: #include using namespace std; int main() { int score1, score2, score3 > score1>> score2>> score3; average = (score1+score2+score3)/3;
33
33 Cout 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
34 Expanding the if Statement Q: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 if Statement if (expression) { statement1; statement1; statement2; statement2;} if (score > 90) { grade = 'A'; cout << "Good Job!\n"; }
35
35 2. The if/else Statement Q:Define an if/else statement? 1. Provides two possible paths of execution 2. Performs one statement if the expression is true, otherwise performs another statement.
36
36 Q: Write a general format for the if/else statement? General Format: if (expression) statement1; else statement2;
37
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
38 Program 8 #include using namespace std; int main() { int number; cout >number; if (number % 2 ==0) cout<<number <<“is even \n”;
39
39 else cout<<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
40 Program 9 no Q:Write a c++ program to enter a salary, if the salary is >= 5000 then tax is 30%,otherwise tax is 10% #include using namespace std; int main() { int salary; float tax; cout >salary; if (salary>=5000) { tax = salary*0.3; cout<<“tax is:”<<tax; }
41
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
42 3. Nested if Statements Q: 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 Nested if statements can be used to test more than one condition
43
43 4. The if/else if Statement Q:What is an if/else if Statement 1. It tests a series of conditions until one is found to be true 2. Often simpler than using nested if/else statements
44
44 Q: Write a general format of if/else if if (expression) statements ; statements ; else if (expression) statements ;. statements ;. // other else ifs // other else ifs else if (expression) else if (expression) statements ; statements ;else
45
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
46
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 = 90 displays 'A' Score >= 80 displays 'B' Score >= 70 displays 'C' Score >= 60 displays 'D' Score <60 displays (Fail)
47
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
48. else if(score>=70) cout<<“C\n”; else if(score>=60) cout<<“D\n”;elsecout<<“Fail/n”;system(“pause”); return 0; }
49
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 = 90 displays 'A' Score >= 80 displays 'B' Score >= 70 displays 'C' Score >= 60 displays 'D' Score < 60 displays (Fail)
50
50. # include # include 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 }
51
51 2. Loop Structure First we have to define a counter Q:Define a counter? It is a variable that is incremented or decremented according to the loop counter. Q: When do we initialize the counter? The counter must be initialized before entering the loop.
52
52. Q: Define a loop? It is a control structure that causes statements to be executed more than once Q: Mention the types of loop structure? 1. While 2. Do-while 3. for
53
53 1. While Loop Structure Q: Write a general format for a while loop structure? while (expression) Statements; Q:Design a flowchart that represents a while loop structure? Loop body expression true false
54
54 Q: How does while loop work? 1. The expression is evaluated 2. If it is true, the statements in the body of the loop is executed 3. The expression is evaluated again and again till it becomes false then the loop stops
55
55 Program 12 Q:Using while loop write a c++ program that displays numbers from 1-10, and their squares? # include # include using namespace std; int main( ) { int count = 1; while(count<=10){ cout<< count<<“ “<<count*count; count++}system(“pause”); return 0; }
56
56 Program 13 Write a c++ program that allows the user to enter scores for 4 subjects; then displays the average? #include<iostream> using namespace std; int main( ) { sum = 0; float average; float score; cout<<“Enter the score1”; cin>> score1; cout<<“Enter the score2”; cin>> score2; cout<<“Enter the score3”; cin>> score3;
57
57. cout<<“Enter the score4”; cin>> score4; sum = score1+score2score3+score4; average = sum/ cout<<“average is”<<average<<endl; system(“pause”); return 0; }
58
58 Program 14 Using while loop, write a c++ program that allows the user to enter scores for 4 subjects; then displays the average?#include<iostream> using namespace std; int main( ) { int count = 1; sum = 0; float average; float score; while(count<=4){ cout<<“Enter the score”;
59
59. cin>> score; sum = sum+score; counter =counter +1; }cout<<endl; average = sum/4; cout<<“average is”<<average<<endl; system(“pause”); return 0; }-------------------------------------------------------------------------
60
60 2.DO-While loop Q: Define do-while loop? It tests the condition after executing the body of the loop. Q:Draw a flowchart for the do-while loop? Body of the loop expression true false
61
61 Q: Write a general format for do-while loop? Do{ Statement 1; Statement 2 ;..While(condition) Q:Using flowcharts, compare between [While] and [Do..While] control loop Q: Define an infinite loop? It is a loop that never finished
62
62 Program 15 Q: Using do-while loop, write a c++ program that prints numbers from 1-10 # include # include using namespace std; int main( ) { int counter = 1 Do{ cout<<counter<<“ “; count++} while ( counter<=10) system(“pause”); return 0; }
63
63 3.For Loop Q: Write a general format of the For loop? For(initial value; expression; updating statement) ------------------------------------------------------------------------ Program 16 Program 16 Q: Write a c++ program that displays the numbers from 1-10 using for loop? #include<iostream> using namespace std; Int main( ) { for(int counter = 1; counter<=10; counter++) cout<<counter<<endl; System (“pause”); return 0; }
64
64 Program 17 Q: Write a c++ program that displays the numbers from 10- 1 using for loop? #include<iostream> using namespace std; int main( ) { for(int counter = 10; counter<=1; counter++) cout<<counter<<endl; system {“pause”); return 0; }
65
65 Functions Q: Define a function? 1. A function is a collection of statements that is to be performed. 2. The definition includes: - Return type - Function name - Parameter lists - Body of the function Q: Write a general format of defining a function? Return type Function name (parameter lists) { Body of the function; }
66
66 Q: Mention the benefit of using functions? The function divides a big problems to smaller ones. Q:What is a void data type function ? It is a function that returns no value. Q: Write a general format of calling a function? Function name( ) Program 18 Q: Write a c++ program that have the following functions: 1. Main 2. First 3. Second Then show the out put screen
67
67. #include<iostream> using namespace std; void First( ) { cout<<“Now iam inside the first function”<<endl; } void second( ) { cout<<“Now iam inside the second function”<<endl; } int main( ) { cout<<“Iam in the main function”<<endl;
68
68. First( ); // call a first function Second( ); // call the second function cout<<“Back to the main function”<<endl; system(“pause”); return 0; }-------------------------------------------------------------- Iam in the main function Now Iam inside the first function Now Iam inside the second function Back to the main function
69
69 Program 19 Q; Using a function sum(), write a c++ program that add any two integer numbers #include<iostream> using namespace std; int sum(int, int); int main( ) { int val1= 50,val2=90, Total; Total=sum(val1,val2); cout<<“the sum of val1 and val2 is : “<<Total<<endl; system(“pause”); return 0; }
70
70. Int sum(num1, num2) { return num1+num2; }--------------------------------------------------------------------------
71
71. Program 20 Program 20 Q:Write a c++ program using a function square( ), to calculate the square of any integer number entered by the user #include<iostream> using namespace std: int main( ) { int a; cin>>a; cout<<“square of”<<a<<“is”<<square(a)<<endl; system(“pause”); return 0; }
72
72. int square(int a); { return a*a; }----------------------------
73
73 Arrays Q: Define an array? An array is a variable that can store multiple values of the same type. We have to deal with: 1. One dimensional array 2. Two dimensional array Q:Write a general format of declaration of one dimensional array? Data type Array Name [Array Size];
74
74 Q:Write a general format declaring two dimensional array?. Data type Array name [number of rows][number of columns]; Program 21 Q: Write a c++ program that enable you to enter any 8 integers and display them in one dimensional array form #include using namespace std; int main { int A[8];
75
75. for(int i=1; i<=8; i++) { cout <<“enter A[i]”<<endl; cin>>A[i];} for(int i=1; i<=8; i++) { cout A[i]; }system(“pause”) return 0; }
76
76 Program 22 Q: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> using namespace std; int main( ) { int s[4]={34, 30, 20, 56}; int sum = 0; for(int i=1; i<=4;i++) sum = sum+s[i];
77
77 cout<<“sum is: “<<sum; system(“pause”); return 0; } --------------------------------------------
78
78 Program 23 Write a c++ program that calculates the sum of two given arrays #include<iostream> using namespace std; void main() int a[4]={2,4,6,8}, i,b[4]={1,3,5,6},c[4]; for(i=0;i<=3;i++){c[i]=a[i]+b[i]; cout<<c[i]<<" "; } system (“pause”) return}
79
79 Pointers Q:Define pointers? Pointers are special type of variables that do not contain values, but contain addresses of these values. Q: Write a general format of declaring pointers? * ; Example: int *xptr
80
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; intptr = # Memory layout: Memory layout: numintptr 25 0x4a00 address of num : 0x4a00
81
81. وبالله التوفيق
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.