1 2 3 Set total to zero Set

  • Slides: 37
Download presentation

 ﺗکﺮﺍﺭ ﺑﺎ ﺍﺳﺘﻔﺎﺩﻩ ﺍﺯ ﺷﻤﺎﺭﻧﺪﻩ ﺍﻟگﻮﺭﻳﺘﻢ ﻣﺜﺎﻝ 1. 2. 3. Set total to

ﺗکﺮﺍﺭ ﺑﺎ ﺍﺳﺘﻔﺎﺩﻩ ﺍﺯ ﺷﻤﺎﺭﻧﺪﻩ ﺍﻟگﻮﺭﻳﺘﻢ ﻣﺜﺎﻝ 1. 2. 3. Set total to zero Set grade counter to one While grade counter is less than or equal to ten 1. 2. 3. 4 Input the next grade Add the grade into the total Add one to the grade counter Set the class average to the total divided by ten Print the class average l

 ﺑﺮﻧﺎﻣﻪ ﻣﺜﺎﻝ ﻗﺒﻞ int total; // sum of grades entered by user int

ﺑﺮﻧﺎﻣﻪ ﻣﺜﺎﻝ ﻗﺒﻞ int total; // sum of grades entered by user int grade. Counter; // number of the grades to be entered next int grade; // grade value entered by user int average; // average of grades // initialization phase total = 0; // initialize total grade. Counter = 1; // initialize loop counter // processing phase ﺷﻮﺩ ﻣﻲ ﺯﻳﺎﺩ ﻭﺍﺣﺪ ﻳک ﻣﺮﺗﺒﻪ ﻫﺮ ﺩﺭ ﺣﻠﻘﻪ ﺷﻤﺎﺭﻧﺪﻩ while ( grade. Counter <= 10 ){ // loop 10 times ﺭﺳﺪ ﻣﻲ 10 ﺑﻪ ﻧﻬﺎﻳﺘﺎ ﻭ cout << "Enter grade: "; // prompt for input cin >> grade; // input next grade total = total + grade; // add grade to total grade. Counter = grade. Counter + 1; // increment counter by } // end while // termination phase average = total / 10; // integer division yields integer result // display total and average of grades cout << "n. Total of all 10 grades is " << total << endl; cout << "Class average is " << average << endl; 5

Top-Down Design ﻃﺮﺍﺣﻲ ﺑﺎﻻ ﺑﻪ پﺎﻳﻴﻦ ﺷکﻞ کﻠﻲ ﻣﺴﺎﻟﻪ ﺭﺍ ﺩﺭ ﻗﺎﻟﺐ ﺷﺒﻪ کﺪ

Top-Down Design ﻃﺮﺍﺣﻲ ﺑﺎﻻ ﺑﻪ پﺎﻳﻴﻦ ﺷکﻞ کﻠﻲ ﻣﺴﺎﻟﻪ ﺭﺍ ﺩﺭ ﻗﺎﻟﺐ ﺷﺒﻪ کﺪ ﺑﻴﺎﻥ l : کﻨﻴﺪ Determine the class average for the quiz ﺍﻳﻦ ﺷکﻞ کﻠﻲ ﺭﺍ ﺑﻪ ﻣﺴﺎﻳﻞ کﻮچکﺘﺮ l : ﺑﺸکﻨﻴﺪ Initialize variables Input, sum and count the quiz grades Calculate and print the class average 7

 ﺗﻘﺴﻴﻢ گﺎﻡ ﺑﻪ گﺎﻡ ﺗﻘﺴﻴﻢ l Input, sum and count the quiz grades

ﺗﻘﺴﻴﻢ گﺎﻡ ﺑﻪ گﺎﻡ ﺗﻘﺴﻴﻢ l Input, sum and count the quiz grades ﺑﻪ Input the first grade (possibly the sentinel) While the user has not as yet entered the sentinel Add this grade into the running total Add one to the grade counter Input the next grade (possibly the sentinel) Calculate and print the class average ﺑﻪ 9 If the counter is not equal to zero Set the average to the total divided by the counter Print the average Else Print “No grades were entered”

 ﺗکﺮﺍﺭ ﻧﺎ ﻣﻌﻴﻦ : ﻣﺜﺎﻝ cout << "Enter grade or -1 to quit:

