EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

37
EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder

Transcript of EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

Page 1: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

EGR 2261

Engineering Problem Solving Using C and C++

Professor Nick Reeder

Page 2: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

Reminders

Please turn off cell phones.

No food or soft drinks in the classroom.

Stow water bottles at floor level.

Page 3: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

EGR 2261 Unit 1Overview

Read Malik, Chapter 1. Homework #1 and Lab #1 due next

week. Quiz next week.

Page 4: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

Chapter 1:An Overview of Computers and

Programming Languages

Page 5: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

Introduction

• Without software, the computer is useless.• Software is developed with programming languages.• C++ is one of the most popular programming

languages.– TIOBE Index– LangPop.com– IEEE

• C++ is suited for a wide variety of programming tasks.

5C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 6: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

C++ Versus C

• C++ evolved from C, and you can think of C as being a subset of C++.• So in learning C++, you’re also learning C

(and then some).

• Other popular languages that evolved from C include Java, C#, Objective-C.

C++C

Page 7: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

Elements of a Computer System

• Hardware– CPU– Main memory– Secondary storage– Input/Output devices

• Software

7C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 8: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

Central Processing Unit and Main Memory

• Central processing unit– Brain of the computer– Most expensive piece of

hardware– Carries out arithmetic and

logical operations

8C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 9: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

Central Processing Unit and Main Memory (cont’d.)

• Main memory– Directly connected to the CPU

• All programs must be loaded into main memory before they can be executed.

• All data must be brought into main memory before it can be manipulated.

• When computer power is turned off, everything in main memory is lost.

9C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 10: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

Central Processing Unit and Main Memory (cont’d.)

• Main memory is an ordered sequence of memory cells.– Each cell has a unique location in main memory, called the

address of the cell.

• Each cell can contain either a programming instruction or data.

10C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 11: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

Secondary Storage

• Secondary storage: device that stores information permanently

• Examples of secondary storage:– Hard disks – Flash drives– Floppy disks– Zip disks– CD-ROMs– Tapes

11C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 12: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

Input/Output Devices

• Input devices feed data and programs into computers.– Keyboard – Mouse

• Output devices display results.– Monitor– Printer

12C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 13: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

Software

• Software: programs that do specific tasks.• System programs control the computer.

– Operating system monitors the overall activity of the computer and provides services such as:

• Memory management• Input/output activities• Storage management

• Application programs perform a specific task.– Word processors– Spreadsheets– Games

13C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 14: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

The Language of a Computer

• Digital devices (computers, iPods, cell phones, …) use binary code to represent information. This means they store all information (numbers, text, images, music, …) as sequences of 0s and 1s.

• Each 0 or 1 in such a sequence is called a bit (short for binary digit).

• Example of an 8-bit sequence: 01101100• A typical song in an MP3 file might

contain 40 million bits.

Page 15: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

The Language of a Computer (cont’d.)

• Byte:– A sequence of eight bits

• Kilobyte (KB): 210 bytes = 1024 bytes• Table 1-1 on page 6 (next slide) shows some other

related terms.• This is useful general information, but not critical to

what we’ll do in this course.

15C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 16: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

16C++ Programming: From Problem Analysis to Program Design, Seventh Edition

The Language of a Computer (cont’d.)

Page 17: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

The Language of a Computer (cont’d.)

• At least three different encodings have been used to represent text as sequences of 0s and 1s.

• ASCII (American Standard Code for Information Interchange) – 128 characters; see Appendix C in textbook.

• EBCDIC– Formerly used by IBM; obsolete.

• Unicode – 65,536 characters; covers most modern alphabets.

17C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 18: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

The Evolution of Programming Languages

• Early computers were programmed in machine language., which required the programmer to type sequences of 0s and 1s.

• To calculate wages = rate * hours in machine language:

100100 010001 //Load

100110 010010 //Multiply

100010 010011 //Store

18C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 19: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

The Evolution of Programming Languages (cont’d.)

• Assembly language replaces sequences of 0s and 1s with mnemonics (abbreviations such as LOAD or MULT).

• Using assembly language instructions, wages = rate * hours can be written as:

LOAD rateMULT hourSTOR wages

19C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 20: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

The Evolution of Programming Languages (cont’d.)

• High-level languages include Basic, FORTRAN, COBOL, Pascal, C, C++, C#, and Java. Programs written in these language look much more like English.

• Compiler: translates a program written in a high-level language into machine language.

• The equation wages = rate * hours can be written in C++ as: wages = rate * hours;

