Lecture 2(Basics)
Lecture 2(Basics)
1. Problem specification
- understand problem
- identify input/output
2. Solution design
- work problem out by hand for specific case.
- design general algorithm
Problem:
Write a program that converts from miles (m) to kilometers (km).
1. Problem Specification
need to know that 1 mile is equivalent to 1.6093 Kilometers.
x miles is equivalent to (x 1.6093) km
input miles --> output kilometers
Example of steps in problem solving
…
2. Solution design
Display message:
“Please enter distance in miles:__”
Read the miles (m)
Compute the number of kilometers (km)
Display message:
“Equivalent distance in kilometers is:__”
Example of steps in problem solving …
3. Implementation
// Miles2km.cpp
// Convert miles to kilometers.
#include <iostream>
using namespace std;
int main()
{
double miles, km;
cout << “Please enter distance in miles:”;
cin >> miles;
km = miles * 1.6093;
cout<<“Equivalent distance in km is :” << km << endl;
return 0;
}
Example of steps in problem solving …
3. Implementation
// Miles2km.cpp
// Convert miles to kilometers.
#include <iostream>
using namespace std;
int main()
{
double miles, km;
cout << “Please enter distance in miles:”;
cin >> miles; <- - - - - - - - - - - - - - - -
km = miles * 1.6093;
cout << “Equivalent distance in km is :” << km << endl;
return 0;
}
Example of steps in problem solving …
3. Implementation
// Miles2km.cpp
// Convert miles to kilometers.
#include <iostream>
using namespace std;
int main()
{
double miles, km;
cout << “Please enter distance in miles:”;
cin >> miles;
km = miles * 1.6093;
cout<<“Equivalent distance in km is :” << km << endl;
return 0;
}
Namespaces
Namespaces allow to group entities like classes,
objects and functions under a name. This way the
global scope can be divided in "sub-scopes", each
one with its own name.
#include <iostream>
It instructs the preprocessor to read a file from disk and insert its contents
into the source code file. In this case it reads the general I/O streams
header from the compiler's standard header directory.
#define PI (3.14159)
cout << (2.0 * PI) << endl;
Standard Input and Output Objects
• C++ does not directly define any statements to do input
or output (IO). Instead, IO is provided by the standard
library.
• iostream istream, ostream
• ? Stream:
• A stream is a sequence of characters intended to be
read from or written to an IO device of some kind. The
term "stream" is intended to suggest that the
characters are generated, or consumed, sequentially
over time.
IO objects
• cin (standard input)
• cout (standard output)
Running a C++ Program
• C++ source code is written with a text
editor
• The compiler on your system converts
source code to object code.
• The linker combines all the object code into
an executable program.
Three C++ Program Stages
other
other code
code
from
from libraries,
libraries,
etc.
etc.
Errors
There are three general categories of errors:
1) Syntax Errors
- violations of the C++ grammar rules
- caught by the compiler
2) Run-Time Errors
- also called an execution error
- caught during the running of the
program (peripheral devices may not be turned on)
3) Logical Errors
- error in design of program
- do not produce an error message
- produce incorrect output
using namespace std;
Syntax and Semantics
Syntax: The formal rules governing how valid
instructions are written in a programming
language.
Declaration statement
Example int k;
int classSize;
can also be written as
int k, classSize;
1. Assignment statement
Assigns a value to a variable.
Ex: k = 65;
classSize = 50;
Syntax : variable = expression;
2. Initialized declaration
Combines declaration and assignment
int k = 65;
int classSize = 50;
Integer Object ….
Sometimes want an integer object that will not change values during
execution of program.
const int WEEK_DAYS = 7;
const int WKS_IN_YR= 52;
Integer Operations:
int a = 1, b = 2, c = 3;
- addition: a + 4 value is 1+ 4 = 5
- subtraction: c - 1 value is 3 - 1 = 2
- multiplication: b * c value is 2 * 3 = 6
- negation: -a value is -1
Integer Object ….
int a = 17, b = 3;
- integer division: a/b value is 17/3 = 5
- remainder: a%b value is 17 % 3 = 2
Example of uses:
1) Convert 100 inches into feet and inches
100/12 = 8
100 % 12 = 4
So 100 inches is equivalent to 8 feet, 4 inches
2) How do you know if a number is odd or even?
Assignment operator ( = )
Syntax object = expression;
Ex: a = a + 1; (counter)
a = b = 100; (multiple
assignment)
Have seen:
quarters, dimes, nickels, pennies, total, size,…
If variable name consists of more than one word, start each new word
with an upper case letter
Ex: bookPrice, studentId
Better style do do
int numCoffees; // Number of coffees
Output Statements
1) x = 8; y = 6;
cout << x << y;
cout << “ “ << y;
3) yards = 8;
feet = 3 * yards;
cout << yards << “ yd. is”;
cout << endl;
cout << feet << “ ft.”;
Output Statements …
What will be output of each of these program fragments?
4) cout << “Yes”;
cout << endl << “No.”;