1 / 20

Write C++ Statements // Decrease theCount by 2 until theCount is negative.

Write C++ Statements // Decrease theCount by 2 until theCount is negative. while ( theCount >= 0 ) { theCount -= 2; } // Another way while ( !(theCount < 0) ) theCount -= 2; // Incorrect! while ( theCount !< 0 ) theCount -= 2; // Incorrect! while ( theCount > 0 )

Download Presentation

Write C++ Statements // Decrease theCount by 2 until theCount is negative.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Write C++ Statements // Decrease theCount by 2 until theCount is negative. while ( theCount >= 0 ) { theCount -= 2; } // Another way while ( !(theCount < 0) ) theCount -= 2; // Incorrect! while ( theCount !< 0 ) theCount -= 2; // Incorrect! while ( theCount > 0 ) theCount -= 2;

  2. Write C++ Statements // While theValue is not positive, increase its value by 5. while ( theValue <= 0 ) { theValue += 5; } // Another way while ( !(theValue > 0) ) theValue += 5; // Incorrect! while ( theValue < 0 ) { theValue += 5; }

  3. Write C++ Statements // While theValue is not positive and theCount is // less than 10, increase theValue by 5 and theCount by 1. while ( theValue <= 0 && theCount < 10 ) { theValue += 5; theCount ++; } // Another way while ( !(theValue > 0 || theCount >= 10) ) { theValue += 5; theCount ++; } // increase theValue by 5 and theCount by 1 // until theValue is positive or theCount is 10 or higher. DeMorgan’s Law

  4. 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 If not emergemcy Lose points Else if submit after grace time and pass tests 1 - 5 Zero points Could pass CS143 Else Retake CS143 next semester Nested IF statement!

  5. Style yangq PROG1 Differences: NONE Your submission as of the above date using HiC, v3.1.8. If this is a programming assignment, please review the assignment writeup and programming ground rules to make sure you didn't miss anything. File: J:\CS143\Programs\Program 1\prog1.cpp: 25: 26: #include <iostream> 27: #include <string> 28: using namespace std; 29: 30: int main() 31: { 32: ... Miss Comment Block! Could lose 3 points!

  6. Style Your submission as of the above date using HiC, v3.1.8. If this is a programming assignment, please review the assignment writeup and programming ground rules to make sure you didn't miss anything. File: J:\CS143\Programs\Program 1\prog1.cpp: 1: //--------------------------------------------------------------------- 2: // Name: Qi Yang 3: // 4: // Course: CS 143, Section 9, Spring 2007 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 <iostream> 27: #include <string> 28: using namespace std;

  7. 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:

  8. 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;

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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!!”;

  14. 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: }

  15. 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

  16. 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: {

  17. 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;

  18. Style 35: const float Fahr_Per_Celsius = 9.0 / 5.0; 36: 37: int main() 38: { 39: string variable; 40: float amount, conversion; 41: Naming conversion! 35: 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;

  19. Style 35: const float FAHR_PER_CELSIUS = 9.0 / 5.0; 36: 37: int main() 38: { 39: string unit; 40: string kilo = “kilometers”; 41: float amount, conversion; 42: Unused code! 35: const float FAHR_PER_CELSIUS = 9.0 / 5.0; 36: 37: int main() 38: { 39: string units; 40: float amount, conversion; 41: . . . 42: cin >> units >> amount; 43: if ( unit == “kilometers” ) 44: { 45: . . .

  20. Wednesday Program 1 is due Program 2 starts (Loops) Lab1 grace date • Thursday Lab 2: submit to HiC and Demo tracing to me Can start 5 pm today S:\Academic • Friday Quiz3-4 on HiC: 1 point and due Monday Quiz3-5 on paper: 10 points in class • Test 1: 60 points Next Friday, Feb. 25 • Exercise Now

More Related