200 likes | 334 Views
Review of C++ Lesson 1. Objectives. Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops. Problem.
E N D
Objectives Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops
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.
Flowchart Start Prompt Read in year F Leap Year? T Is not leap year Is leap year End
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; }
Comment Statements • //Helen Kow CS 265 Program #1 • //This program tells you if a year is a leap year or not
Preprocessor Directives • #include"stdafx.h" • #include<iostream> • using std::cin; • using std::cout; • using std::endl;
Function main( ) • int main() • { • //code goes here • return 0; • }
Variable Declarations • int year, rem4, rem100, rem400; year rem4 rem100 rem400
Prompt and Input • cout <<"Enter a year "; • cin>> year; year 2018 rem4 rem100 rem400
Calculations • rem4 = year%4; • rem100 = year %100; • rem400 = year % 400; year 2018 rem4 2 18 rem100 18 rem400
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
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.
Loop _ _ _ _ _ _ _ _ _ _ _ _ |||||||||_ _ _ _ _ _ _ _ _ Code to be repeated While condition is true
Loop Syntax do { //code to be repeated } while (some condition is true );
Start Flowchartwith loop _ _ _ _ _ _ _ _ _ _ _ _ _ | | ||| | | | | | | | | | | | | Prompt Read in year Leap Year? Is not leap year Is leap year Play again? _ _ _ _ _ _ while again == true End
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
Summary Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops