Vous êtes sur la page 1sur 16

1/28/2013

ENGR 1200U Introduction to Programming Lecture 7 Simple C++ Programs (Chapter 2) (contd) Dr. Eyhab Al-Masri

1992-2012 by Pearson Education, Inc. & John Wiley & Sons Some portions are adopted from C++ for Everyone by Horstmann

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.

ENGR 1200U Winter 2013 - UOIT

1/28/2013

Now consider this expression -(b * b - (4 * a * c))) / 2 * a) It is still is not correct. There are too many closing parentheses.

ENGR 1200U Winter 2013 - UOIT

Every program that carries out input or output needs the <iostream> header. If you use mathematical functions such as sqrt, you need to include <cmath>. 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 function or symbol, check your header files.

ENGR 1200U Winter 2013 - UOIT

1/28/2013

This program produces the wrong output: #include <iostream> using namespace std; int main() { double price = 4.35; int cents = 100 * price; // Should be 100 * 4.35 = 435 cout << cents << endl; // Prints 434! return 0; } Why?
ENGR 1200U Winter 2013 - UOIT

In the processor hardware, numbers are represented in the binary number system, not in decimal. In the binary system, there is no exact representation for 4.35, just as there is no exact representation for in the decimal system. The representation used by the computer is just a little less than 4.35, so 100 times that value is just a little less than 435. The remedy is to add 0.5 in order to round to the nearest integer: int cents = 100 * price + 0.5;

ENGR 1200U Winter 2013 - UOIT

1/28/2013

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 1200U Winter 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 not to put a space after a function name. Write sqrt(x) not sqrt (x)

ENGR 1200U Winter 2013 - UOIT

1/28/2013

Review Questions

ENGR 1200U Winter 2013 - UOIT

Which parts of a computer can store program code? Which parts can store user data?

Both program code and data are typically stored long term in a computers secondary storage, such as a hard disk. Program code and data can also be stored in a computers primary storage.
Secondary storage is relatively inexpensive and retains information even if the computers power is turned off.

Primary storage consists of read-only memory (ROM), and random access memory (RAM). RAM is relatively expensive when compared to secondary storage, and is erased whenever the computer is turned off

ENGR 1200U Winter 2013 - UOIT

1/28/2013

Which parts of a computer serve to give information to the user? Which parts take user input?
User of a computer receives information via: display screen, speakers, printers
Computers output devices

User can input data using the computers keyboard, a pointing device (i.e. mouse), a microphone, or a webcam
computers input devices

ENGR 1200U Winter 2013 - UOIT

What does this program print?


#include<iostream> usingnamespacestd; intmain() { cout<<"6*7="<<6*7<<endl; return0; }
The program prints the following: 6 * 7 = 42

ENGR 1200U Winter 2013 - UOIT

1/28/2013

What does this program print?


#include<iostream> usingnamespacestd; intmain() { cout<<"Hello"<<"World"<<endl; return0; }
The program prints the following: HelloWorld

ENGR 1200U Winter 2013 - UOIT

What does this program print?


#include<iostream> usingnamespacestd; intmain() { cout<<"Hello"<<endl<<"World"<<endl; return0; }
The program prints the following: Hello World

ENGR 1200U Winter 2013 - UOIT

1/28/2013

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<iostream> usingnamespacestd; intmain() { cout<<"HelloWorld!<<endl; return0; }

Program 1 (compile-time error) no end quote on string

ENGR 1200U Winter 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<iostream> usingnamespacestd; intmain() { cout<<"HelloWorld!"<<endl return0; }

Program 2 (compile-time error)

no semicolon on the cout statement


ENGR 1200U Winter 2013 - UOIT

1/28/2013

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<iostream> usingnamespacestd; intmain { cout<<"HelloWorld!"<<endl; return0; }

Program 3 (compile-time error)

No parentheses for main function


ENGR 1200U Winter 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<iostream> usingnamespacestd; intmain() { cout<<"HelloWolrd!<<endl; return0; }

Program 4 (run-time error) World is misspelled

ENGR 1200U Winter 2013 - UOIT

1/28/2013

How do you discover compile-time errors? How do you discover run-time errors?
A compile-time error is typically found by the compiler during the compilation process. A compile-time error is caused when the source code violates the rules of the programming language being used.

A run-time error cannot be found by the compiler. It is found by testing the program and carefully examining the output or results for errors.

ENGR 1200U Winter 2013 - UOIT

1)#include<iostream> 2) An extra semicolon at the end of Line 3. 3) int main(); Missing semicolon at the end of Line 5. 4) { 5) cout <<"Pleaseentertwonumbers:" 6) cout <<x,y; 7) cout <<"Thesumof<<x<<"and"<<y 8) <<"is:"x+y<<endl; 9) return; There is a missing double quote on Line 7 (after 10) } The sum of).
There is a missing << operator on Line 8 (before x + y). The return statement on Line 9 should return a value.

No using namespace std line

ENGR 1200U Winter 2013 - UOIT

10

1/28/2013

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.5 percent per month). Every month, $500 is withdrawn to meet college expenses. After how many years is the account 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 1200U Winter 2013 - UOIT

