[go: up one dir, main page]

0% found this document useful (0 votes)
56 views41 pages

Chapter 12 - Writing A Complete Program (Student)

The document outlines the six main steps of the programming process: 1) understand the problem, 2) plan the logic, 3) code the program, 4) translate the program into machine language, 5) test the program, and 6) put the program into production. It then provides examples of two programming exercises, outlining the analysis, logical steps, and code for calculating car rental charges and a computer salesman's weekly salary.

Uploaded by

vinjasmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views41 pages

Chapter 12 - Writing A Complete Program (Student)

The document outlines the six main steps of the programming process: 1) understand the problem, 2) plan the logic, 3) code the program, 4) translate the program into machine language, 5) test the program, and 6) put the program into production. It then provides examples of two programming exercises, outlining the analysis, logical steps, and code for calculating car rental charges and a computer salesman's weekly salary.

Uploaded by

vinjasmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

CHAPTER 12:

Writing a Complete Program


Learning Outcome:

● The steps that are necessary to develop a


computer program.
● Logical flow through a program.
● Introduction of a procedural program.
● The basic tasks that must be performed in
building a C program.
● The analysis on the problems given.
Programming Process (Steps)

What are the SIX (6) steps programming process?

(1) Understand the


problem
(4) Translate the program
into machine language

(2) Plan the logic


(5) Test the program

(3) Code the program


(6) Put the program into
production

Next
Programming Process (Steps)

1. Understand the problem


2. Plan the logic
3. Code the program
4. Translate the program into machine
language
5. Test the program
6. Put the program into production
Programming Process (Steps)

1. Understand the problem


• Gain a clear understanding of what is
required in a program.
• To satisfy the needs from the customers.
• Identify possible inputs, processing,
outputs, and additional requirements.
Back
Programming Process (Steps)

1. Understand the problem (continued)


E.g. Data requirement for a program that calculates the circumference
and area of a circle where radius is given:

Constants PI (3.14159)
Inputs radius of circle
Outputs circumference of circle, and area of circle.
Formulas circumference of circle =
2 * PI * radius of circle,
area of circle =
PI * radius of circle * radius of circle
Programming Process (Steps)

2. Plan the logic


• Plans the logical steps to the program.
• Tools: Flowchart and Pseudocode
• Breaks a large programming problem into
reasonable units.
• Reasonable units: Modules, subroutines,
procedures, functions or methods.
• This process is known as modularization.
(refer appendix)
Programming Process (Steps)

3. Code the program


• Choose one or more programming
language to write the program after
preparing the previous steps.
• Planning before coding programs
produced are less prompt to errors and
changes.
Programming Process (Steps)

4. Translate the program into machine


language
• Convert the program into machine language
using compiler or interpreter.
• Error messages displayed as programming
language statements written incorrectly.
Programming Process (Steps)

5. Test the program


• To ensure the program runs correctly and
is error free.
• Sample data is needed: Valid (correct) and
invalid (incorrect) input data.
Programming Process (Steps)

5. Test the program (continued)


E.g. A set of valid and invalid input data that can be used for testing
purpose:

Field Valid input data Invalid input data


Name Ang Boon Ching 1234567890@#$
* Name shouldn’t contain invalid
characters.
Age 18 ABCDE, or -1
* Age should contain only digits, and
shouldn’t be negative.
Gender M ?
* Gender should be either M or F.
Back
Programming Process (Steps)

5. Test the program (continued)


• 3 types of errors in testing:
a) Syntax error
b) Logic error
c) Runtime error
Programming Process (Steps)

5. Test the program (continued)


• (a) Syntax error: Code that violates the
syntax or grammar of a programming
languages. E.g.:
integer num; // should be int

if (num > 100)


printf("Big\n");
else (num <= 100) // invalid else
printf("Small\n");
Programming Process (Steps)

5. Test the program (continued)


• (b) Logic error: Program produces
incorrect results (e.g. wrong formulas). E.g.:
int num1, num2, sum;

printf("Enter 2 integers > ");


scanf("%d %d", &num1, &num2);

sum = num1 - num2; // wrong formula

printf("Sum = %d\n", sum);


Programming Process (Steps)

5. Test the program (continued)


• (c) Runtime error: An error occurs when
the program is running. E.g.:
int x = 10, y = 0, result;

result = x / y; // division by zero


// causes runtime error

printf("Result = %d\n", result);


Programming Process (Steps)

5. Test the program (continued)


• Debugging: Process of locating and
correcting syntax and logic errors in a
program.
Programming Process (Steps)

6. Put the program into production


• Once the program is tested adequately, it is
ready for the organization to use.
Logical Flow Through a Program

● Procedural program: A program in which


one procedure follows another from
beginning until the end.
Logical Flow Through a Program

● 3 distinct parts in a procedural program:


a) Initialization tasks
b) Main body tasks
c) End-of-job tasks

NOTE: Designing a procedural program can


help to keep the program structured and
organized.
(a) Initialization tasks:

● Perform at the beginning of a program


● The tasks include:
i. preprocessor directives. e.g.
#include<stdio.h>
ii. Declare constant, assign names, and
values. e.g. #define MINUTE 60
iii. Declare and initialize variables. e.g.
int days, hours, minutes;
int totalMinutes;
char repeat = 'Y';
REMEMBER!!!

● Use constants when a fixed value is used


consistently in a program.
● Give meaningful names for memory
locations to store and retrieve data.
● Every variable used in a program must be
declared.
(b) Main body tasks:

● The actual work in the program.


● It may include loops, decisions, and
sequence execution of C statements.
(C) End-of-job tasks:

● Performing the final steps at the end of


