Académique Documents
Professionnel Documents
Culture Documents
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
The parentheses are unbalanced. This is very common with complicated expressions.
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.
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.
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;
1/28/2013
Itreallyiseasiertoreadwithspaces!
So always use spaces around all operators: + - * / % =
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)
1/28/2013
Review Questions
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
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
1/28/2013
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; }
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; }
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; }
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; }
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.
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.
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.
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; }
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;
#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
#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; }
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
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.
15
1/28/2013
16