et156 introduction to c programming

32
Credit hours: 4 Contact hours: 50 (30 Theory, 20 Lab) Prerequisite: TB143 Introduction to Personal Computers

Upload: wardah

Post on 05-Jan-2016

32 views

Category:

Documents


2 download

DESCRIPTION

ET156 Introduction to C Programming. Credit hours: 4 Contact hours: 50 (30 Theory, 20 Lab) Prerequisite: TB143 Introduction to Personal Computers. Unit 2 Arithmetic Expressions and Functions. Content Covered: Problem Solving and Program Design in C: Chapter 2, “Overview of C,” - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: ET156 Introduction to C Programming

Credit hours: 4Contact hours: 50 (30 Theory, 20 Lab)Prerequisite: TB143 Introduction to Personal Computers

Page 2: ET156 Introduction to C Programming

Unit 2 Arithmetic Expressions and Functions

Content Covered:Problem Solving and Program Design in C:

Chapter 2, “Overview of C,” pp. 70-100, Section 2.5 through Chapter Review

Chapter 3, “Top-Down Design with Functions,” pp. 108-151

Page 3: ET156 Introduction to C Programming

Unit 2 Arithmetic Expressions and Functions

Objectives1. Create a C program that performs input,

processing, and output.1.9: Write a program that uses arithmetic

operators.1.10: Use placeholders to format output.1.11: Compare interactive mode and batch mode

operations.

2. Write a program that uses variables and constants.2.5: Write code that converts data to a different data type.

Page 4: ET156 Introduction to C Programming

Unit 2 Arithmetic Expressions and Functions

Objectives – Cont…3. Apply modular programming techniques to C

programming.3.1: Write code that uses libraries.3.2: Analyze a problem and define an algorithm to

resolve it.3.3: Explain how C library functions can simplify

program development.3.4: Use top-down design to break a problem into

smaller problems.3.5: Create a structure chart to track

relationships among subprograms.3.6: Write functions and function prototypes to

create modular code.

Page 5: ET156 Introduction to C Programming

Unit 2 Arithmetic Expressions and Functions

Objectives – Cont…4. Use debugging techniques to locate and

correct programming errors.4.1: Explain the difference between syntax errors,

runtime errors, and logic errors.4.2: Explain why it is important to predict and

verify the results of an operation.

5. Write a program that reads and writes data to a file.5.1: Use fopen, fscanf, and fprintf to perform

basic file I/O.

Page 6: ET156 Introduction to C Programming

Unit 2 Arithmetic Expressions and Functions

1. Arithmetic operators2. Data type

conversion3. Formatting output4. Interactive mode vs.

batch mode5. fopen, fscanf, and

fprintf6. Syntax errors,

runtime errors, and logic errors

7. Predicting and verifying results

8. Problem analysis9. Algorithms10. C libraries11. Top-down design12. Structure charts13. Function prototypes

and functions

Key Concepts and Objectives

Page 7: ET156 Introduction to C Programming

1-7

Page 8: ET156 Introduction to C Programming

1-8

Page 9: ET156 Introduction to C Programming

1-9

Page 10: ET156 Introduction to C Programming

1-10

Page 11: ET156 Introduction to C Programming

1-11

Page 12: ET156 Introduction to C Programming

1-12

Page 13: ET156 Introduction to C Programming

1-13

Page 14: ET156 Introduction to C Programming

1-14

Page 15: ET156 Introduction to C Programming

1-15

Page 16: ET156 Introduction to C Programming

1-16

Page 17: ET156 Introduction to C Programming

1-17

Page 18: ET156 Introduction to C Programming

1-18

Page 19: ET156 Introduction to C Programming

1-19

Page 20: ET156 Introduction to C Programming

1-20

Page 21: ET156 Introduction to C Programming

1-21

Page 22: ET156 Introduction to C Programming

1-22

Page 23: ET156 Introduction to C Programming

1-23

Page 24: ET156 Introduction to C Programming

HomeworkThe following homework is designed to cover the course objectives

for this unit. Assignment 2.1:Write a short paragraph explaining

syntax errors runtime errors logic errors.

 Assignment 2.2:

Complete the following exercises from Problem Solving and Program Design in C: Page 152, Review Questions 1-5

