Download presentation
Presentation is loading. Please wait.
Published byStewart Warren Modified over 9 years ago
1
1 CS 1430: Programming in C++
2
2 Input: 45.5 55 60.5 39.5 -1 Input ends with -1 Sentinel-Controlled Loop Input: 4 45.5 55 60.5 39.5 Input begins with 4 (count) Count-Controlled Loop
3
Input: 4 45.5 55 60.5 39.5 Variables totalCount: number of scores to process loopCount: number of iterations the loop body has been executed (number of scores processed) Pseudo Code Input totalCount Set loopCount to 0 // LCV Initializing other variables While loopCount < totalCount Input the score Process score Increase loopCount by 1 3
4
How to Initialize? Range is known const float MIN_SCORE = 0.0; const float MAX_SCORE = 60.0; // Initialize max max = MIN_SCORE; // Initialize min min = MAX_SCORE; // Initialize total total = 0; 4 Range is NOT known // Input the first score cin >> score; // Initialize max max = score; // Initialize min min = score; // Initialize total total = score;
5
// The range is known const float MIN_SCORE = 0.0; const float MAX_SCORE = 60.0; float score, max = MIN_SCORE; int totalCount, loopCount; // Interactive mode cout << "Enter the count of scores: "; cin >> totalCount; loopCount = 0; // No prime read of score! while (loopCount < totalCount) { // Interactive mode cout << "Enter a score: "; cin >> score; if (score MAX_SCORE) cout << "Invalid score: " << score; else { if (score > max) max = score; } loopCount ++; } // Four Parts of Loop 5
6
// Another way: No loopCount float score, max = MIN_SCORE; int totalCount; // Batch mode: no input prompt // cout << "Enter the count of scores: "; cin >> totalCount; while (totalCount > 0) { // Batch mode: no input prompt // cout << "Enter a score: "; cin >> score; if (score MAX_SCORE) cout << "Invalid score: " << score; else { if (score > max) max = score; } totalCount --; // totalCount = totalCount - 1; } // What’s the value of totalCount after the loop? 6
7
// The range is NOT known float score, max; int totalCount, loopCount; // Batch mode cin >> totalCount; loopCount = 0; while (loopCount < totalCount) { // Batch mode cin >> score; if (loopCount == 0) // first score max = score; else { if (score > max) max = score; } loopCount ++; } 7
8
// The range is NOT known float score, max; int totalCount, loopCount; // Batch mode cin >> totalCount; loopCount = 0; while (loopCount < totalCount) { loopCount ++; // Batch mode cin >> score; if (loopCount == 1) // first score max = score; else { if (score > max) max = score; } 8
9
Average and Max of n Scores Range is known Invalid scores are not considered 9 Initialize (total, max, validCount) Input totalCount While totalCount > 0 Input a score If score out of range display a message Else Increase validCount by one add score to total if score > max max = score Decrease totalCount by one Compute average using validCount and total
10
// Average and Max of n Scores // Range is known and invalid scores are not considered float score, max = MIN_SCORE, total = 0.0, average; int totalCount, validCount = 0; cin >> totalCount; while (totalCount > 0) { cin >> score; if (score MAX_SCORE) cout << "Invalid score: " << score; else { validCount ++; total += score; // total = total + score; if (score > max) max = score; } totalCount --; } average = total / validCount; // Any problems? 10
11
// validCount could be 0! average = total / validCount; // while loop to compute max, total and validCount while (totalCount > 0) { … } if (validCount > 0) { average = total / validCount; cout << endl << "max = " << max; cout << endl << "average = " << average; // cout << endl << "average = " << total / validCount; } else cout << endl << "No valid scores entered."; 11
12
More Arithmetic Operators validCount ++; // validCount = validCount + 1; totalCount --; // totalCount = totalCount - 1; total += score; // total = total + score; total -= score; // total = total - score; yValue /= xValue; // yValue = yValue / xValue; yValue %= xValue; // yValue = yValue % xValue; yValue *= xValue; // yValue = yValue * xValue; 12
13
Example float num, total = 1; // DO_01: // Input a num // If it’s positive // Add it to total // Otherwise // Multiply it to total cin >> num; if (num > 0) total += num; else total *= num; 13
14
Example int num; cin >> num; // DO_02: // While num is not positive // Increment its value by 2 while (num <= 0) num += 2; 14
15
Example float num, total; cin >> total; // DO_03: // If total is negative // Input a value to num and add it to total // until total is not negative while (total < 0) { cin >> num; total += num; } 15
16
Factorial N! = 1 * 2 * 3 *…* (N - 1) * N 0! = 1 1! = 1 2! = 1 * 2 = 2 … 5! = 1 * 2 * 3 * 4 * 5 = 120 … 16
17
Factorial Pseudo Code Input Num (assume Num >= 0) Fact = 1 loopCount = 0 While loopCount < Num loopCount ++ Fact = Fact * loopCount Output Fact 17 N! = 1 * 2 * 3 *…* (N - 1) * N 0! = 1 1! = 1 2! = 1 * 2 = 2 … 5! = 1 * 2 * 3 * 4 * 5 = 120 … How to compute n! for any n?
18
Factorial Pseudo Code Input Num Fact = 1 loopCount = Num While loopCount > 1 Fact = Fact * loopCount loopCount -- Output Fact Correct! 18 Pseudo Code Input Num Fact = 1 loopCount = Num While loopCount > 1 loopCount -- Fact = Fact * loopCount Output Fact Correct? NO!
19
Schedule Program 1 Due Time : Tuesday by 10 pm Grace time: Friday by 10pm Quiz3-3 Due 10 PM Wednesday Lab2 Tuesday by 5 pm Lab3 Tuesday evening 19
20
20 Lab 3 If no differences by 5 pm Thursday Five (5) points Else if no differences by 5 pm the following Tuesday Three (3) points Else No Points! Same for other Labs Unless specified otherwise.
21
21 Program 1 Differences: NONE Follow the Requirements! (At least one Nested IF statement) Style: Could lose up to 7 points! Save your log file Print log file for me to check style or Drop your UserName_Prog1.cpp file to K:\Courses\CSSE\yangq\DropBox
22
22 Program 1 If submit by the due time and no differences Two bonus points on style (total cannot be more than 15 points) Else if submit by the grace time and no differences May still get 15 points Else if submit after grace time and pass test 1 Zero points Could pass CS143 Else Retake CS143 next semester
23
23 Program 1: One Nested IF statement! if (...) {... else... } else if (...) {... } else if (...)... else { if (...)... }
24
24 Two IF statements if (...) {... } else if (...) {... } else if (...)... else { } if (...)... else...
25
25 Style ------------- File with line numbers added: Prog1.cpp ------------------------- 1: 2: #include 3: #include 4: using namespace std; 5: 6: int main() 7: { 8:... Miss Comment Block! Could lose 3 points!
26
26 Style ------------- File with line numbers added: Prog1.cpp ------------------------- 1: //--------------------------------------------------------------------- 2: // Name: Qi Yang 3: // 4: // Course: CS 143, Section 9, Fall 2015 5: // 6: // Purpose: This program converts between kilometers and 7: // miles or between Fahrenheit temperature and Celsius. 8: // 9: // Input : This program accepts the following prompted input 10: // from the keyboard: 13: // 14: // Output: This program provides the following output prompts to 15: // standard output (the monitor): 16: // "Input a type: kilometers, miles, fahrenheit, or celsius; 17: // followed by a space and then an amount: " 24: //--------------------------------------------------------------------- 25: 26: #include 27: #include 28: using namespace std;
27
27 Style 36: 37: int main() 38: { 30: const float KM_PER_MILE = 1.61; 31: const float FREEZING_F = 32.0; 32: const float FAHR_PER_CELSIUS = 9.0 / 5.0; 39: string units; 40: float amount, conversion; 41: 42: cout << "Input a type: kilometers, miles, fahrenheit, or celsius;" 43: << endl << "followed by a space and then an amount: " << endl; 44: cin >> units >> amount; Constants should be before main() 30: const float KM_PER_MILE = 1.61; 31: const float FREEZING_F = 32.0; 32: const float FAHR_PER_CELSIUS = 9.0 / 5.0; 36: 37: int main() 38: { 39: string units; 40: float amount, conversion; 41:
28
28 Style 30: const float KM_PER_MILE = 1.61; 31: const float FREEZING_F = 32.0; 32: const float FAHR_PER_CELSIUS = 9.0 / 5.0; 36: 37: int main() 38: { 39: string units; 40: float amount, conversion; 41: 42: cout << "Input a type: kilometers, miles, fahrenheit, or celsius;" 43: << endl << "followed by a space and then an amount: " << endl; 44: cin >> units >> amount; Do not indent constants! 30: const float KM_PER_MILE = 1.61; 31: const float FREEZING_F = 32.0; 32: const float FAHR_PER_CELSIUS = 9.0 / 5.0; 36: 37: int main() 38: { 39: string units; 40: float amount, conversion; 41: 42: cout << "Input a type: kilometers, miles, fahrenheit, or celsius;" 43: << endl << "followed by a space and then an amount: " << endl; 44: cin >> units >> amount;
29
29 Style 76: if (amount > 120) 77: { 78: conversion = (amount – 32.0) / FAHR_PER_CELSIUS; 79: cout << endl << "Temperature " << amount << " degrees in " 80: << "Fahrenheit is " << conversion 81: << " degrees in Celsius."; 82: } 83: else Magic Number! 76: if (amount > MAX_FAHR_TEMP) 77: { 78: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 79: cout << endl << "Temperature " << amount << " degrees in " 80: << "Fahrenheit is " << conversion 81: << " degrees in Celsius."; 82: } 83: else
30
30 Style 76: if (amount > MAX_FAHR_TEMP) 77: { 78: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 79: cout << endl << "Temperature " << amount << " degrees in " 80: << "Fahrenheit is " << conversion 81: << " degrees in Celsius."; 82: } 83: else Brace Alignment! 76: if (amount > MAX_FAHR_TEMP) 77: { 78: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 79: cout << endl << "Temperature " << amount << " degrees in " 80: << "Fahrenheit is " << conversion 81: << " degrees in Celsius."; 82: } 83: else
31
31 Style 76: if (amount > MAX_FAHR_TEMP) 77: { 78: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 79: cout << endl << "Temperature " << amount << " degrees in “ << "Fahrenheit is " << conversion << " degrees in Celsius."; 80: } 81: else Line too long! Each line can have at most 74 columns! 76: if (amount > MAX_FAHR_TEMP) 77: { 78: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 79: cout << endl << "Temperature " << amount << " degrees in " 80: << "Fahrenheit is " << conversion 81: << " degrees in Celsius."; 82: } 83: else
32
32 Style 76: if (amount > MAX_FAHR_TEMP) 77: { 78: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 79: cout << endl << "Temperature " << amount << " degrees in “ 80: << "Fahrenheit is " << conversion 81: << " degrees in Celsius."; 82: } 83: else Alignment! 76: if (amount > MAX_FAHR_TEMP) 77: { 78: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 79: cout << endl << "Temperature " << amount << " degrees in " 80: << "Fahrenheit is " << conversion 81: << " degrees in Celsius."; 82: } 83: else
33
33 Style 76: if (amount > MAX_FAHR_TEMP) 77: { 78: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 79: cout << endl << "Temperature " << amount << " degrees in “ 80: << "Fahrenheit is " << conversion 81: << " degrees in Celsius."; 82: } 83: else 84: cout << “Invalid unit!!”; Indentation! 76: if (amount > MAX_FAHR_TEMP) 77: { 78: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 79: cout << endl << "Temperature " << amount << " degrees in " 80: << "Fahrenheit is " << conversion 81: << " degrees in Celsius."; 82: } 83: else 84: cout << “Invalid unit!!”;
34
34 Style 76: if (amount > MAX_FAHR_TEMP) 77: { cout << “Invalid unit!!”;} 78: else 79: { 80: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 81: cout << “The result is good."; 82: } 83: 84: Braces on separate lines! 76: if (amount > MAX_FAHR_TEMP) 77: { 78: cout << “Invalid unit!!”; 79: } 80: else 81: { 82: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 83: cout << “The result is good."; 84: }
35
35 Style 76: if (amount > MAX_FAHR_TEMP) { 77: cout << “Invalid unit!!”;} 78: else { 79: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 80: cout << “The result is good."; 81: } 82: 83: Braces on separate lines! 76: if (amount > MAX_FAHR_TEMP) 77: { 78: cout << “Invalid unit!!”; 79: } 80: else 81: { 82: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 83: cout << “The result is good."; 84: }
36
36 Style 76: if (amount>MAX_FAHR_TEMP) 77: { 78: conversion=(amount - FREEZING_F)/FAHR_PER_CELSIUS; 79: cout << endl << "Temperature " << amount<<" degrees in “ 80: << "Fahrenheit is "<<conversion 81: << " degrees in Celsius."; 82: } 83: else Space before and after operator! 76: if (amount > MAX_FAHR_TEMP) 77: { 78: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 79: cout << endl << "Temperature " << amount << " degrees in " 80: << "Fahrenheit is " << conversion 81: << " degrees in Celsius."; 82: } 83: else
37
37 Style 76: if (amount > MAX_FAHR_TEMP) 77: { 78: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 82: } 83: 84: else if 85: 86: { 87: 88: 89: } 90: 91: else 92: 93: { 94: No blank line before/after else! 76: if (amount > MAX_FAHR_TEMP) 77: { 78: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 79:... 80: } 81: else if 82: { 83:... 84: } 85: else 86: {
38
38 Style 33: const float FAHR_PER_CELSIUS = 9.0 / 5.0; 34: 35: int main() 36: 37: { 38: 39: string units; 40: float amount, conversion; 41: 42: cout << "Input a type: kilometers, miles, fahrenheit, or celsius;" 43: << endl << "followed by a space and then an amount: " << endl; 44: cin >> units >> amount; No blank line before/after brace! 32: const float FAHR_PER_CELSIUS = 9.0 / 5.0; 36: 37: int main() 38: { 39: string units; 40: float amount, conversion; 41: 42: cout << "Input a type: kilometers, miles, fahrenheit, or celsius;" 43: << endl << "followed by a space and then an amount: " << endl; 44: cin >> units >> amount;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.