1 / 20

Review of C++ Lesson 1

Review of C++ Lesson 1. Objectives. Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops. Problem.

holleb
Download Presentation

Review of C++ Lesson 1

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. Review of C++Lesson 1

  2. Objectives Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops

  3. Problem • Read in a year and determine if it is a leap year. A leap year is a year that is evenly divisible by 4 and not by 100. Or, a year that is evenly divisible by 400 is also a leap year.

  4. Flowchart Start Prompt Read in year F Leap Year? T Is not leap year Is leap year End

  5. Program Code //Helen Kow CS 265 Program #1 //This program tells you if a year is a leap year or not #include"stdafx.h" #include<iostream> using std::cin; using std::cout; using std::endl; int main() { int year, rem4, rem100, rem400; cout <<"Enter a year "; cin>> year; rem4 = year%4; rem100 = year %100; rem400 = year % 400; if ( rem4 == 0 && rem100 != 0 || rem400 == 0) cout << endl << year << " is a leap year " << endl; else cout << endl << year << " is not a leap year \n“; return 0; }

  6. Comment Statements • //Helen Kow CS 265 Program #1 • //This program tells you if a year is a leap year or not

  7. Preprocessor Directives • #include"stdafx.h" • #include<iostream> • using std::cin; • using std::cout; • using std::endl;

  8. Function main( ) • int main() • { • //code goes here • return 0; • }

  9. Variable Declarations • int year, rem4, rem100, rem400; year rem4 rem100 rem400

  10. Prompt and Input • cout <<"Enter a year "; • cin>> year; year 2018 rem4 rem100 rem400

  11. Calculations • rem4 = year%4; • rem100 = year %100; • rem400 = year % 400; year 2018 rem4 2 18 rem100 18 rem400

  12. Test and Output • if ( rem4 == 0 && rem100 != 0 || rem400 == 0) • cout << endl << year << " is a leap year " << endl; • else • cout << endl << year << " is not a leap year \n“; year 2018 rem4 2 18 rem100 18 rem400

  13. Sample Output

  14. Modification of leap year problem • Read in a year and determine if it is a leap year. A leap year is a year that is evenly divisible by 4 and not by 100. Or, a year that is evenly divisible by 400 is also a leap year. • Allow the user to repeat the program as many times as they desire.

  15. Loop _ _ _ _ _ _ _ _ _ _ _ _ |||||||||_ _ _ _ _ _ _ _ _ Code to be repeated While condition is true

  16. Loop Syntax do { //code to be repeated } while (some condition is true );

  17. Start Flowchartwith loop _ _ _ _ _ _ _ _ _ _ _ _ _ | | ||| | | | | | | | | | | | | Prompt Read in year Leap Year? Is not leap year Is leap year Play again? _ _ _ _ _ _ while again == true End

  18. Code with Loop //Helen Kow CS 265 Program #1 //This program tells you if a year is a leap year or not #include "stdafx.h" #include <iostream> using std::cin; using std::cout; using std::endl; int main() { int year, rem4, rem100, rem400; char again; do { cout <<"Enter a year "; cin>> year; rem4 = year%4; rem100 = year %100; rem400 = year % 400; if ( rem4 == 0 && rem100 != 0 || rem400 == 0) cout << endl << year << " is a leap year " << endl; else cout << endl << year << " is not a leap year " << endl; cout << "\nPlay again ?? "; cin >> again; } while (again == 'y'); return 0; } 1 2 3

  19. Sample Execution

  20. Summary Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops

More Related