Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CPCS 202 Chapter 2 – Input/Output 12-10-1429.

Slides:



Advertisements
Similar presentations
For loops For loops are controlled by a counter variable. for( c =init_value;c
Advertisements

11-2 Identify the parts of the “main” function, which include Preprocessor Directives main function header main function body which includes Declaration.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 4 (Conditional Statements) © CPCS
TDBA66, VT-03 Lecture - Ch. 21 A complete C-program Display Fig. 2.1 and comment on different things such as Preprocessor directives Header files Identifiers.
1 CS 201 Introduction to C (1) Debzani Deb. 2 Outline Overview of C General form of a C program C Language Elements.
1 ICS103 Programming in C Lecture 8: Data Files. 2 Outline Why data files? Declaring FILE pointer variables Opening data files for input/output Scanning.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
1 CS 201 Introduction to C (2) Debzani Deb. 2 Overview C Arithmetic Expressions Formatting Numbers in Program Output Interactive Mode, Batch Mode, and.
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
COS120 Software Development Using C++ AUBG Fall semester 2010
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
Computer Science 210 Computer Organization Introduction to C.
Chapter 2 Overview of C Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Overview of C++ Problem Solving, Abstraction, and Design using.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Pengantar C/C++. 2 Outline  C overview  C language elements  Variable declarations and data types  Executable statements  General form of a C program.
1 計算機程式設計 Introduction to Computer Programming Lecture01: Introduction and Hello World 9/10/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 3 Slides By Dr. Daniyal Alghazzawi.
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter
CHAPTER 2 PART #3 INPUT - OUTPUT 1 st semester King Saud University College of Applied studies and Community Service Csc
1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.
The Development Process Problem Solving. Problem Solving - Dr. Struble 2 What Is Asked of Computer Programmers? Input Transformation Output Write a computer.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 1 (Software Development Method) © CPCS
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Software Development Method Reference : Problem Solving & Program Design in C ; Jeri R.Hanly, Elliot B.Koffman.
Introduction to Programming
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 3: Introduction to C: Input & Output, Assignments, Math functions.
Khalid Rasheed Shaikh Computer Programming Theory 1.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Overview of C. C—a high-level programming language developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories. We will discuss: –the elements of a.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
1 ELE118 lecture 3 Dr. Mehmet Demirer Dr. Seniha Esen Yuksel.
CISC105 – General Computer Science Class 2 – 6/7/2006.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Computer Programming for Engineers
Introduction Chapter 1 1/22/16. Check zyBooks Completion Click on the boxes for each section.
A.Abhari CPS1251 Topic 2: C Overview C Language Elements Variable Declaration and Data Types Statement Execution C Program Layout Formatting Output Interactive.
Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
C Language Elements Preprocessor Directives # (sign for preprocessor directive commands) #include Standard header file (.h) Library.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
INTRODUCTION TO PROGRAMING System Development Mansoura October 2015.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 3 (Functions) © CPCS /1433 – Term 1.
Lecture2.
CSCE 206 Structured Programming in C
Computer Science 210 Computer Organization
What's a Computer? Monitor Disk Main mouse Memory Keyboard Network
ICS103 Programming in C Lecture 3: Introduction to C (2)
Computer Science 210 Computer Organization
Compiled and ready to run Memory Stack /*
CC213 Programming Applications
Introduction to CS Your First C Programs
Lecture3.
Unit 3: Variables in Java
Chapter 2: Overview of C++
An Overview of C.
Computer Science II CS132/601* Lecture #C-1.
Getting Started With Coding
Presentation transcript:

Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CPCS 202 Chapter 2 – Input/Output

© CHAPTER 2 – Input / Output 1. The Software Development Method 2. Variables 3. Constants 4. Output Operations and Functions 5. Input Operations and Functions 6. Using Comments 7. Design / Algorithm Types 8. Writing Program Structure 9. Changing the Display 10. Types of Errors 2 # 1. Hello World 2. Get User ID 3. Convert Miles to Kilometers 4. Finding the Value of the Coins  column shows the topics index.  column shows the programs index.

© The Software Development Method A. Introduction  You need to be a problem solver  A good problem solver  a good programmer  Programmers use the Software Development Method  This is what you will learn in this course. B. Prototype 3 1 ImplementationProblemAnalysis Design / Algorithm TestingMaintenance

© C. Example The Software Development Method 1. Problem: Your summer surveying job requires you to study some maps that give distances in kilometers and some that use miles. You and your coworkers prefer to deal in metric measurements. Write a program that performs the necessary conversion. Implementation Problem Analysis Design / Algorithm TestingMaintenance

© The Software Development Method 2. Analysis (تحليل): 1. Problem Input: miles 2. Problem Output: kilometers 3. Relevant Formula: 1 mile = kilometers ImplementationProblem Analysis Design / Algorithm TestingMaintenance C. Example

© The Software Development Method 3. Design / Algorithm: 1. Get the distance in miles from the user. 2. Convert the distance to kilometers. (1 kilometer = miles) 3. Display the distance in kilometers on the screen. ImplementationProblemAnalysis Design / Algorithm Design / Algorithm TestingMaintenance C. Example

© The Software Development Method 4. Implementation: (تنفيذ) Write the code. Implementation ProblemAnalysis Design / Algorithm TestingMaintenance C. Example

© C. Example The Software Development Method 5. Testing: 1. Verify that the program works properly. 2. Try few test cases. ( if the input is …, the output has to be … ) ImplementationProblemAnalysis Design / Algorithm Testing Maintenance

© The Software Development Method 6. Maintenance (صيانة): 1. Remove undetected errors. 2. Keep it up-to-date. ImplementationProblemAnalysis Design / Algorithm Testing Maintenance C. Example

© Variables ( متغير ) Introduction  Store values in the memory under given names; these values can be changed  The types of the possible values called Data Types  Choose good names for the variables:  No need to write any comment  Easy to track  Case sensitive  Don’t use a Reserved Word  Three important stages in variables: 1. Declaring a variable 2. Initializing a variable 3. Assigning a new value 10 2 Data Types Numbers only Integer age = 36 Double \ Float degree = 36.4 Character gender = ‘M’ The three data types showing are not all of the data types in C

© Variables – Declaring A. Introduction  Each variable needs to be declared before using it  Declaring a variable reserves space in the memory  Declaring variables has to be at the beginning of functions (before writing any statement) B. Syntax C. Example  int id;  double dollar;  char gender;  double tall, width, weight; 11 2 Data Types Numbers only Integer (int) Double (double) Character (char)

© Variables – Initializing A. Introduction  The first value for a variable called initialing a variable  You can not use a variable without initializing  You can initial a variable at any place inside the function B. Syntax C. Example  id = ;  dollar = 3.75;  gender = ‘M’; 12 2 Expression could be a number or an equation

© Variables – Declaring & Initializing A. Introduction  You can save the space and the time by initializing a variable in the same time with declaring it  Again, this has to be at the beginning of functions before writing any statement B. Syntax C. Example  int id = ;  double dollar = 3.75;  char gender = ‘M’; 13 2 Expression could be a number or an equation

© Variables – Assigning A. Introduction  Changing the value of a variable called assigning a new value to the variable B. Syntax C. Example  id = ;  dollar = 3.75;  gender = ‘M’; 14 2 Expression could be a number or an equation

© Variables Declaring, Assigning, and Initializing Conclusion 1  int id, age;  double dollar;  char gender = ‘d’;  id = ;  dollar = 3.75;  age = 21;  dollar = 3.77; 15 2 Declaring Initializing Assigning Declaring & Assigning

© Variables Tracking Variables in the Memory Conclusion 2  You can track the values of each variable in a table  The following program consists of 3 variables. The table tracks the values of each variable after executing each statement: 1. X = 10; 2. Z = 15.5; 3. Y = X + Z; 4. Y = Y + 1; 5. Z = Y – ; 6. Z = Z / 2 + 5; 7. X = Z % 3; 16 The memory stores the last value of each variable #XYZ

© Constants ( ثابت ) A. Introduction  Store a value under a name  The value of the constant can’t be changed = You can’t assign a new value  Constants require one stage only, which is defining; it has to be at the begging of the program before writing ant function  Work with numbers only B. Syntax C. Examples  #define id  #define dollar

© Output Operations and Functions A. Introduction  The output operations and functions will help you to display anything on the screen.  You can display some text only, a value of a variable only, or both.  In C language, you need to include the file stdio in order to use the function printf. B. Syntax 18 4

© Output Operations and Functions C. Examples 1. Display Hello… on the screen in C? use the function printf printf(“Hello…”); 2. Display Hello… in 1 st line and good in 2 nd line? use the operation \n printf(“Hello… \ ngood”); 3. Display the value of the variable age; (if age is integer)? use the operation %d printf(“%d”, age); 19 4

© Output Operations and Functions C. Examples 4. Display the value of the variable X; (if X is double\float)? use the operation %f printf(“%f”, X); 5. Display the value of the variable Y; (if Y is character)? use the operation %c printf(“%c”, Y); 6. Display My age is then the value of the variable AGE? printf(“My age is %d”, AGE); 7. Display the variables age and GPA in one statement? printf(“I am %d years old, GPA: %f”, age, GPA); 20 4

© Output Operations and Functions D. Conclusion  The output operations and functions will help you to display text and/or the values of a group of variables on the screen. For examples :  printf(“Enter the object mass in grams?”);  printf(“%c”, first_init);  printf(“I am %d years old.”, AGE); 21 4

© Input Operations and Functions A. Introduction  The input operations and functions will help you to get values for the variables from users.  You need to ask the user to input a value using the output function, then you can use the input function to get the value from the user.  In C language, you need to include the file stdio in order to use the function scanf. B. Syntax 22 5

© Input Operations and Functions C. Examples 1. Get the value for the variable X; (if X is integer)? use the operation %d scanf(“%d”, &X); 2. Get the value for the variable X; (if X is double\float)? use the operation %lf (Long Float) and not %f scanf(“%lf”, &X); 3. Get the value for the variable X; (if X is character)? use the operation %d scanf(“%c”, &X); 23 5

© Input Operations and Functions C. Examples 4. Get 3 characters from the user? scanf(“%c%c%c”, &first, &second, &third); D. Conclusion  The input operations and functions will help you to get a value for a declared variable from the user. For examples :  scanf(“%c%d”, &first_initial, &age); 24 5

© Using Comments A. With your comments, it will easy to remember the job of each statement in your program. B. Comment Types in C language:  Single-line: Start with the symbol // and end up with the end of the line  Single-line or Multi-lines: Start with /* and end up with */ C. Example: 1. /* Name: Daniyal 2. ID: */ 3. double miles, kms; 4. /* EXECUTABLE STATMENTS */ 5. printf(“Enter the distance in miles: “); // ask the user 6. scanf(“%lf”, &miles); // get the miles 25 6

© Design/Algorithm Types  Algorithm without variables: 1. Get the distance in miles. 2. Convert the distance to kilometers. (1 kilometer = miles) 3. Display the distance in kilometers.  Algorithm with variables: 1. Get the value X (X: the distance in miles) 2. Y = X / (Y: the distance in kilo) 3. Display the value Y  Algorithm with good variables’ names: 1. Get the value TotalMiles 2. TotalKilo = TotalMiles / Display the value TotalKilo 26 7 Algorithms could write any way, but they have to be understandable

© Writing Program Structure A. Any group of statements needs to be inside a function (Note: you will learn later more about functions and write more than one function) B. The main function will be executed first 1. /* include the header files here for any external function, such as input/output functions */ 2. /* define any Constant (not Variable) here */ 3. int main (void) 4. { 5. /* Declare the variables here */ 6. /* Start writing the statements */ 7. return (0); 8. } you have to declare the variables at the beginning of any function 27 8 Be Ready to Write a Program in C

© Writing Program Structure 28 8 C. Example

© Hello World A. Problem  We need a program that displays on the screen the text Hello World!! B. Analysis  Input  Output  the text “Hello World!!”  Formula C. Design 1. Display “Hello World!!” on the screen 29 1 Implementation ProblemAnalysis DesignOutline Testing Maintenance

© #include int main(void) { // 1. Display “Hello World!!” on the screen printf(“Hello World!!\n”); return(0); } Hello World D. Outline E. Implementation 30 1 Implementation ProblemAnalysis DesignOutline Testing Maintenance #include int main(void) { // 1. Display “Hello World!!” on the screen return(0); }

© #include int main(void) { // 1. Display “Hello World!!” on the screen printf(“Hello World!!\n”); return(0); } Hello World D. Outline E. Implementation 31 1 Implementation ProblemAnalysis DesignOutline Testing Maintenance #include int main(void) { // 1. Display “Hello World!!” on the screen return(0); }

© Get User ID A. Problem  Write a program that gets the ID value from the user B. Analysis  Input  ID  Output  Formula C. Design 1. Get the ID from the user  user_ID 32 Put the value you get from the user in a variable called user_ID 2 Implementation ProblemAnalysis DesignOutline Testing Maintenance Number of variables: 1 This indicates that you need to declare 1 variable in the program

© Get User ID 33 2 Implementation ProblemAnalysis DesignOutline Testing Maintenance #include int main(void) { int user_ID; // 1. Get the ID from the user return(0); }

© Get User ID 34 2 DON’T try to get a value from a user (scanf) without asking (printf) Implementation ProblemAnalysis DesignOutline Testing Maintenance #include int main(void) { int user_ID; // 1. Get the ID from the user printf(“Please enter your user ID: “); scanf(“%d”, user_ID); return(0); }

© Convert Miles to Kilometers A. Problem  Your summer surveying job requires you to study some maps that give distances in kilometers and some that use miles. You and your coworkers prefer to deal in metric measurements. Write a program that performs the necessary conversion.  Each miles equal to kilometer. 35 Write the Analysis & Design for this problem? 3 Implementation ProblemAnalysis DesignOutline Testing Maintenance

© Convert Miles to Kilometers B. Analysis  Input  KMS_PER_MILE =  Miles  Output  Kilometers  Formula  Kilometers = Miles x C. Design 1. Get the number of miles from the user  miles 2. Convert miles to kilometers: kms = miles x KMS_PER_MILE 3. Display the number of kilometers  kms 36 3 Implementation ProblemAnalysis DesignOutline Testing Maintenance They are 2 variables in the Design, so 2 variables need to be declared This input has a constant value

© Convert Miles to Kilometers 37 3 Implementation ProblemAnalysis DesignOutline Testing Maintenance /* Converts distances from miles to kilometers. */ #include /* printf, scanf definitions */ #define KMS_PER_MILE /* conversion constant */ int main(void) { double miles,/* distance in miles */ kms; /* equivalent distance in kilometers */ /* 1. Get the number of miles from the user */ /* 2. Convert miles to kilometers */ /* 3. Display the number of kilometers */ return (0); }

© /* Converts distances from miles to kilometers. */ #include /* printf, scanf definitions */ #define KMS_PER_MILE /* conversion constant */ int main(void) { double miles,/* distance in miles */ kms; /* equivalent distance in kilometers */ /* 1. Get the number of miles from the user */ scanf("%lf", &miles); printf("The distance in miles is %.2f.\n", miles); /* 2. Convert miles to kilometers */ kms = KMS_PER_MILE * miles; /* 3. Display the number of kilometers */ printf("That equals %.2f kilometers.\n", kms); return (0); } Convert Miles to Kilometers 38 3 Implementation ProblemAnalysis DesignOutline Testing Maintenance This program has a mistake that may confuse any user, what is it?

© /* Converts distances from miles to kilometers. */ #include /* printf, scanf definitions */ #define KMS_PER_MILE /* conversion constant */ int main(void) { double miles,/* distance in miles */ kms; /* equivalent distance in kilometers */ /* 1. Get the number of miles from the user */ scanf("%lf", &miles); printf("The distance in miles is %.2f.\n", miles); /* 2. Convert miles to kilometers */ kms = KMS_PER_MILE * miles; /* 3. Display the number of kilometers */ printf("That equals %.2f kilometers.\n", kms); return (0); } Convert Miles to Kilometers 39 3 Implementation ProblemAnalysis DesignOutline Testing Maintenance The programmer tries to get a value from the user without display any message on the screen to notify the user

© /* Converts distances from miles to kilometers. */ #include /* printf, scanf definitions */ #define KMS_PER_MILE /* conversion constant */ int main(void) { double miles,/* distance in miles */ kms; /* equivalent distance in kilometers */ /* 1. Get the number of miles from the user */ printf("Please enter the value of distance in miles : "); scanf("%lf", &miles); printf("The distance in miles is %.2f.\n", miles); /* 2. Convert miles to kilometers */ kms = KMS_PER_MILE * miles; /* 3. Display the number of kilometers */ printf("That equals %.2f kilometers.\n", kms); return (0); } Convert Miles to Kilometers 40 3 Implementation ProblemAnalysis DesignOutline Testing Maintenance

© /* Converts distances from miles to kilometers. */ #include /* printf, scanf definitions */ #define KMS_PER_MILE /* conversion constant */ int main(void) { double miles,/* distance in miles */ kms; /* equivalent distance in kilometers */ /* 1. Get the number of miles from the user */ printf("Please enter the value of distance in miles : "); scanf("%lf", &miles); printf("The distance in miles is %.2f.\n", miles); /* 2. Convert miles to kilometers */ kms = KMS_PER_MILE * miles; /* 3. Display the number of kilometers */ printf("That equals %.2f kilometers.\n", kms); return (0); } Convert Miles to Kilometers 41 3 Implementation ProblemAnalysis DesignOutline Testing Maintenance

© Finding the Value of the Coins A. Problem  We need a program that calculates the number of coins in a save box, and it displays the number of the dollars and the changes in cents.  USA Currency Coins:  1 quarter = 25 cents  1 dime = 10 cents  1 nickel = 5 cents  1 penny = 1 cent  For example:  10 quarters + 8 dimes + 1 nickels + 10 pennies = (10 x 25) + (8 x 10) + (1 x 5) + (10 x 1) = 345 cents = 3 dollars and 45 cents 42 Quiz: Write the Analysis & Design for this problem? 4 Implementation ProblemAnalysis DesignOutline Testing Maintenance

© Finding the Value of the Coins B. Analysis  Input  The count of quarters  The count of dimes  The count of nickels  The count of pennies  Output  The value in dollars  The changes in cents  Formula  1 quarter = 25 cents1 dime = 10 cents  1 nickel = 5 cents1 penny = 1 cent 43 4 Implementation ProblemAnalysis DesignOutline Testing Maintenance

© Finding the Value of the Coins C. Design (1 st version) 1. Get the count of each kind of coin 2. Find the value in dollars and change 3. Display the value in dollars and the change 44 4 Implementation ProblemAnalysis DesignOutline Testing Maintenance C. Design (2 nd version) 1. Get the count of each kind of coin 2. Compute the total value in cents 3. Find the value in dollars and change 4. Display the value in dollars and the change

© Finding the Value of the Coins C. Design (3 rd version) 1. Get the count of the quarters  quarters 2. Get the count of the dimes  dimes 3. Get the count of the nickels  nickels 4. Get the count of the pennies  pennies 5. Compute the total value in cents: total_cents = quarters x 25 + dimes x 10 + nickels x 5 + pennies x 1 6. Find the value in dollars and change: dollars = total_cents / 100 change = total_cents % Display the value in dollars  dollars 8. Display the change  change 45 4 Implementation ProblemAnalysis DesignOutline Testing Maintenance How many variables need to be declared? and what are their types?

© /* * Determines the value of a collecting of coins. */ #include int main(void) { int pennies, nickels; /* input - count of each coin type */ int dimes, quarters; /* input - count of each coin type */ int change; /* output - change amount */ int dollars; /* output - dollar amount */ int total_cents; /* total cents */ /* 1. Get the count of the quarters */ /* 2. Get the count of the dimes */ /* 3. Get the count of the nickels */ /* 4. Get the count of the pennies */ /* 5. Compute the total value in cents. */ /* 6. Find the value in dollars and change. */ /* 7. Display the value in dollars. */ /* 8. Display the change. */ return(0); } Finding the Value of the Coins 46 4 Implementation ProblemAnalysis DesignOutline Testing Maintenance

© /* * Determines the value of a collecting of coins. */ #include int main(void) { int pennies, nickels; /* input- count of each coin type */ int dimes, quarters; /* input- count of each coin type */ int change; /* output- change amount */ int dollars; /* output- dollar amount */ int total_cents; /* total cents */ /* 1. Get the count of the quarters */ printf("Number of quarters> "); scanf("%d", &quarters); /* 2. Get the count of the dimes */ printf("Number of dimes> "); scanf("%d", &dimes); Finding the Value of the Coins dollar = 100 cents 1 quarter = 25 cents 1 dime = 10 cents 1 nickel = 5 cents 1 penny = 1 cent Implementation ProblemAnalysis DesignOutline Testing Maintenance

© /* 3. Get the count of the nickels */ printf("Number of nickels> "); scanf("%d", &nickels); /* 4. Get the count of the pennies */ printf("Number of pennies> "); scanf("%d", &pennies); /* 5. Compute the total value in cents. */ total_cents = 25 * quarters + 10 * dimes + 5 * nickels + pennies; /* 6. Find the value in dollars and change. */ dollars = total_cents / 100; change = total_cents % 100; /* 7. Display the value in dollars. 8. Display the change. */ printf("\nYour coins are worth %d dollars and %d cents.\n", dollars, change); return(0); } Finding the Value of the Coins dollar = 100 cents 1 quarter = 25 cents 1 dime = 10 cents 1 nickel = 5 cents 1 penny = 1 cent Implementation ProblemAnalysis DesignOutline Testing Maintenance

