Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > Knowledge Base > Tutors' Corner > Problem Solving (or "Where do I start"?)


Reply
 
Thread Tools Display Modes
  #1  
Old 06-28-2006, 02:01 AM
DougT's Avatar
DougT DougT is offline
Ultimate Antique

Administrator
* Expert *
 
Join Date: Sep 2005
Location: Maldon,Essex, UK
Posts: 3,939
Default Problem Solving (or "Where do I start"?)


The purpose of this Tutorial is to introduce the art of Problem Analysis which is simply the process that you use to work out the steps (logic) required to solve a programming problem.

It’s a fact that many people starting off in programming are given a task and the first cry is “Help - I don’t know where to start”. Hopefully, if you’re one of those people, then by the time you’ve read this you will know the answer.

There are many different ways of analysing problems and I’ll be going through the method I use, it’s not a formal method that you could read in a textbook but one that I have used over many years and it works for me. (It’s very, very loosely based on Jackson)

The process is best described using an example, for this tutorial I’ll take the problem as:

Write a Billing System for a Mobile Telephone company that processes information from a database on a monthly basis and calculates the total cost per Customer on the following basis:

Monthly Rental is 25.00. The call times are measured by hundredths of Seconds. Calls are charged by the Second. The first 100 minutes used are free (except for International calls which are charged at 0.50 per minute). After the customer has used 100 minutes, calls are charged at a rate of 0.15 per minute. The first 50 Text Messages are free and after the 50 have been sent they cost 0.10 per message.

The output required is the Period, Customer Details (Name, Address, Account Number, Mobile Number), Total number of International call minutes used and cost, Total number of other call minutes and cost, Total number of text messages sent and cost, total cost, VAT (Value Added Tax) and amount to pay.

The Database contains the following validated data in a series of Tables

Table Customer:

Customer Name
Customer Address
Customer Mobile Number
Customer Account Number

Table Calls:

Customer Account Number
Date and Time Call started
Date and Time Call ended
Type of Call (“I” for International, “N” for In Country)

Table Messages:

Customer Account Number
Date and Time Message Sent

Where to start?

It may seem a bit silly, but I start with the output required. What does the program have to produce? In our example it’s fairly straightforward,
  • The Period of the Charges
  • Some information about the Customer
  • The Total number of Call Minutes used and cost (International and Others)
  • The Total number of Text messages sent and cost
  • The Total cost
  • The VAT
  • The Grand Total the Customer has to pay
In order to create the output, we have to have some input, so the next thing to do is to look at that. The first thing to notice is that we have been given validated data in the Database. This means that it is not our responsibility to make sure that the data is valid – something else has already done that.

What Input do we need?
  • Customer Information – on the Database (Table Customer)
  • Call Information – on the Database (Table Calls)
  • Call type Information – on the Database (Table Calls)
  • Message Information – on the Database (Table Messages)
Most of the information we need is already on the Database, but in addition we need some input from the end user:
  • Date Range that represents the Period for charging
  • a rate for VAT
Now for the clever part:

What do we have to do to the input to create the output?

At this point, we look at the constraints (or ‘Rules’) that have to be applied. To the data (Including the very obvious)

We are interested in all customers on the Database

What do we have to do with this data? – Input it and Output it, nothing else.

We are only interested in data within the Period in question

We have to compare the Date and Time of the Calls to the Date& Time range the end user inputs and only process data within that range.

We have to round up the call times to the nearest second and report totals in Minutes

Each call is recorded to the nearest .01 of a second; we have to round it to the nearest second and divide by 60

We have to count the messages sent

When we read a message record we have to add one to a count and when all messages have been counted, multiply by a rate

We have to apply the appropriate Cost calculations to the call times / messages as defined in the requirements i.e.


Rental: Fixed at 25.00 per Month

Call Costs:
Non International =First 100 minutes Free, subsequent minutes cost 0,15 per minute

International = 0.50 per minute

Message Costs: First 50 are free, subsequent messages cost 0.10 each

In order to create the totals we will have to apply a formula to the data:

Total Cost of Calls = the sum of the Cost of each Call

The Cost of each Call = the duration of the Call, rounded up to the nearest second, Multiplied by the appropriate rate (Zero if the current total number of minutes is less than or equal to 50, otherwise 0.10)

The current total number of minutes = the sum of the minutes used so far.

