chapter 2 overview of c instructor: kun-mao chao ( 台大資工 趙坤茂 )

Post on 26-Dec-2015

260 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Chapter 2Overview of C

Instructor: Kun-Mao Chao(台大資工 趙坤茂 )

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-2

C Language Elements in Miles-to-Kilometers Conversion Program

保留字 . See Appendix E. (p. 819)宣告 (declaration). See. Sec. 2.2.

See Appendix B. (p. 789)

Operator (運算子 ) See Appendix C. (p. 807)

p. 36

Identifier, pp. 38

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-3

Memory Before and After Execution of a Program

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-4

Before and After assignment of a variable

kms = KMS_PER_MILE * miles;

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-5

Before and After Assignment of a Variable

sum = sum + item;

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-6

How to Read Input from the End User

scanf("%lf", &miles);

“%lf” stands for reading a double variable.

“&miles” is the memory address where the double variable will be stored.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-7

How to Read Input from the End User

scanf("%c%c%c", &first, &middle, &last);

“%c” stands for reading a character variable.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-8

General Form of a C Program

preprocessor directives: #include and #define

Any used variable in the program should be declared before the usage.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-9

The Execution for Multiple Operators (1/2)

The execution of multiple operators can be expressed as an evaluation tree.

First, evaluate c=PI* radius

Then, evaluate area=c* radius

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-10

The Execution for Multiple Operators (2/2)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-11

Evaluation Tree and Evaluation for v = (p2 - p1) / (t2 - t1)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-12

Evaluation Tree and Evaluation for z - (a + b / 2) + w * -y

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-13

Homework #1

• Write a program to convert a temperature in degrees Fahrenheit to degrees Celsius– Formula: celsius = 5/9 * (fahrenheit - 32)– Hint: revise the Miles-to-Kilometers conversion

program.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-14

Outline of Lab. This Week

• How to create an empty project– TA is sorry for the mistake last week due to the

non-empty project.

• How to write a program efficiently– Introduction to the debugging skill and tool

• Please refer to Appendix G (p. 829) for Visual C++ IDE (Integrated Development Environment).

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-15

Case Study: Finding the Value of Coins (1/3)

• Write a program to determine the value of a collection of coins– e.g., quarters, dimes, nickels, and pennies.

• The algorithmic flow:– 1. Get and display the customer’s initials.– 2. Get the count of each kind of coin.– 3. Compute the total value in cents.– 4. Find and display the value in dollars and

change.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-16

Case Study: Finding the Value of Coins (2/3)

1. Read the initials of the customer

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-17

Case Study: Finding the Value of Coins (3/3)

2. Read the count of each kind of coins

3. Compute the total value in cents

4. Find and display the value in dollars and change

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-18

The input can be read from a file instead of from the user.

The output can be written into a file instead of on the screen.

Read input from a file

Write the result into a file

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-19

A Program with Syntax Errors

Syntax error occurs when the code violates grammar rules of C and is detected by the compiler.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-20

A Program with a Run-Time Error

Run-time error occurs when the program directs the computer to perform an illegal operation (e.g., divide by zero).

temp=0divide by zero

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-21

A Common Error with Carriage Return

Read “2003”

Read “\n”, “B”, “M” instead of “B”, “M”, “C”

Suppose the user input “2003” and press enter key.Then input “BMC” and press enter key.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-22

A Common Error That Produces Incorrect Results Due to & Omission

scanf does not know where to store the value entered by the user, and just use the original value stored in first and second.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 2-23

Outline of Lab. Lecture This Week

• Demo of your Homework #1.– Please bring the source code and executable

file.

• Your homepage

• Other Visual C++ 6.0 editing functionalities.

• File management in Visual C++ 6.0.

top related