introduction to problem solving and programming problem solving and programming •a computer is a...

35
Introduction to Problem Solving and Programming Lecture 1

Upload: others

Post on 31-Mar-2020

14 views

Category:

Documents


0 download

TRANSCRIPT

Introduction to Problem Solving and Programming

Lecture 1

Outline

• Introduction to computers, basic programming concepts and construct.

• Problem solving and program development process.

• C++ elements, syntax, and semantics

• Input and output statements.

• Control structures

• User-defined functions: design and use of parameters

• Arrays, storage for large amount of data.

• 20 Midterm Exam

• 10 Quizzes

• 5 pts Lab Exam

• 5 pts Attendance

• 60 pts Final Exam

Grading (subject to changes)

Problem Solving and Programming

• A computer is a machine designed to perform operations specified with a set of instructions called a program.

• Hardware refers to the computer equipment.

– keyboard, mouse, terminal, hard disk, printer

• Software refers to the programs that describe the steps we want the computer to perform.

Operating Systems—Bridging Software and Hardware

• An operating system is software that has the job of managing and interacting with the hardware resources of a computer.

0-8

Program Translation

• A central processing unit (CPU) is designed to interpret and execute a specific set of instructions represented in binary form (i.e., 1s and 0s) called machine code .

• Only programs in machine code can be executed by a CPU, depicted in Figure

• Writing programs at this “low level” is tedious and error-prone. Therefore, most programs are written in a “high-level” programming language such as C++, Matlab, Python, C#.

• Since the instructions of such programs are not in machine code that a CPU can execute, a translator program must be used.

• There are two fundamental types of translators. One, called a compiler , translates programs directly into machine code to be executed by the CPU.

0-10

0-11

An Engineering Problem-Solving Methodology

1. Define the problem.

2. Plan the solution.

3. Coding the program

4. Testing.

Steps of writing a program:

1. Defining the Problem

• Specify the input data.

• Specify the output (the problem solution).

• Specify the processing and logic to generate output from input.

2. Plan the solution

• The most important step.

• Write an algorithm ( step by step sequence of instructions).

• Or draw a flow chart (a graphicalrepresentation of the algorithm).

3. Coding the program

• A program is written for the solution.

• A high level language is used.

• C++ will be used during the course.

4. Testing

• Tracing each step.

• Use sample data.

• Compare results with expected.

• A compiler is used to translate source codeinto machine language.

• Run program with test data.

Flow charts

• A way to diagram the logic of the solution.

• Use specific symbols with written message.

• The basic symbols are:

1. Terminal nodes for the beginning and end of the flow chart.

Start Stop

2. I/p and O/p Blocks.

• Input block to show where data is input from the input device into memory.

• Output block to show the output to be displayed by the output device.

Flow charts

ReadList of input

variables

WriteList of output

data items

Reada, b, c

ReadX(i),

i=1,2,…,20

Writex, y, z

Write“Problem Solving”

3 different values will be stored in memory locations a, b, c, respectively.

20 different values will be entered in an array X.

The values stored in memory locations x, y, z, will be displayed or printed.

The characters (string) between “ ” will be displayed or printed as they are.

3. Processing Blocks.

• A rectangle is used whenever calculations to be performed.

Variable= Expression

Z= b2+4ac Sum= Sum+ (X+i)

i= i+1

4. Decision Blocks.

• Allow you take different paths through flow chart based on certain conditions.

• A comparison is made between

• Two variables.

• A variable and a constant.

• An expression and a variable or a constant.

A> B T

F

i≤ 20 T

F

A≠ B T

F

Relational operator

5. Connector.

• To connect different parts of the long flow charts.

1

6. Flow line.

• Arrow headed lines to show the sequence of execution.

• Normal flow from top to down and left to right.

Ex.1:

• Draw a flow chart to find the roots of the second degree equation: Ax2+Bx+C=0

• Draw a flow chart to compute and print the sum of the first N integers; 1+2+3+…+N.

Ex.2:

• Draw a flow chart to compute and print the sum and the average of N numbers.

(Trace I, sum for 1,2,4,5)

Ex.3:

• Draw a flow chart to print the largest of 1000 positive numbers read one by one. (Exercise: If not positive?)

Ex.4:

• Draw a flow chart to compute and print

for a set of points (xi, yi), i=1, 2, ..,n

Ex.5:

• Draw a flow chart to read X and find Sum by adding the series:

Sum=x+2x2+3x3+…+NxN

Ex.6:

• Draw a flow chart to compute N! by using the formula:

N!=1.2.3…..N

Ex.8:• Draw a flow chart to calculate and print SUM1 and

SUM2 where:

• SUM1= 5+ Z2/4+Z3/9+… stop after adding N terms,

Ex.9:• Draw a flow chart to calculate and print SUM1,

SUM2, and R where:

• SUM1= X3/3+X4/4+X5/5+… stop after adding 15terms.

• SUM2 =SUM1+(Y+1)+(Y+2)2+… stop after adding 20terms.

• R=((SUM1)2+(SUM2 )2)/4.

Ex.10:• Draw a flow chart to write the output for a simple

calculator that performs addition, subtraction,multiplication and division on two numbers enteredin the following manner: First number, operator, andsecond number.

Ex.11:• Draw a flowchart to calculate and print the

commission of a salesman using the following rules:

Sales (s) Commission (C)

S≤500 1%

500<S≤1200 2%

S>1200 4%

Ex.12:• Draw a flow chart to print the location of all negative

numbers in an unordered array of 30 real numbers.