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,” pp. 70-100, Section 2.5 through Chapter Review Chapter 3, “Top-Down Design with Functions,” pp. 108-151 Unit 2 Arithmetic Expressions and Functions Objectives 1. 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. 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. 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. Unit 2 Arithmetic Expressions and Functions Key Concepts and Objectives 1. Arithmetic operators 2. Data type conversion 3. Formatting output 4. Interactive mode vs. batch mode 5. fopen, fscanf, and fprintf 6. Syntax errors, runtime errors, and logic errors 7. Predicting and verifying results 8. Problem analysis 9. Algorithms 10. C libraries 11. Top-down design 12. Structure charts 13. Function prototypes and functions Figure 2.1 C Language Elements in Miles-to-Kilometers Conversion Program Open Pelles C and copy this program 1-7 Figure 2.2 Memory(a) Before and (b) After Execution of a Program 1-8 Figure 2.3 Effect of kms = KMS_PER_MILE * miles; 1-9 Figure 2.4 Effect of sum = sum + item; 1-10 Figure 2.5 Effect of scanf("%lf", &miles); 1-11 Figure 2.6 Scanning Data Line Bob 1-12 Figure 2.7 General Form of a C Program 1-13 Figure 2.8 Evaluation Tree for area = PI * radius * radius; 1-14 Figure 2.9 Step-by-Step Expression Evaluation 1-15 Figure 2.10 Evaluation Tree and Evaluation for v = (p2 - p1) / (t2 - t1); 1-16 Figure 2.11 Evaluation Tree and Evaluation for z - (a + b / 2) + w * -y 1-17 Figure 2.13 Batch Version of Miles-to-Kilometers Conversion Program 1-18 Figure 2.14 Miles-to-Kilometers Conversion Program with Named Files 1-19 Figure 2.15 Compiler Listing of a Program with Syntax Errors 1-20 Figure 2.16 A Program with a Run-Time Error 1-21 Figure 2.17 Revised Start of main Function for Supermarket Coin Value Program 1-22 Figure 2.18 A Program That Produces Incorrect Results Due to & Omission 1-23 Unit 2 Arithmetic Expressions and Functions Homework The 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 Homework The 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 Lab 2.4: Design and Write a Program Using Functions 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 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. main 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 5. Desk-check your algorithm with at least two sets of values. Record your deskcheck data in the Word document. 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 Area = length * width Yard House Mow Area = Yard Area – House Area House Area = length * width Unit 2 Lab 2.4: Design and Write a Program Using Functions 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 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 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 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.