Now, just stop and think for a second or two about the nature of the data and how things work in the real world. What if we get to the stage where the Customer has used 98 minutes so far and makes a call lasting 5 minutes? We will need to charge only for 3 minutes, as the first 2 minutes of the 5-minute call will be free.


So we have derived an additional constraint that our program will have to take care of..We will need to check if the minutes used so far plus the duration of this call is greater than 100 and if so, only charge for the difference between the two for that call.

Understanding the nature of the Data and considering all possible combinations of conditions that can happen is one of the keys to good analysis. We’ll see another example later.

Total Cost of Messages = the number of messages Multiplied by the appropriate rate (Zero if the current number of messages is less than or equal to 50, otherwise 0.10

We have to calculate the VAT

VAT = (Monthly Rental + Total Cost of Calls + Total Cost Of Messages) Multiplied by the Rate

We have to calculate the Total the Customer has to pay

Total = Total Cost of Calls + Total Cost of Messages + Monthly Rental + VAT
Continued in next Post.........
__________________
semel insanivimus omnes
S Data in context = Information, S Information in context = Knowledge, S Knowledge in context = Experience
S Experience in context = Wisdom= Data
Reply With Quote
  #2  
Old 06-28-2006, 02:03 AM
DougT's Avatar
DougT DougT is offline
Ultimate Antique

Administrator
* Expert *
 
Join Date: Sep 2005
Location: Maldon,Essex, UK
Posts: 3,939
Default Part 2 of 2

Phew, it’s a bit of a slog, but we’re nearly there.

Now, we’ve done some quite detailed analysis, and at this point we review what we’ve done and confirm (or otherwise) that we actually have enough information to solve the original problem. In our case, we don’t have any gaps in the data. We have all the information we need available and the end user inputs any information we don’t have on the Database.

However, we have a small problem with the constraints. We need to know what to do if the Customer starts the call in this month and finishes it in the next month. (e.g. the call starts at 23:55 on the last day of the period and ends at 00:05 on the first day of the next period. The definition of the problem doesn’t say what to do in that case. So we would go back and seek some advice as to how such conditions should be catered for. (This equally applies if you’ve set your own problem ask yourself the question!)

This is another important step, requiring some knowledge of the actual business and how the data is created. Assumptions must not be made, otherwise you can end up writing perfect code that does the wrong job. For the sake of our example, the answer is that we charge for all calls started in the month we are interested in irrespective of when they end.

Using this answer we now know how to select the data required, only calls that start in the month in question are required.

So, now
  • We know the output
  • We know the input
  • We know what we’ve got to do in order to change the input into the output. (The Processes involved)
Next we have to decide the order in which we do things.
Looking at our analysis and the overall problem we can identify some major processes:

Accept and validate the input from the end user (Date range and VAT rate)
As each Customer’s data is treated in the same way, A main loop that reads Customer Details and :
Processes Calls which
Processes International Calls
Processes Other Calls
Processes Messages
Calculates the VAT and the Grand Total
Outputs the Details
Continue until the data for all the Customers has been processed
That’s it. Our analysis is complete and all that’s left is to go through the design process and implement the solution in a programming language of our choice.

In the Design stage you would capitalise on the functionality offered by the programming language you selected but I would suggest as a minimum you would:
  • Define the User Interface (how the information is presented on the screen / output media)
  • Define the structure of the code (eg identifying Functions and Subroutines required by the Major Processes)
  • Design some test data which exercises all possible paths the program could go through

More on Design in another Tutoral to come later

To summarise the steps:
  1. Look at the Output elements required in detail. Identify what has to be output
  2. Look at the Input elements you have been given, identify where they come from
  3. Using the constraints (rules) you have been given identify what you have to do to the input to create the output, step by step
  4. Think about the nature of the data and derive any further constraints
  5. Determine from 1 to 4 above if you have all the information necessary to solve the problem. If you don’t, then go back and ask. This is important, you mustn’t make any assumptions, if you’re in the slightest doubt go and ask
  6. Decide the order in which things have to be done.
You may like to think about what you’d change if (as does very often happen, known as "Scope Creep") the problem setter came along and said, “by the way, I’d also like to see the details costs per call as well as the totals”
__________________
semel insanivimus omnes
S Data in context = Information, S Information in context = Knowledge, S Knowledge in context = Experience
S Experience in context = Wisdom= Data
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->