Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 © FCIT@KAU 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.

3 © FCIT@KAU 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 1. 2. 3. 4. 5. 6.

4 © FCIT@KAU 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 1. 2. 3. 4. 5. 6. 4 1

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

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

7 © FCIT@KAU The Software Development Method 4. Implementation: (تنفيذ) Write the code. Implementation ProblemAnalysis Design / Algorithm TestingMaintenance 1. 2. 3. 4. 5. 6. 7 1 C. Example

8 © FCIT@KAU 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 1. 2. 3. 4. 5. 6. 8 1

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

10 © FCIT@KAU 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

11 © FCIT@KAU 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)

12 © FCIT@KAU 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 = 0750428;  dollar = 3.75;  gender = ‘M’; 12 2 Expression could be a number or an equation

13 © FCIT@KAU 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 = 0750428;  double dollar = 3.75;  char gender = ‘M’; 13 2 Expression could be a number or an equation

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

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

16 © FCIT@KAU 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 – 1.5 + 5; 6. Z = Z / 2 + 5; 7. X = Z % 3; 16 The memory stores the last value of each variable #XYZ 1.10 2.15.5 3.25.5 4.26.5 5.30 6.20 7.2 2

17 © FCIT@KAU 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 0750428  #define dollar 3.75 17 3

18 © FCIT@KAU 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

19 © FCIT@KAU 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

20 © FCIT@KAU 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

21 © FCIT@KAU 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

22 © FCIT@KAU 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

23 © FCIT@KAU 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

24 © FCIT@KAU 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

25 © FCIT@KAU 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: 707997 */ 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

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

27 © FCIT@KAU 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

28 © FCIT@KAU Writing Program Structure 28 8 C. Example

29 © FCIT@KAU 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

30 © FCIT@KAU #include int main(void) { // 1. Display “Hello World!!” on the screen printf(“Hello World!!\n”); return(0); } 1. 2. 3. 4. 5. 6. 7. 8. 9. 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); } 1. 2. 3. 4. 5. 6. 7. 8. 9.

31 © FCIT@KAU #include int main(void) { // 1. Display “Hello World!!” on the screen printf(“Hello World!!\n”); return(0); } 1. 2. 3. 4. 5. 6. 7. 8. 9. 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); } 1. 2. 3. 4. 5. 6. 7. 8. 9.

32 © FCIT@KAU 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

33 © FCIT@KAU 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); } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.

34 © FCIT@KAU 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); } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.

35 © FCIT@KAU 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 1.609 kilometer. 35 Write the Analysis & Design for this problem? 3 Implementation ProblemAnalysis DesignOutline Testing Maintenance

36 © FCIT@KAU Convert Miles to Kilometers B. Analysis  Input  KMS_PER_MILE = 1.609  Miles  Output  Kilometers  Formula  Kilometers = Miles x 1.609 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

37 © FCIT@KAU 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 1.609 /* 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); } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18.

38 © FCIT@KAU /* Converts distances from miles to kilometers. */ #include /* printf, scanf definitions */ #define KMS_PER_MILE 1.609 /* 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 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. This program has a mistake that may confuse any user, what is it?

39 © FCIT@KAU /* Converts distances from miles to kilometers. */ #include /* printf, scanf definitions */ #define KMS_PER_MILE 1.609 /* 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); } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 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

40 © FCIT@KAU /* Converts distances from miles to kilometers. */ #include /* printf, scanf definitions */ #define KMS_PER_MILE 1.609 /* 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 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23.

41 © FCIT@KAU /* Converts distances from miles to kilometers. */ #include /* printf, scanf definitions */ #define KMS_PER_MILE 1.609 /* 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); } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. Convert Miles to Kilometers 41 3 Implementation ProblemAnalysis DesignOutline Testing Maintenance

42 © FCIT@KAU 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

43 © FCIT@KAU 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

44 © FCIT@KAU 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

45 © FCIT@KAU 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 % 100 7. 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?

46 © FCIT@KAU /* * 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 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25.

47 © FCIT@KAU /* * 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 47 4 1 dollar = 100 cents 1 quarter = 25 cents 1 dime = 10 cents 1 nickel = 5 cents 1 penny = 1 cent Implementation ProblemAnalysis DesignOutline Testing Maintenance 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22.

48 © FCIT@KAU /* 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 48 4 1 dollar = 100 cents 1 quarter = 25 cents 1 dime = 10 cents 1 nickel = 5 cents 1 penny = 1 cent Implementation ProblemAnalysis DesignOutline Testing Maintenance 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43.

49 © FCIT@KAU Changing the Display - Integer A. You can organize the display for integer variables. B. Formats: 49 ValueFormatDisplayed Output 234%4d ▒ 234 234%5d ▒▒ 234 234%6d ▒▒▒ 234 234%1d234 -234%4d-234 %5d ▒ -234 -234%6d ▒▒ -234 -234%2d-234 9 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 --------- 1 2 13 6 37 513 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 --------- 1 2 13 6 37 513

50 © FCIT@KAU Changing the Display - Double A. You can organize the display for double variables. B. Formats: 50 ValueFormatDisplayed Output 3.14159%5.2f ▒ 3.14 3.14159%3.2f3.14 3.14159%5.3f3.142 3.14159%5.1f ▒▒ 3.1 0.1234%4.2f0.12 -0.006%8.3f ▒▒ -0.006 -0.006%4.2f-0.01 -0.006%8.5f-0.00600 -3.14159%.4f-3.1416 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 = 102.235; 4. Z = 20.2; 5. printf(“>%6.2f\n”, X); 6. printf(“>%6.2f\n”, Y); 7. printf(“>%6.2f\n”, Z); Run: 10.23 102.24 20.20 C. Example: 1. double X, Y, Z; 2. X = 10.23; 3. Y = 102.235; 4. Z = 20.2; 5. printf(“>%6.2f\n”, X); 6. printf(“>%6.2f\n”, Y); 7. printf(“>%6.2f\n”, Z); Run: 10.23 102.24 20.20

51 © FCIT@KAU 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

52 © FCIT@KAU Types of Errors: Syntax Error 52 10.1. 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

53 © FCIT@KAU Types of Errors: Run-Times Error 53 10 Check the cause of the pop-up error message?

54 © FCIT@KAU 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- …

55 © FCIT@KAU 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 – 1.5 + 5; g) 23 = Z; h) Z = Z / 2 + 5; -55- …

56 © FCIT@KAU 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

57 © FCIT@KAU 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.


Download ppt "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."

Similar presentations


Ads by Google