# Example Sequence Problem¶

The following is an example problem that has been solved using the six step computer based problem solving method mentioned above. The goal is to show you how a sequential program works and also to show how the six steps are used to develop a program from a given problem.

## Sequence Programming Example¶

Here is the problem: Write a program that will allow the user to enter a subtotal and the program will calculate the final total including tax.

1. Top-Down Chart

The first thing we need to find out is how to calculate tax in your particular province. If the program is to be used anywhere in Canada, it could get really confusing since each province has a different tax rate and some even calculate taxes differently than other provinces. To help simplify the problem, we are just going to do it for Ontario. The Harmonized Sale Tax (HST) in Ontario is currently 13%. The following is a top-down design breaking the problem up into smaller manageable pieces: 1. Flow Chart

The next step is to convert the top-down design into a flowchart. To help create the flow chart, use the bottom boxes of each of the arms, in order from left to right from the top-down design. This would mean Get subtotal, Calculate HST, Add 2 values together and Put final total. Remember that every flowchart has the “Start” oval at the top and the “Stop” oval at the bottom. This is so people know where to begin and end. The arrows are used so people can follow the flow. The words in the flow charts are not full sentences but simplified pieces of code. Ensure that you include any formulas and use the variable names that you will plan on using in your code. 1. Pseudo-code

Pseudo-code converts your flowchart into something that more resembles the final code you will write. Once again though it is not code (hence the name pseudo-code), so it is generically written so that it can be translated into any language. It should be understood by anyone that can write a computer program, not just people that use the same programming language that you do. The first word on each line should be a verb (an action word), since you want the computer to do something for you. By convention the first verb is also in all CAPS (capital letters). Here is the pseudo-code for the problem:

GET subtotal from user
CALCULATE tax ← subTotal * HST
CALCULATE total ← subTotal + tax
SHOW tax back to user
SHOW total back to user
1. Code

Once you have the pseudo-code done, the hardest part of solving the problem should be finished. Now you just convert your pseudo-code into the specific programming language you have chosen:

 ``` 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 26 27 28 29 30 31 32``` ```// Copyright (c) 2020 Mr. Coxall All rights reserved // // Created by: Mr. Coxall // Created on: Sep 2020 // This program calculates total from subtotal and tax #include #include int main() { // this function calculates total from subtotal and tax const float HST = 0.13; float tax; float subTotal; float total; //input printf("Enter the subtotal: \$"); scanf("%f", &subTotal); // process tax = + subTotal * HST; total = subTotal + tax; // output printf("\n"); printf("The HST is: \$%.2f.\n", tax); printf("The total cost is: \$%.2f.\n", total); printf("\nDone.\n"); 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 24 25 26 27 28 29 30 31 32 33 34 35``` ```// Copyright (c) 2020 St. Mother Teresa HS All rights reserved. // // Created by: Mr. Coxall // Created on: Sep 2020 // This program calculates total from subtotal and tax #include #include int main() { // this function calculates total from subtotal and tax const float HST = 0.13; float tax; float subTotal; float total; // input std::cout << "Enter the subtotal: \$"; std::cin >> subTotal; // process tax = + subTotal * HST; total = subTotal + tax; // output std::cout << "" << std::endl; std::cout << "The HST is: \$" << std::fixed << std::setprecision(2) << std::setfill('0') << tax << "." << std::endl; std::cout << "The total cost is: \$" << std::fixed << std::setprecision(2) << std::setfill('0') << total << "." << std::endl; std::cout << "\nDone. " << std::endl; } ```
 ``` 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 26 27 28 29 30 31 32 33 34 35 36``` ```/* Created by: Mr. Coxall * Created on: Sep 2020 * This program calculates total from subtotal and tax */ using System; /* * The Program class * Contains all methods for performing how local and global variables work */ class Program { public static void Main (string[] args) { // this function calculates total from subtotal and tax const float HST = 0.13f; float tax; float subTotal; float total; //input Console.Write("Enter the subtotal: \$"); subTotal = float.Parse(Console.ReadLine()); // process tax = + subTotal * HST; total = subTotal + tax; // output Console.WriteLine("\n"); Console.WriteLine("The HST is: \${0:0.00}.", tax); Console.WriteLine("The total cost is: \${0:0.00}.", total); Console.WriteLine ("\nDone."); } } ```
 ``` 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 26 27 28 29 30 31 32 33 34``` ```/** * Created by: Mr. Coxall * Created on: Sep 2020 * This program calculates total from subtotal and tax */ package main import ( "fmt" ) func main() { // this function calculates total from subtotal and tax const HST float64 = 0.13 var tax float64 var subTotal float64 var total float64 // get user input fmt.Print("Enter the subtotal: \$") fmt.Scanln(&subTotal) // process input tax = subTotal * HST total = subTotal + tax // output results fmt.Println() fmt.Printf("The HST is: \$%.2f.\n", tax) fmt.Printf("The total cost is: \$%.2f.\n", total) fmt.Println("\nDone.") } ```
 ``` 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48``` ```/* * This program shows how local and global variables work * * @author Mr Coxall * @version 1.0 * @since 2020-09-01 */ import java.util.Scanner; // Import the Scanner class final class Main { private Main() { // Prevent instantiation // Optional: throw an exception e.g. AssertionError // if this ever *is* called throw new IllegalStateException("Cannot be instantiated"); } /** * Main entry point into program. * * @param args nothing passed in */ public static void main(final String[] args) { // this function calculates total from subtotal and tax final float HST = 0.13f; float tax; float subTotal; float total; Scanner scanner = new Scanner(System.in); // input System.out.print("Enter the subtotal: \$"); subTotal = scanner.nextFloat(); // process tax = subTotal * HST; total = subTotal + tax; // output System.out.println(); System.out.printf("The HST is: \$%.2f.\n", tax); System.out.printf("The total cost is: \$%.2f.\n", total); System.out.println("\nDone."); } } ```
 ``` 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 26 27``` ```// Copyright (c) 2020 Mr. Coxall All rights reserved // // Created by: Mr. Coxall // Created on: Sep 2020 // This program calculates total from subtotal and tax const prompt = require('prompt-sync')() // this function calculates total from subtotal and tax const HST = 0.13 let tax let subTotal let total // get user input subTotal = parseFloat(prompt("Enter the subtotal: \$")) // process input tax = subTotal * HST total = subTotal + tax // output results console.log() console.log(`The HST is: \$\${tax.toFixed(2)}.`) console.log(`The total cost is: \$\${total.toFixed(2)}.`) console.log("\nDone.") ```