© Changing the Display - Integer A. You can organize the display for integer variables. B. Formats: 49 ValueFormatDisplayed Output 234%4d ▒ %5d ▒▒ %6d ▒▒▒ %1d %4d-234 %5d ▒ %6d ▒▒ %2d C. Example: 1. printf(“ X Y\n”); 2. printf(“ \n”); 3. printf(“%4d%4d\n”, 1, 2); 4. printf(“%4d%4d\n”, 13, 6); 5. printf(“%4d%4d\n”, 37, 513); Run: X Y C. Example: 1. printf(“ X Y\n”); 2. printf(“ \n”); 3. printf(“%4d%4d\n”, 1, 2); 4. printf(“%4d%4d\n”, 13, 6); 5. printf(“%4d%4d\n”, 37, 513); Run: X Y

© Changing the Display - Double A. You can organize the display for double variables. B. Formats: 50 ValueFormatDisplayed Output %5.2f ▒ %3.2f %5.3f %5.1f ▒▒ %4.2f %8.3f ▒▒ %4.2f %8.5f %.4f Hint: think about the displayed output first, then write the format 9 C. Example: 1. double X, Y, Z; 2. X = 10.23; 3. Y = ; 4. Z = 20.2; 5. printf(“>%6.2f\n”, X); 6. printf(“>%6.2f\n”, Y); 7. printf(“>%6.2f\n”, Z); Run: C. Example: 1. double X, Y, Z; 2. X = 10.23; 3. Y = ; 4. Z = 20.2; 5. printf(“>%6.2f\n”, X); 6. printf(“>%6.2f\n”, Y); 7. printf(“>%6.2f\n”, Z); Run:

© Types of Errors The program will not run until the error fixed. e.g. missing a semicolon. 1. Syntax Error Discovered while the program running. e.g. dividing a number by zero 2. Run-time Error The result is not correct. e.g. to find the sum of X and Y, the equation is X/Y 3. Logic Error 51 10

© Types of Errors: Syntax Error A pop-up window will appear. CHOSE No to stop the program.1. A pop-up window will appear. CHOSE No to stop the program.2. Check the error message and the line number.2. Check the error message and the line number.3. Fix the error.3. Fix the error

© Types of Errors: Run-Times Error Check the cause of the pop-up error message?

© Questions 1. The main different between the variables and the constants in a program is: a) the value of the constant can be changed during the program b) the value of the variable can be changed during the program c) there is no difference between them d) none of the above is a correct statement 2. The codes at any function written in C language need to follow the following order: a) first, we write the executed statements. Then, we declare the variables b) first, we declare the variables. Then, we write the executed statements c) we can declare any variable at anyplace inside a function -54- …

© Questions 3. Which of the following statements have a syntax error: a) X = 10; b) Y = 20 c) Z = 15.5; d) Y = X + Z; e) Y = Y + 1; f) Z = Y – ; g) 23 = Z; h) Z = Z / 2 + 5; -55- …

© Homework 1. Type Program 3 and run it, and display your name and your ID at the beginning of the output ? (Take a copy of the code and a snapshot of the output of three different test cases, and then print them in ONE page only) 2. Track the memory in Program 4 ? (You need to show the code, the output of one test case, and tracking the memory in ONE page only; you can choose any input values) -56- … HANDWRITING IN THE HOMEWORK IS NOT ACCEPTABLE

© CHAPTER 2 – Input / Output 1. The Software Development Method 2. Variables 3. Constants 4. Output Operations and Functions 5. Input Operations and Functions 6. Using Comments 7. Design / Algorithm Types 8. Writing Program Structure 9. Changing the Display 10. Types of Errors 57 # 1. Hello World 2. Get User ID 3. Convert Miles to Kilometers 4. Finding the Value of the Coins  column shows the topics index.  column shows the programs index.