Unit 2 Arithmetic Expressions and Functions

Page 25: ET156 Introduction to C Programming

HomeworkThe following homework is designed to cover the course objectives

for this unit. Lab 2.3: Lawn Mowing Program

Fill in the blanks

Lab 2.4: Design and Write a Program Using Functions You will design and write a program that uses functions and

arithmetic operations.

 Project Part 1:Your instructor will give you the complete project description.

Project Part 1 is due at the beginning of the next unit.

Unit 2 Arithmetic Expressions and Functions

Page 26: ET156 Introduction to C Programming

You will design and write a program that uses functions and arithmetic operations.

1. Read Programming Project #9 on page 104 of your textbook. Your program will be based on this project, with the following exceptions:

a. It will output the area in square feet for the yard and the house.

b. It will output the time required to cut the grass in minutes, rounded to the nearest whole minute.

The program should accept real numbers for height and width.

2. Create a Word file that documents the program’s design. Make sure to include:

• Program constants

• Program inputs

• Program outputs

• Algorithm

Unit 2 Lab 2.4: Design and Write a Program Using Functions

Page 27: ET156 Introduction to C Programming

You will design and write a program that uses functions and arithmetic operations.

3. Add a sketch of the structure chart for the program to the Word document. Use the Shape tools in Word or use another program and copy and paste the drawing into Word.

4. Answer the following questions:a. Is it possible to create one function that can be used more than once?b. How many arguments would you pass to a function named

calcRectangleArea? Think of the formula you need to use.c. What data type should the calcRectangleArea function return? Decimal or not.

Unit 2 Lab 2.4: Design and Write a Program Using Functions

Page 28: ET156 Introduction to C Programming

5. Desk-check your algorithm with at least two sets of values. Record your desk-check data in the Word document.

Unit 2 Lab 2.4: Design and Write a Program Using Functions

Input width of yard

Input length of yard

Input width of house

Input length of house

Output sq feet to mow

Output time to mow

Yard

House

Yard Area = length * width

House Area = length * width

Mow Area = Yard Area – House Area

Page 29: ET156 Introduction to C Programming

6. Answer the following question:a. What type of errors does desk-checking your algorithm help identify? Is it syntax, run-time, or logic errors?

7. Create a new WIN32 Console project named nnLab2-1 in a new workspace, replacing nn with your initials.

8. Add a new source code file to the project and save it as calcMowTime.c.

9. Add comments to document the lab name, your name, and the purpose of the project.

/* comments go here */ 10. Add a preprocessor directive to include the stdio.h library. #include <stdio.h>

Unit 2 Lab 2.4: Design and Write a Program Using Functions

Page 30: ET156 Introduction to C Programming

11. Add preprocessor directives for each constant macro. #define SQ_FT_GRASS_PER_SEC 2 #define SECONDS_PER_MINUTE 60 12. Add the function prototype for the calcRectangleArea function. double calcRectangleArea(double length, double width);

13. Answer the following question: a. What did you use as the function’s return type?

14. Write code to implement the calcRectangleArea function. double calcRectangleArea(double length, double width)

{return (length * width);

}

Unit 2 Lab 2.4: Design and Write a Program Using Functions

Page 31: ET156 Introduction to C Programming

15. Add a main function to the program and add code to do the following:• Declare input and output variables.• Declare variables to store the area of the house and the area of the

yard.• Prompt the user for the input data.• Calculate the area of the yard.• Calculate the area of the house.• Calculate the number of minutes required to cut the grass.• Output the area of the yard and the area of the house as a number

with a width of 10 digits and one decimal digit.• Output the number of minutes required to cut the grass, rounded to

the nearest number.

16. Click Project > Build nnLab2-1.17. Answer the following question:a. What type of errors can be identified by the compiler?18. Test your program using the desk-check values and correct any errors

you find.

Unit 2 Lab 2.4: Design and Write a Program Using Functions

Page 32: ET156 Introduction to C Programming

Unit 2 Lab 2.4: Design and Write a Program Using Functions

Make sure your name is a comment in the program. In Pelles, click File and Print the Code

When the code works, run the code, add the data. Before closing the command console, print the screen by using ALT + PrtSrc

Staple the work and turn in for a grade.