20C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 21: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

Microsoft Visual Studio

• In this course you’ll use Microsoft Visual Studio Community 2015 to enter, run, and test your programs.

• This is a free version of Visual Studio that you can install at home. Instructions on course website.

• It’s powerful, complex software used by professional programmers.

Page 22: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

A Simple C++ Program//***************************************************// Name: SimpleWageProgram// Author: Nick Reeder // Date: 07/13/2015 // Computes weekly wages.//***************************************************#include <iostream>using namespace std;int main() { int rate = 15; int hours = 40; int wages = rate * hours; cout << wages << endl;

return 0;}

22C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 23: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

Parentheses, Braces, and Brackets

• C++ uses the following symbols to enclose code:• Parentheses ( )• Braces { }• Square brackets [ ]• Angle brackets < >

• Each pair of symbols has a specific use. They are not interchangeable. You can’t use parentheses where you’re supposed to use braces, and so on.

Page 24: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

Processing a C++ Program

24C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 25: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

Processing a C++ Program (cont’d.)

• Steps in processing a C++ program:– Use an editor to create source

code in C++.– Preprocessor directives begin

with # and are processed by the preprocessor.

– Compiler:• Checks that the program obeys

the language rules.• Translates into machine

language (object program).

25C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 26: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

Processing a C++ Program (cont’d.)

• Steps in processing a C++ program (cont’d.):– Linker:

• Combines object program with other files to create executable code.

• Library: contains prewritten code you can use.

– Loader: • Loads executable program into

main memory.

– The last step is to execute the program.

26C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 27: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

Integrated Development Environement

• In the old days you might need several computer programs to perform all of the steps listed above. But Visual Studio combines all of these into a single program. It’s an example of an Integrated Development Environment (IDE).

• Another popular free C++ IDE is Bloodshed’s Dev-C++ at http://www.bloodshed.net/devcpp.html.

Page 28: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

Programming with the Problem Analysis–Coding–Execution Cycle

• Algorithm: – Step-by-step problem-solving

process– Solution achieved in finite

amount of time

• Programming is a process of problem solving.

28C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 29: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

The Problem Analysis–Coding–Execution Cycle (cont’d.)

• Step 1: Analyze the problem.– Outline the problem and its requirements.– Design steps (algorithm) to solve the problem.

• Step 2: Implement the algorithm.– Implement the algorithm in code.– Verify that the algorithm works.

• Step 3: Maintain.– Use and modify the program if the problem domain

changes.

29C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 30: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

Example 1-1

• Design an algorithm to find the perimeter and area of a rectangle.

• The perimeter and area of the rectangle are given by the following formulas:

perimeter = 2 * (length + width)area = length * width

30C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 31: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

Example 1-1 (cont’d.)

• Algorithm:– Get length of the rectangle.

– Get width of the rectangle.

– Find the perimeter using the following equation:

perimeter = 2 * (length + width)

– Find the area using the following equation:

area = length * width

31C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 32: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

Programming Methodologies

• Two popular approaches to programming design:– Structured– Object-oriented

32C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 33: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

Structured Programming

• Structured design: – Dividing a problem into smaller subproblems.

• Structured programming:– Implementing a structured design.

• The structured design approach is also called:– Top-down (or bottom-up) design– Stepwise refinement– Modular programming

33C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 34: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

Object-Oriented Programming

• Object-oriented design (OOD)– Identify components called objects.– Determine how objects interact with each other.

• Specify relevant data and possible operations to be performed on that data.

• Each object consists of data and operations on that data.

34C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 35: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

Object-Oriented Programming (cont’d.)

• An object combines data and operations on the data into a single unit.

• A programming language that implements OOD is called an object-oriented programming (OOP) language.

• Must learn how to represent data in computer memory, how to manipulate data, and how to implement operations .

35C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 36: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

Object-Oriented Programming (cont’d.)

• Write algorithms and implement them in a programming language.

• Use functions to implement algorithms.• Learn how to combine data and operations on the

data into a single unit called an object.• C++ was designed to implement OOD.

36C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 37: EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.

ANSI/ISO Standard C++

• C++ evolved from C .• C++ designed by Bjarne Stroustrup at Bell

Laboratories in early 1980s.– Many different C++ compilers were available.– C++ programs were not always portable from one compiler

to another.

• In 1998, ANSI/ISO C++ language standards were approved.

• Second standard called C++11 approved in 2011.

37C++ Programming: From Problem Analysis to Program Design, Seventh Edition