ﺗکﺮﺍﺭ ﻧﺎ ﻣﻌﻴﻦ : ﻣﺜﺎﻝ cout << "Enter grade or -1 to quit: "; cin >> grade; while ( grade != -1 ){ total = total + grade; grade. Counter = grade. Counter + 1; cout << "Enter grade or -1 to quit: "; cin >> grade; } // end while 10

 ﺳﺎﺧﺘﺎﺭﻫﺎﻱ کﻨﺘﺮﻟﻲ ﺗﻮ ﺩﺭ ﺗﻮ ﺍﻭﻟﻴﻦ ﺗﻘﺴﻴﻢ l Initialize variables Input the ten

ﺳﺎﺧﺘﺎﺭﻫﺎﻱ کﻨﺘﺮﻟﻲ ﺗﻮ ﺩﺭ ﺗﻮ ﺍﻭﻟﻴﻦ ﺗﻘﺴﻴﻢ l Initialize variables Input the ten quiz grades and count passes and failures Print a summary of the exam results and decide if tuition should be raised ﺗﻘﺴﻴﻢ Initialize variables ﺑﻪ Initialize passes to zero Initialize failures to zero Initialize student counter to one 12 l

 ﺳﺎﺧﺘﺎﺭﻫﺎﻱ کﻨﺘﺮﻟﻲ ﺗﻮ ﺩﺭ ﺗﻮ ﺗﻘﺴﻴﻢ l Input the ten quiz grades and

ﺳﺎﺧﺘﺎﺭﻫﺎﻱ کﻨﺘﺮﻟﻲ ﺗﻮ ﺩﺭ ﺗﻮ ﺗﻘﺴﻴﻢ l Input the ten quiz grades and count passes and failures ﺑﻪ While student counter is less than or equal to ten Input the next exam result If the student passed Add one to passes Else Add one to failures Add one to student counter Print a summary of the exam results and decide if tuition should be raised ﺑﻪ 13 Print the number of passes Print the number of failures If more than eight students passed Print “Raise tuition”

1 // Fig. 2. 11: fig 02_11. cpp 2 // Analysis of examination results

1 // Fig. 2. 11: fig 02_11. cpp 2 // Analysis of examination results 3 #include <iostream> 4 5 using std: : cout; 6 using std: : cin; 7 using std: : endl; 8 9 1. Initialize variables 2. Input data and count passes/failures int main() 10 { 11 // initialize variables in declarations 12 int passes = 0, // number of passes 13 failures = 0, // number of failures 14 student. Counter = 1, // student counter 15 result; // one exam result 16 17 // process 10 students; counter-controlled loop 18 while ( student. Counter <= 10 ) { 19 cout << "Enter result (1=pass, 2=fail): "; 20 cin >> result; 21 22 if ( result == 1 ) // if/else nested in while 14 23 passes = passes + 1;

24 else 25 failures = failures + 1; 26 27 student. Counter = student.

24 else 25 failures = failures + 1; 26 27 student. Counter = student. Counter + 1; 28 } 29 30 // termination phase 31 cout << "Passed " << passes << endl; 32 cout << "Failed " << failures << endl; 33 34 if ( passes > 8 ) 35 cout << "Raise tuition " << endl; 36 37 return 0; // successful termination 38 } Enter result (1=pass, 2=fail): 1 Enter result (1=pass, 2=fail): 2 Enter result (1=pass, 2=fail): 1 Enter result (1=pass, 2=fail): 1 Passed 9 Failed 1 Raise tuition Program Output 15