the program.
● Normally, for printing messages or results.
Exercise 12.1:
● Write a program that computes a customer’s car
rental charges. Every car charges is RM25.00
per-day plus a charge of RM0.40 for every
kilometer used (kilometer used is to be obtained
from the difference of the beginning and ending
reading of car’s odometer). Finally, the rental
charges will be displayed on the screen.
Exercise 12.1 : (continued)
Analysis: (Data requirement)

Constant RM25.00 per day and RM0.40 per km used.


s
Inputs Number of day, beginning odometer and ending
odometer.
Outputs Rental charges.
Formulas Rental charges = (number of day * RM25.00) +
(kilometers used * RM0.40)
Where, kilometer used = ending odometer -
beginning odometer.
Exercise 12.1 : (continued)
Logical steps:

● Get the number of day


● Get the beginning odometer and ending odometer
● Compute the kilometers used
● Compute the rental charges
● Display the result on screen
Exercise 12.1-Answer
/***** Initialization Tasks ******************************/
#include <stdio.h>

// Constants
#define CHARGE_PER_DAY 25.00
#define CHARGE_PER_KM 0.40

int main()
{
// Variables
int dayUsed;
int beginOdometer;
int endOdometer;
int kmUsed;
double rental;
Exercise 12.1-Answer (Continued)
/***** Main Body Tasks ***********************************/
// Inputs
printf("Enter number of days car is used : ");
scanf("%d", &dayUsed);

printf("Enter begin reading of odometer : ");


scanf("%d", &beginOdometer);

printf("Enter end reading of odometer : ");


scanf("%d", &endOdometer);

// Process
kmUsed = endOdometer - beginOdometer;
rental = (dayUsed * CHARGE_PER_DAY) + (kmUsed * CHARGE_PER_KM);

/***** End of Job Tasks **********************************/


// Output
printf("\n--- Rental Summary ---\n\n");
printf("Kilometers used : %d KM\n", kmUsed);
printf("Rental Charges : RM %.2f\n", rental);
return 0;
}
Exercise 12.2:
Write a program that calculates the weekly final salary of
a computer salesman. The program accepts 2 numbers
that represent hours worked and sales quantity for a
salesman on a particular week. The program finally
displays the basic salary, commission and final salary
on the screen.

Assume that 40 hours of work or less will be paid at


RM6.00 per hour, while every hour above 40 will be paid
at RM9.00 per hour.

In addition, if an employee sells more than 10 units of


computers, he/she will receive an extra RM5.50 per
computer after the 10th computer sold.
Exercise 12.2-Answer
/***** Initialization Tasks ******************************/
#include <stdio.h>

// Constants
#define NR_RATE 6.00 // Normal rate = RM6.00 per hour
#define OT_RATE 9.00 // Overtime rate = RM9.00 per hour
#define COMM_PER_UNIT 5.50 // Commission gain per extra unit

int main()
{ // Variables
int hoursWorked;
int unitsSold;
double basicSalary;
double commission;
double finalSalary;

/***** Main Body Tasks ***********************************/


// Inputs
printf("--- Enter Works Information ---\n\n");
printf("Enter number of hours worked : ");
scanf("%d", &hoursWorked);

printf("Enter units of computer sold : ");


scanf("%d", &unitsSold);
Exercise 12.2-Answer (Continued)
// Process
// 1. Calculate basic salary
if (hoursWorked <= 40)
basicSalary = hoursWorked * NR_RATE;
else
basicSalary = (40 * NR_RATE) + ((hoursWorked - 40) * OT_RATE);

// 2. Calculate commission
if (unitsSold > 10)
commission = (unitsSold - 10) * COMM_PER_UNIT;
else
commission = 0.00;

// 3. Calculate final salary


finalSalary = basicSalary + commission;

//***** End of Job Tasks **********************************/


// Output
printf("\n--- Weekly Salary Summary ---\n\n");
printf("Basic salary : RM %7.2f\n", basicSalary);
printf("Commission : RM %7.2f\n", commission);
printf("-------------------------\n");
printf("Final salary : RM %7.2f\n", finalSalary);
printf(" ==========\n");
return 0;
}
Modularization (appendix)

● Process of breaking a large program into


several smaller and manageable modules.
Calculate the
circumference and
area of a circle

Perform
Read input Display output
calculations
Modularization (appendix)
Reasons / advantages of modularization:

(a) Provide abstraction:


It is the process of paying attention to
important properties while ignoring
nonessential details.
Calculate the
circumference and
area of a circle

Perform
Read input Display output
calculations

Paying focus on the module on


hand first, meanwhile ignoring the
details of the rest of the modules.

Stephen
Modularization (appendix)
Reasons / advantages of modularization:

(b) Allow multiple programmers work together


on a program:
You can dissect any large task into modules,
and gain the ability to divide the tasks among
various people.
Calculate the
circumference and
area of a circle

Perform
Read input Display output
calculations

Stephen Tom Jerry

Each team member can work on a


different module at the same time.
Modularization (appendix)
Reasons / advantages of modularization:

(c) Allow you to reuse your work:


If a subroutine or function is useful and well-
written, you may want to reuse it more than
once within a program or in other programs.
Calculate the
circumference and
area of a circle

Perform
Read input Display output
calculations

reuse

An useful and well written


module can be reused in
New program
other programs.
Calculate the
circumference and
area of any shape
selected by user
Modularization (appendix)
Reasons / advantages of modularization:

(d) Makes it easier to identify structures:


When you combine several programming
tasks into modules, it may be easier for you
to identify the structures.
Figure 12.1: Figure 12.2:
Selection logic from a payroll Modularized logic from a payroll program
program (before modularization)

When the structures are Modularization helps to


complicated, the logic may make the structures clearer
be hard-to-understand. and easy-to-understand.

You might also like