of 16
8/14/2019 Lecture 7 - Simple C++ Programs pt 3.pdf
1/16
1/28/20
1992-2012 by Pearson Education, Inc. & John Wiley & SonsSome portions are adopted from C++ f or Everyone by Horstmann
ENGR 1200U Introduction to Programming
Lecture 7
Simple C++ Programs (Chapter 2) (contd)
Dr. Eyhab Al-Masri
ENGR 1200UWinter 2013 - UOIT
Consider the expression
(-(b * b - 4 * a * c) / (2 * a)
What is wrong with it?
The parentheses are unbalanced.This is very common with complicated expressions.
?
8/14/2019 Lecture 7 - Simple C++ Programs pt 3.pdf
2/16
1/28/20
ENGR 1200UWinter 2013 - UOIT
Now consider this expression
-(b * b - (4 * a * c))) / 2 * a)
It is still is not correct.
There are too many closing parentheses.
ENGR 1200UWinter 2013 - UOIT
Every program that carries out input or output needsthe header.
If you use mathematical functions such as sqrt,you need to include .
If you forget to include the appropriate header file,the compiler will not know symbols such as
cout or sqrt.
If the compiler complains about an undefined functionor symbol, check your header files.
8/14/2019 Lecture 7 - Simple C++ Programs pt 3.pdf
3/16
1/28/20
ENGR 1200UWinter 2013 - UOIT
This program produces the wrong output:
#include
using namespace std;
int main()
{
double price = 4.35;
int cents = 100 * price;
// Should be 100 * 4.35 = 435
cout
8/14/2019 Lecture 7 - Simple C++ Programs pt 3.pdf
4/16
1/28/20
ENGR 1200UWinter 2013 - UOIT
It is easier to read
x1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
than
x1=(-b+sqrt(b*b-4*a*c))/(2*a);
Itreallyiseasiertoreadwithspaces!
So always use spaces around all operators: + - * / % =
ENGR 1200UWinter 2013 - UOIT
However, dont put a space after a unary minus:
thats a used to negate a single quantity like this: -b
That way, it can be easily distinguished from a binary minus,
as in a - b
It is customary notto put a space after a function name.
Write sqrt(x)
not sqrt (x)
8/14/2019 Lecture 7 - Simple C++ Programs pt 3.pdf
5/16
1/28/20
ENGR 1200UWinter 2013 - UOIT
Review Questions
ENGR 1200UWinter 2013 - UOIT
Which parts of a computer can store programcode? Which parts can store user data? Both program code and data are typically stored long
term in a computers secondary storage, such as a harddisk. Secondary storage is relatively inexpensive and retains
information even if the computers power is turned off.
Program code and data can also be stored in acomputers primary storage.
Primary storage consists of read-only memory (ROM), andrandom access memory (RAM). RAM is relatively expensivewhen compared to secondary storage, and is erasedwhenever the computer is turned off
8/14/2019 Lecture 7 - Simple C++ Programs pt 3.pdf
6/16
1/28/20
ENGR 1200UWinter 2013 - UOIT
Which parts of a computer serve to giveinformation to the user? Which parts take userinput? User of a computer receives information via: display
screen, speakers, printers
Computers output devices
User can input data using the computers keyboard, apointing device (i.e. mouse), a microphone, or awebcam
computers input devices
ENGR 1200UWinter 2013 - UOIT
What does this program print?
#include
usingnamespacestd;
intmain()
{
cout
8/14/2019 Lecture 7 - Simple C++ Programs pt 3.pdf
7/16
1/28/20
ENGR 1200UWinter 2013 - UOIT
What does this program print?
#include
usingnamespacestd;
intmain()
{
cout
8/14/2019 Lecture 7 - Simple C++ Programs pt 3.pdf
8/16
1/28/20
ENGR 1200UWinter 2013 - UOIT
Write three versions of the hello.cpp (Question 5)program that have different compile-time errors.Write a version that has a run-time error.
#include
usingnamespacestd;
intmain()
{
cout
8/14/2019 Lecture 7 - Simple C++ Programs pt 3.pdf
9/16
1/28/20
ENGR 1200UWinter 2013 - UOIT
Write three versions of the hello.cpp (Question 5)program that have different compile-time errors.Write a version that has a run-time error.
#include
usingnamespacestd;
intmain
{
cout
8/14/2019 Lecture 7 - Simple C++ Programs pt 3.pdf
10/16
1/28/20
ENGR 1200UWinter 2013 - UOIT
How do you discover compile-time errors? Howdo you discover run-time errors?
A compile-time error is typically found by the compiler during thecompilation process. A compile-time error is caused when the sourcecode violates the rules of the programming language being used.
A run-time errorcannot be found by the compiler. It is found by testingthe program and carefully examining the output or results for errors.
ENGR 1200UWinter 2013 - UOIT
1) #include
2)
3) int main();
4) {
5) cout
8/14/2019 Lecture 7 - Simple C++ Programs pt 3.pdf
11/16
1/28/20
ENGR 1200UWinter 2013 - UOIT
Write an algorithm to settle the following question A bank account starts out with $10,000. Interest is
compounded monthly at 6 percent per year (0.5percent per month). Every month, $500 is withdrawnto meet college expenses. After how many years is theaccount depleted?
1. Repeat the following while account is greater than $0:
a) Set account_value equal to account_value times 1.005.b) Deduct 500 from account_value.c) Increment number of months by 1.
2. Print the total number of months divided by 12 to determine how
The account is depleted after 22 months, or 1.83333 years.
ENGR 1200UWinter 2013 - UOIT
Write a program that prints the sum of the first ten positiveintegers, 1 + 2 + + 10 without using variables.
#include
usingnamespacestd;
intmain(){
cout
8/14/2019 Lecture 7 - Simple C++ Programs pt 3.pdf
12/16
1/28/20
ENGR 1200UWinter 2013 - UOIT
Write a program that prints the balance of an account that earns 5
percent interest per year after the first, second, and third year. Donot use variables.
#includeusingnamespacestd;
intmain(){
cout
8/14/2019 Lecture 7 - Simple C++ Programs pt 3.pdf
13/16
1/28/20
ENGR 1200UWinter 2013 - UOIT
Write a program that displays your name inside a box on theterminal screen, like this:
Do your best to approximate lines with characters such as | - +.
#includeusingnamespacestd;
intmain(){
cout
8/14/2019 Lecture 7 - Simple C++ Programs pt 3.pdf
14/16
1/28/20
ENGR 1200UWinter 2013 - UOIT
Write a program that displays the following image, usingcharacters such as / \ - | + for the lines. Write as Ohm.
#includeusingnamespacestd;
intmain(){
cout
8/14/2019 Lecture 7 - Simple C++ Programs pt 3.pdf
15/16
1/28/20
ENGR 1200UWinter 2013 - UOIT
What are the values of the following expressions? In eachline, assume that
double x = 2. 5;
double y = -1. 5;
int m = 18;
int n = 4;
a. x + n * y - (x + n) * y
b. m / n + m % n
c. 5 * x - n / 5
d. 1 - (1 - (1 - (1 - (1 - n) ) ) )
e. sqrt(sqrt(n) )
a. 6.25
b. 6c. 12.5d.3e. Warningorerror!
ENGR 1200UWinter 2013 - UOIT
Write the following mathematical expressions in C++.
s=s0+v0*t+(1/2)*g*t*t;
G
=
4
*
PI
*
PI
*
(pow(a,
3)
/
(p
*
p
*
(m1
+
m2)));
FV=PV*pow((1+(INT/100)),YRS);
8/14/2019 Lecture 7 - Simple C++ Programs pt 3.pdf
16/16
1/28/20
ENGR 1200UWinter 2013 - UOIT
#includeusingnamespacestd;intmain(){
inttotal;intx1;coutx1;total =total+x1;coutx2;total =total+x1;doubleaverage=total/2;cout