for ﺣﻠﻘﻪ ﺗکﺮﺍﺭ چﻨﻴﻦ ﺍﺳﺖ for ﺷکﻞ کﻠﻲ ﺣﻠﻘﻪ ﺗکﺮﺍﺭ l for ( initialization;

for ﺣﻠﻘﻪ ﺗکﺮﺍﺭ چﻨﻴﻦ ﺍﺳﺖ for ﺷکﻞ کﻠﻲ ﺣﻠﻘﻪ ﺗکﺮﺍﺭ l for ( initialization; Loop. Continuation. Test; increment ) statement : ﻣﺜﺎﻝ for( int counter = 1; counter <= 10; counter++ ) cout << counter << endl; No semicolon after last statement 21 ﺭﺍ چﺎپ ﻣﻲ کﻨﺪ 10 ﺗﺎ 1 ﺍﻋﺪﺍﺩ ﺍﺯ – l

 ﻣﺜﺎﻝ - for ﺣﻠﻘﻪ ﺗکﺮﺍﺭ • Program to sum the even numbers from

ﻣﺜﺎﻝ - for ﺣﻠﻘﻪ ﺗکﺮﺍﺭ • Program to sum the even numbers from 2 to 100 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // Fig. 2. 20: fig 02_20. cpp // Summation with for #include <iostream> using std: : cout; using std: : endl; int main() { int sum = 0; for ( int number = 2; number <= 100; number += 2 ) sum += number; cout << "Sum is " << sum << endl; return 0; } Sum is 2550 23

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 variables // Fig. 2. 22: fig 02_22. cpp // Counting letter grades #include <iostream> using std: : cout; using std: : cin; using std: : endl; 2. Input data int main() { int grade, // one grade a. Count = 0, // number of A's b. Count = 0, // number of B's c. Count = 0, // number of C's d. Count = 0, // number of D's f. Count = 0; // number of F's 2. 1 Use switch loop to update count cout << "Enter the letter grades. " << endl << "Enter the EOF character to end input. " << endl; while ( ( grade = cin. get() ) != EOF ) { Notice how the case statement is used switch ( grade ) { // switch nested in while case 'A': // grade was uppercase A case 'a': // or lowercase a ++a. Count; break; // necessary to exit switch case 'B': // grade was uppercase B case 'b': // or lowercase b ++b. Count; break; 25

35 36 37 38 39 40 41 42 43 44 45 46 47 48

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 case 'C': // grade was uppercase C case 'c': // or lowercase c ++c. Count; break; case 'D': // grade was uppercase D break causes switch to end and case 'd': // or lowercase d the program continues with the first ++d. Count; statement after the switch structure. break; 2. 1 Use switch loop to update count Notice the default statement. case 'F': // grade was uppercase F case 'f': // or lowercase f ++f. Count; break; case 'n': // ignore newlines, case 't': // tabs, case ' ': // and spaces in input break; default: // catch all other characters cout << "Incorrect letter grade entered. " << " Enter a new grade. " << endl; break; // optional } } 3. Print results cout << "nn. Totals for each letter grade are: " << "n. A: " << a. Count << "n. B: " << b. Count << "n. C: " << c. Count << "n. D: " << d. Count << "n. F: " << f. Count << endl; 26 return 0; }

Enter the letter grades. Enter the EOF character to end input. a B c

Enter the letter grades. Enter the EOF character to end input. a B c C A d f C E Incorrect letter grade entered. Enter a new grade. D A b Totals for each letter grade are: A: 3 B: 2 C: 3 D: 2 F: 1 Program Output 27

 ﻋﻤﻠگﺮﻫﺎﻱ ﻣﻨﻄﻘﻲ l && (logical AND) – l || (logical OR) – l

ﻋﻤﻠگﺮﻫﺎﻱ ﻣﻨﻄﻘﻲ l && (logical AND) – l || (logical OR) – l Returns true if both conditions are true Returns true if either of its conditions are true ! (logical NOT, logical negation) – – ﻧﺘﻴﺠﻪ ﻋﺒﺎﺭﺕ ﺷﺮﻃﻲ ﺭﺍ ﻣﻌکﻮﺱ ﻣﻲ کﻨﺪ ﻳﻌﻨﻲ ﺭﻭﻱ ﻳک ﻋﻤﻠﻮﻧﺪ ﻋﻤﻞ ﻣﻲ کﻨﺪ. ﻋﻤﻠگﺮ ﺗکﻲ ﺍﺳﺖ ﺟﺪﻭﻝ ﺍﺭﺯﺵ ﺗﺮکﻴﺐ گﺰﺍﺭﻩ ﻫﺎ Expression Result true && false true || false true !false true 31 l