constants.py

 ```1 2 3 4 5 6 7 8``` ```#!/usr/bin/env python3 """ Created by: Mr. Coxall Created on: Sept 2020 This module contains constants for the tax program """ HST = 0.13 ```

main.py

 ``` 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 26 27 28 29 30``` ```#!/usr/bin/env python3 """ Created by: Mr. Coxall Created on: Sep 2020 This module calculates total from subtotal and tax """ import constants def main() -> None: """The main() function calculates total and tax, returns None.""" # input sub_total = float(input("Enter the subtotal: \$")) # process tax = sub_total * constants.HST total = sub_total + tax # output print("") print(f"The HST is \${tax:,.2f}.") print(f"The total cost is \${total:,.2f}.") print("\nDone.") if __name__ == "__main__": main() ```

### Example Output¶ 1. Debug

It is hard to show the debugging step, since I ensured that the program above worked correctly before I pasted it into the page. When programmers write code it is extremely unlikely that it will work right away the first time. This is why the development environment has tools to help the programmer fix simple mistakes. The two main kinds of mistakes are syntax errors and logical errors.

In modern languages high level languages and IDEs, syntax errors are usually easy to see and fix. A syntax error is a piece of code that the compiler or interpreter does not understand. It would be like speaking to you and one of the sentences did not make any sense to you. A modern IDE will nicely place a squiggly line under the code (or some other way of showing you) it does not understand, so that you can fix the problem. A logical error is a lot harder to find. This is a problem with the way you solved the problem. The code will still compile or be interpreted and run but the program will give you the wrong answer (or maybe just the wrong answer some times!). There is no easy way to solve these problems other than to step though your code one line at a time.

1. Document the code

This is hopefully not done just at the end of your programming but as you write your code. All the same it is good practice to go over you code at the end to ensure that someone else looking at it will understand what is going on. In the above example you can see that there is a comment at the start of the program and in the function as well. Also I have used a naming convention that is hopefully easy to understand what the variables are holding. In addition, the value of the HST is places in a constants, since they only change very infrequently.

The above six steps are an example of how you should go about solving a computer based problem. Ensure when you are given a problem, you do not make the mistake that most people do and go directly to the computer and start coding. If you have not first been able to break the problem down into smaller pieces and solve the problem on paper, going to the computer and starting to code will not help you. You will just end up going in circles, wasting time, creating bad code and getting nowhere. Programming is just problem solving on a computer but you have to have solved the problem before you actually get to the computer to help you get the answer.