Write a program that prints the sum of the rst ten positive integers, 1 + 2 + + 10 without using variables.
#include<iostream> usingnamespacestd; intmain() { cout<< "Thesumofthefirsttenpositiveintegersis" <<1+2+3+4+5+6+7+8+9+10<<endl; return0; }

ENGR 1200U Winter 2013 - UOIT

11

1/28/2013

Write a program that prints the balance of an account that earns 5 percent interest per year after the rst, second, and third year. Do not use variables.
#include<iostream> usingnamespacestd; intmain() { cout << "Thestartingbalanceis$10,000."<<endl; cout<<"Theinterestrateis5%."<<endl; cout<<"Thebalanceafteroneyearis:" <<10000*1.05<<endl; cout<<"Thebalanceaftertwoyearsis:" <<(10000*1.05)*1.05<<endl; cout<<"Thebalanceafterthreeyearsis:" <<(((10000*1.05)*1.05)*1.05)<<endl; return0;

ENGR 1200U Winter 2013 - UOIT

#include<iostream> usingnamespacestd; intmain() { doublebalance(10000); cout<<"Thestartingbalanceis$10,000."<<endl; cout<<"Theinterestrateis5%."<<endl; balance=balance*1.05; cout<<"Thebalanceafteroneyearis:" <<balance<<endl; balance=balance*1.05; cout<<"Thebalanceaftertwoyearsis:" <<balance<<endl; balance=balance*1.05; cout<<"Thebalanceafterthreeyearsis:" <<balance<<endl; return0; }
ENGR 1200U Winter 2013 - UOIT

12

1/28/2013

Write a program that displays your name inside a box on the terminal screen, like this:
Eyhab

Do your best to approximate lines with characters such as | - +.

#include<iostream> usingnamespacestd; intmain() { cout<<endl; cout<<"++"<<endl; cout<<"|Eyhab|"<<endl; cout<<"++"<<endl; cout<<endl; system("pause"); return0; } ENGR 1200U
Winter 2013 - UOIT

Write a program that prints a face similar to (but different from) the following:
#include<iostream> usingnamespacestd; intmain() { cout<<endl; cout<<"++++ "<<endl; cout<<"/\\ /*\\ /Hey\\"<<endl; cout<<"\\ /\\ //<there,|"<<endl; cout<<"++/\\ Human!/"<<endl; "<<endl; cout<<"|||| cout<<"++++"<<endl; cout<<""<<endl; cout<<endl; return0; }

ENGR 1200U Winter 2013 - UOIT

13

1/28/2013

Write a program that displays the following image, using characters such as / \ - | + for the lines. Write as Ohm.
#include<iostream> usingnamespacestd;

intmain() { cout<<"___/\\ /\\ /\\ _______/\\ /\\ /\\ __"<<endl; cout<<"|\\/\\/\\/|\\/\\/\\/|"<<endl; cout<<"|5kOhm|6kOhm|"<<endl; cout<<"|\\ \\"<<endl; cout<<" //"<<endl; cout<<"12V|+|10kOhm\\ 4kOhm\\"<<endl; cout<<"| |//"<<endl; cout<<" \\ \\"<<endl; cout<<"|//"<<endl; cout<<"|||"<<endl; cout<<"|||"<<endl; cout<<""<<endl; return0; } ENGR 1200U
Winter 2013 - UOIT

What is the value of mystery after this sequence of statements?

intmystery=1; mystery=1 2*mystery; mystery=mystery+1;


mystery is 0

ENGR 1200U Winter 2013 - UOIT

14

1/28/2013

What are the values of the following expressions? In each line, assume that
double x = 2. 5; double y = -1. 5; int m = 18; int n = 4;

a. b. c. d. e.

x + n * y - (x + n) * y m/n+m%n 5*x-n/5 1 - (1 - (1 - (1 - (1 - n) ) ) ) sqrt(sqrt(n) )

a. 6.25 b. 6 c. 12.5 d. 3 e. Warningorerror!


ENGR 1200U Winter 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);

ENGR 1200U Winter 2013 - UOIT

15

1/28/2013

Find at least four run-time errors in the following program.


#include<iostream> usingnamespacestd; intmain() total variable should be initialized to zero { inttotal; integer type, what happens if we use double? intx1; cout<<"Pleaseenteranumber:"; cin>>x1; variable x1 was mistakenly used instead of x2 total=total+x1; cout<<"Pleaseenteranothernumber:"; intx2; total is defined as integer; division will be treated cin>>x2; as integer division (any digits after decimal point total=total+x1; in the result will be truncated) doubleaverage=total/2; cout<<"Theaverageofthetwonumbersis" <<average<<"endl"; return0; there should not be double quotes around endl

ENGR 1200U Winter 2013 - UOIT

16