55
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 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

Embed Size (px)

Citation preview

PowerPoint Presentation

Due DatesQuiz 1, 2 : Friday September 11thQuiz 3 : Friday September 18thQuiz 4 : Monday September 28th

Lab 1, 2 and 3 : Friday Sep 11thProject 1 : Friday September 25th

1Office Hours This week Tuesday 3:00 -5:00 PMAnd by appointment (email me)

TAs office hours postedhttp://www.cs.odu.edu/~cs150/Syllabus150_fall_2015_Lab.pdf

2Whats an IDE?An integrated development environment (IDE) is a software application that provides comprehensive facilities to computer programmers for software development.Example of IDE: CodeBlocks3Whats a Compiler?A program that translates instructions written in high-level language into equivalent machine language.

Compilers are like translators.4We saw in the previous session that our code in codeblocks could not run properly without compiling. Why? Because we need to translate the higher level language which is c++ into machine language before running it4Problem Solving Process in a Programming Environment Analyze the program and its requirements and design an algorithm to solve itUnderstand the problem and its requirementsIf problem is complex, divide it into sub problems

5Requirements can include whether the program requires manipulating data, whether it produces output and what the output looks like.5Problem Solving Process in a Programming Environment Implement the algorithm in a programming language (like C++)

Algorithm: A set of steps to accomplish a task

6Problem Solving Process in a Programming Environment Algorithm to go to school:Bike to train stationTake a trainWalk to office

7Problem 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

8In Computer Science Algorithm is a set of steps for computer program to accomplish a task We will see this concept in finite and infinite loops, sometimes you make a mistake and your loop goes on for ever and ever and never stops

8Example of AlgorithmFind the perimeter and area of a rectangle

What do we need to know to find the perimeter and area of rectangle?

9What formulas should we use?

Perimeter=2*(length+width)

Area=length*width

10AlgorithmGet the length of the rectangleGet the width of the rectangleFind the perimeter using the equitation: Perimeter=2*(length+width)Find the area using the equitation: Area=length*width

11What do we use for step 1 in c++? cin >> length; step2: cin >> width 11Problem Solving Process in a Programming Environment Maintain the program

Ex: Modifying your program if the domain changed.12For example if we decide to include negative numbers your program should handle that and so you should modify your program.12Object 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

13Objects (videos and customer)

14Relevant data for each object

Movie nameFirst nameYear released Last nameNumber of copies AddressProducer

15After identifying the objects the next step is to specify for each object the relevant data and possible operations to be performed on that data15Possible operations on the data for each object

Reducing number of copies in stock by one after a copy is rented

16Or increment by one after a customer returns a video16Variable:A memory location whose contents can be changed

C++ data types fall into three categories:Simple data typeStructured data typePointers

Figure 2-3 Memory spaces after the statement length = 6.0; executes17Simple data types:Integral: Integers or numbers without a decimal pointFloating-point: decimal numberEnumeration: User-defined data type

18You wont deal with enumeration in this course18Examples of data typesintex: -67, 0, 78, 763boolex: true, falsecharex: 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!19Char represents single character (letter, digit or special symbol)Double is more precise than float. According to your book the number of the decimal places in float is 6 or 7 and in double it is 1519string TypeSequence of zero or more characters enclosed in double quotation marks Length of a string is number of characters in itExample: length of "William Jacob" is 13

20Why? Because they count space as a character too!20C++ uses scientific notation to represent real numbers (floating-point notation)Letter E stands for exponent

21If on your program you see 7.5E1 21How to define an identifier Identifier: Name of things in the programdatatype identifier;Example:int counter;double interestRate;char grade;

22Identifiers are name of things that appear in the program such as variables, constants, functions22How to store value in a variableUsing assignment operator =

Variable = expression;

Example:counter=5;interestRate=0.05;grade=A;

23CommentsComments are for the reader, not the compilerTwo 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.*/24Arithmetic OperatorsC++ arithmetic operators:+ addition- subtraction* multiplication/ division% modulus (or remainder) operator+, -, *, and / can be used with integral and floating-point data typesUse % only with integral data types

25Order of Precedence of OperatorsAll 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 lastWhen operators are on the same levelPerformed from left to right (associativity)3 * 7 - 6 + 2 * 5 / 4 + 6 means(((3 * 7) 6) + ((2 * 5) / 4 )) + 6

26(((3*7)6)+((2*5)/4)+6 =*((21-6)+(10/4))+6=/((21-6)+2)+6=-(15+2)+6=+17+6=+2327When you use / with integral data types, the integral result is truncated (no rounding)

cout and insertion operator and insertion operator (contd.)When reading data into a char variable>> skips leading whitespace, finds and stores only the next characterReading stops after a single characterTo 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

37Using Predefined Functions in a ProgramFunction (subprogram): set of instructions When activated, it accomplishes a task main executes when a program is runOther functions execute only when called C++ includes a wealth of functionsPredefined functions are organized as a collection of libraries called header files

38Using Predefined Functions in a Program (contd.)To use pow (power), include cmathTwo numeric parameters (base and exponent)Syntax: pow(x,y) = xyx and y are the arguments or parametersIn pow(2,3), the parameters are 2 and 3

39cin and the get FunctionThe get functionInputs next character (including whitespace)

The syntax of cin and the get function:

40Examplechar ch1, ch2;int num;

A25

cin >> ch1 >>ch2 >> num;

A stored in ch1, 2 in ch2 and 5 in num41Examplechar ch1, ch2;int num;

A25

cin.get(ch1);cin.get(ch2);cin>>num;

A stored in ch1, blank in ch2 and 25 in num42int peek();Like cin.get() but a huge difference:Returns the next character in the input sequence, without extracting it

Ch3_peekandputbackfunction.cpp43The function getline

The function getlineReads until end of the current line

Unlike cin Reading does not stop at a whitespace character

44cin and the ignore Functionignore function Discards a portion of the inputThe syntax to use the function ignore is:

intExp is an integer expressionchExp is a char expressionIf intExp is a value m, the statement says to ignore the next m characters or all characters until the character specified by chExp

45cin and the ignore Function (contd.)

46Formatting OutputSyntax of cout when used with