Download presentation
Presentation is loading. Please wait.
Published byEarl Horn Modified over 9 years ago
1
Due Dates Quiz 1, 2 : Friday September 11 th Quiz 3 : Friday September 18 th Quiz 4 : Monday September 28 th Lab 1, 2 and 3 : Friday Sep 11 th Project 1 : Friday September 25 th 1
2
Office Hours This week Tuesday 3:00 -5:00 PM And by appointment (email me) TA’s office hours posted http://www.cs.odu.edu/~cs150/Syllabus150_ fall_2015_Lab.pdf 2
3
What’s an IDE? An integrated development environment (IDE) is a software application that provides comprehensive facilities to computer programmers for software development. Example of IDE: CodeBlocks 3
4
What’s a Compiler? A program that translates instructions written in high-level language into equivalent machine language. Compilers are like translators. 4
5
Problem Solving Process in a Programming Environment 1. Analyze the program and its requirements and design an algorithm to solve it Understand the problem and its requirements If problem is complex, divide it into sub problems 5
6
Problem Solving Process in a Programming Environment 2. Implement the algorithm in a programming language (like C++) Algorithm: A set of steps to accomplish a task 6
7
Problem Solving Process in a Programming Environment Algorithm to go to school: 1. Bike to train station 2. Take a train 3. Walk to office 7
8
Problem Solving Process in a Programming Environment Definition in our book: Algorithm: step-by-step problem-solving process in which a solution is arrived at in a finite amount of time. Algorithm is a set of steps for computer program to accomplish a task 8
9
Example of Algorithm Find the perimeter and area of a rectangle What do we need to know to find the perimeter and area of rectangle? 9
10
What formulas should we use? Perimeter=2*(length+width) Area=length*width 10
11
Algorithm 1. Get the length of the rectangle 2. Get the width of the rectangle 3. Find the perimeter using the equitation: Perimeter=2*(length+width) 4. Find the area using the equitation: Area=length*width 11
12
Problem Solving Process in a Programming Environment 3. Maintain the program Ex: Modifying your program if the domain changed. 12
13
Object Oriented Programming (OOD) Object oriented programming is a methodology that focuses on objects involved in the solution Concepts are represented as "objects Example: Write a program for video rental process for a video rental store 13
14
Objects (videos and customer) 14
15
Relevant data for each object Movie nameFirst name Year released Last name Number of copies Address Producer 15
16
Possible operations on the data for each object Reducing number of copies in stock by one after a copy is rented 16
17
Variable: A memory location whose contents can be changed C++ data types fall into three categories: ◦ Simple data type ◦ Structured data type ◦ Pointers Figure 2-3 Memory spaces after the statement length = 6.0; executes 17
18
Simple data types: Integral: Integers or numbers without a decimal point Floating-point: decimal number Enumeration: User-defined data type 18
19
Examples of data types intex: -67, 0, 78, 763 boolex: true, false charex: ‘A’, ‘a’, ‘*’, ‘6’ (letters, digits, special symbols) floatex: -1.482, 0.18, 2.0 double The maximum number of decimal places in double is more! 19
20
string Type Sequence of zero or more characters enclosed in double quotation marks Length of a string is number of characters in it ◦ Example: length of "William Jacob" is 13 20
21
C++ uses scientific notation to represent real numbers (floating-point notation) Letter E stands for exponent 21
22
How to define an identifier Identifier: Name of things in the program datatype identifier; Example: int counter; double interestRate; char grade; 22
23
How to store value in a variable Using assignment operator = Variable = expression; Example: counter=5; interestRate=0.05; grade=‘A’; 23
24
Comments Comments are for the reader, not the compiler Two types: ◦ Single line: begin with // // This is a C++ program. // Welcome to C++ Programming. ◦ Multiple line: enclosed between /* and */ /* You can include comments that can occupy several lines. */ 24
25
Arithmetic Operators C++ arithmetic operators: ◦ + addition ◦ - subtraction ◦ * multiplication ◦ / division ◦ % modulus (or remainder) operator +, -, *, and / can be used with integral and floating-point data types Use % only with integral data types 25
26
Order of Precedence of Operators All operations inside of () are evaluated first *, /, and % are at the same level of precedence and are evaluated next + and – have the same level of precedence and are evaluated last When operators are on the same level ◦ Performed from left to right (associativity) 3 * 7 - 6 + 2 * 5 / 4 + 6 means (((3 * 7) – 6) + ((2 * 5) / 4 )) + 6 26
27
(((3*7)–6)+((2*5)/4)+6 = * ((21-6)+(10/4))+6=/ ((21-6)+2)+6=- (15+2)+6=+ 17+6=+ 23 27
28
When you use / with integral data types, the integral result is truncated (no rounding) cout << 5/2 ; cout << 5/2.0; Program example 28
29
Increment and Decrement Operators Increment operator: increase variable by 1 ◦ Pre-increment: ++variable ◦ Post-increment: variable++ Decrement operator: decrease variable by 1 ◦ Pre-decrement: --variable ◦ Post-decrement: variable— What is the difference between the following? 29 x = 5; y = ++x; x = 5; y = x++;
30
30
31
Constant data type Named constant: memory location whose content can’t change during execution Syntax to declare a named constant: const datatype identifier = value; In C++, const is a reserved word 31
32
Initializing Variables All variables must be initialized before they are used Variables can be initialized when declared: int first=13, second=10; char ch=' '; double x=12.6; 32
33
I/O Streams and Standard I/O Devices ◦ Input stream: sequence of characters from an input device to the computer ◦ Output stream: sequence of characters from the computer to an output device 33
34
I/O Streams and Standard I/O Devices Use iostream header file to receive data from keyboard and send output to the screen ◦ Contains definitions of two data types: istream : input stream ostream : output stream ◦ Has two variables: cin : stands for common input cout : stands for common output 34
35
cin and cout (extraction operator >> and insertion operator > and insertion operator <<) 35
36
cin and the Extraction Operator >> (cont’d.) When reading data into a char variable ◦ >> skips leading whitespace, finds and stores only the next character ◦ Reading stops after a single character To read data into an int or double variable ◦ >> skips leading whitespace, reads + or - sign (if any), reads the digits (including decimal) ◦ Reading stops on whitespace, non-digit character 36
37
37
38
Using Predefined Functions in a Program Function (subprogram): set of instructions ◦ When activated, it accomplishes a task main executes when a program is run Other functions execute only when called C++ includes a wealth of functions ◦ Predefined functions are organized as a collection of libraries called header files 38
39
Using Predefined Functions in a Program (cont’d.) To use pow (power), include cmath ◦ Two numeric parameters (base and exponent) ◦ Syntax: pow(x,y) = x y x and y are the arguments or parameters ◦ In pow(2,3), the parameters are 2 and 3 39
40
cin and the get Function The get function ◦ Inputs next character (including whitespace) The syntax of cin and the get function: 40
41
Example char ch1, ch2; int num; A25 cin >> ch1 >>ch2 >> num; ‘A’ stored in ch1, ‘2’ in ch2 and 5 in num 41
42
Example char ch1, ch2; int num; A25 cin.get(ch1); cin.get(ch2); cin>>num; ‘A’ stored in ch1, blank in ch2 and 25 in num 42
43
int peek(); Like cin.get() but a huge difference: Returns the next character in the input sequence, without extracting it Ch3_peekandputbackfunction.cpp 43
44
The function getline ◦ Reads until end of the current line Unlike cin Reading does not stop at a whitespace character 44
45
cin and the ignore Function ignore function – Discards a portion of the input The syntax to use the function ignore is: ◦ intExp is an integer expression ◦ chExp is a char expression If intExp is a value m, the statement says to ignore the next m characters or all characters until the character specified by chExp 45
46
cin and the ignore Function (cont’d.) 46
47
Formatting Output Syntax of cout when used with << expression is evaluated value is printed manipulator is used to format the output ◦ Example: endl 47
48
setprecision Manipulator Syntax: Outputs decimal numbers with up to n decimal places Must include the header file iomanip : ◦ #include double PI = 3.14159256; cout << setprecision (2) << PI; 48
49
fixed and showpoint fixed outputs floating-point numbers in a fixed decimal format showpoint forces output to show the decimal point and trailing zeros Examples: ◦ cout << showpoint; ◦ cout << fixed << showpoint; 49
50
setw Outputs the value of an expression in a specified number of columns Must include the header file iomanip cout << setw(5) << x << endl; 50
51
Example 51
52
setfill Manipulator Output stream variables can use setfill to fill unused columns with a character Example: ◦ cout << setfill('#'); 52
53
left and right Manipulators left : left-justifies the output Disable left by using unsetf right : right-justifies the output 53
54
File Input/Output 1.Include fstream header 2.Declare file stream variables 3.Associate the file stream variables with the input/output sources 4.Use the file stream variables with >>, << 5.Close the files 54
55
55
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.