Pages

Showing posts with label tutorial. Show all posts
Showing posts with label tutorial. Show all posts

Saturday, December 3, 2011

Basic Problem Solving Using Computers 1





By: razi.smartcomputing123@gmail.com



Pre-requisite

You are expected to know the following topics before moving to the next slides. Click the links to learn more about them.

What is Computer Programming? (learn more)

What is ideone.com compiler? (learn more)

How to write the basic program structure? (learn more)



Introduction

This presentation demonstrates the concept of problem-solving using computer machine.

All codes can be tested on ideone.com compiler.



What are the advantages of using computer to solve problem?

Computer processing is fast,

millions instructions executed per second

Computer is a reliable processing agent;

it follows instructions exactly as they are.

Computer is consistent;

no such thing as tiredness,

Computer has efficient memory;

huge data collection can be kept in relatively small physical space.



Types of problem that can be solved through programming

Problem can be solved through programming if it can be expressed either as

Arithmetic form, or

Logic form



To perform computer processing we need…

Algorithm

Steps to perform processing

Data Structures

To hold the values prior to, during and post processing

Control Structures

To control program statement execution

These 3 elements are required for computer processing



Algorithm



Narrative Format (Pseudo-Code)

BEGIN

READ integer1, integer2

LET integer3=integer1 + integer2

PRINT integer3

END




Visual Format (Flow Chart)

BEGIN

READ integer1, integer2

LET integer3=integer1 + integer2

PRINT integer3

END

Data Structure

Describe the structure that contains the data value.

Must be designed correctly to achieve efficiency.

Basic data types:

Numbers; integer or decimal point

Text; a character or strings (group of characters)

Compound data types:

Array






Control Structures

Later, we shall see some variations of control structures to add programming flexibility



Problem 1 : Finding area of dimensions

The following table provides a mathematical formula to calculate area for various shapes. Write a program to implement this.






Shape

Formula

Input Values

Rectangle

length x width

Length=5, width=6

Triangle

½ x length x width

Length=3, width=4

Circle

PI x r2

R=2.4

Solution 1a. Area of rectangle

C++

#include «iostream»

using namespace std;

int main() {

int length, width,rectangle;

length=5;

width=6;

rectangle=length * width;

cout«« "area= "««rectangle;

return 0;

}

VB.net

Imports System

Public Class Test

Public Shared Sub Main()

Dim length,width,rectangle as integer

length=5

width=6

rectangle=length * width

Console.WriteLine("area= " & rectangle)

End Sub

End Class



Line 4 declares integer variables. Line 5,6,7 sets the value of the variables. Line 8 outputs the result.

Solution 1b. Area of triangle

C++

#include «iostream»

using namespace std;

int main() {

double length, width, triangle;

length=5;

width=6;

triangle=0.5 * length * width;

cout«« "area= "««triangle;

return 0;

}

VB.net

Imports System

Public Class Test

Public Shared Sub Main()

Dim length,width,triangle as double

length=5

width=6

triangle=0.5*length * width

Console.WriteLine("area= " & triangle)

End Sub

End Class



Line 4 declares integer variables. Line 5,6,7 sets the value of the variables. Line 8 outputs the result.

Solution 1c. Area of circle

C++

#include «iostream»

using namespace std;

int main() {

double PI, radius;

PI=3.14;

radius=2.4;

circle=PI * radius * radius;

cout«« "area= " ««circle;

return 0;

}

VB.net

Imports System

Public Class Test

Public Shared Sub Main()

Dim PI, radius as double

PI=3.14

radius=2.4

circle=PI * radius * radius

Console.WriteLine("area= " & circle)

End Sub

End Class



Line 4 declares integer variables. Line 5,6,7 sets the value of the variables. Line 8 outputs the result.

Problem 2 : Assigning Grades

Student marks range from 0 to 100

Their marks correspond to the grades based on the table below.

Write a program to assign grades to the input marks value.






Min Value

Max Value

Grade

0

39

F

40

59

C

60

79

B

80

100

A

Solution 2. Grades

C++ (http://ideone.com/mO3Lr)

#include «iostream»

using namespace std;

int main() {

double marks;

char grade;

cin»» marks;

if ((marks»=0) && (marks«=39)) {grade='F';}

if ((marks»=40) && (marks«=59)) {grade='C';}

if ((marks»=60) && (marks«=79)) {grade='B';}

if ((marks»=80) && (marks«=100)) {grade='B';}

cout««grade;

return 0;

}

VB.net (http://ideone.com/RirLS)

Imports System

Public Class Test

Public Shared Sub Main()

Dim marks as double

Dim grade as string

marks=val(console.ReadLine())

if ((marks»=0) and (marks«=39)) then grade="F"

if ((marks»=40) and (marks«=59)) then grade="C"

if ((marks»=60) and (marks«=79)) then grade="B"

if ((marks»=80) and (marks«=100)) then grade="B"

Console.WriteLine(grade)

End Sub

End Class



Line 4,5 declares integer variables. Line 6 gets the input value. Line 7,8,9,10 selectively sets the value of the variables. Line 8 outputs the result.

Problem 3 : Login Module

You are required to write codes for login process.

The input_pin value will be compared against the pin value (hardcoded as 1234)

Login trials is limited to 3 times only.

Use WHILE DO(…) LOOP to implement the solution.






Solution 3. Login Module

C++ (http://ideone.com/0CuYI)

#include «iostream»

using namespace std;

int main() {

int pin, input_pin;

int trial,logged;

trial=0;

logged=0;

pin=1234;

while ((trial«3) && (logged==0)) {

cin»»input_pin;

if (input_pin==pin) {

logged=1;}

else {

logged=0;

}

cout««"Trial:"««trial««",Logged:"««logged««endl;

trial++;

}

return 0;

}

VB.net (http://ideone.com/GsXo4)

Imports System

Public Class Test

Public Shared Sub Main()

Dim pin, input_pin as integer

Dim trial, logged as integer

trial=0

logged=0

pin=1234

While (trial«3) and (logged=0)

input_pin=val(console.ReadLine())

If (input_pin=pin) Then

logged=1

Else

logged=0

End If

Console.WriteLine("Trial:" & trial & ",Logged:" & logged)

trial +=1

End While

End Sub

End Class



Line 4,5 declares integer variables. Line 6,7,8 sets initial value for variables. Line 10 gets the input value. Line 11-15 evaluates the input and sets the logged value. Line 9-10 contains the WHILE () DO ...LOOP

Next topics:

More Control Structures

Friday, December 2, 2011

Writing basic program structure using ideone.com compiler





By: razi.smartcomputing123@gmail.com



Pre-requisite

You are expected to know the following topics before moving to the next slides. Click the links to learn more about them.

What is Computer Programming? (learn more)

What is ideone.com compiler? (learn more)



Introduction

This presentation demonstrates the application of basic control structure using C++ and VB.net language.

All codes can be tested on ideone.com compiler.



3 Basic Control Structures



Source Code Templates

C++

#include «iostream»

using namespace std;

int main() {

return 0;

}

VB.net

Imports System

Public Class Test

Public Shared Sub Main()

End Sub

End Class



The followings are the source code templates that is provided by ideone.com

Use this template as a starting point for you to do the programming exercises.

Sequential Control Structure

C++

#include «iostream»

using namespace std;

int main() {

cout««"Hello World"««endl;

cout««"........... "««endl;

return 0;

}






Sequential Control Structure is the default control structure, i.e computer execute statements one after another in top-down direction unless told otherwise.

The following codes display “Hello World” on the first line and dots on the second line.

Sequential Control Structure

Visual Basic .NET

Imports System

Public Class Test

Public Shared Sub Main()

Console.WriteLine("Hello World" & vbCrLf)

Console.WriteLine("..........." & vbCrLf)

Console.ReadLine() 'Wait for key to be pressed

End Sub

End Sub






Sequential Control Structure is the default control structure, i.e computer execute statements one after another in top-down direction unless told otherwise.

The following codes display “Hello World” on the first line and dots on the second line.

Sequential Control Structure & Variable Declaration

C++

#include «iostream»

using namespace std;

int main() {

int num1;

int num2;

int num3;

num1=1;

num2=2;

num3=num1+num2;

cout««"The output is "««num3««endl;

return 0;

}






Line 4,5,6 declare integer variables. Line 7,8,9 assign values to them. Line 10 outputs the value.

Sequential Control Structure & Variable Declaration

Visual Basic .NET

Imports System

Public Class Test

Public Shared Sub Main()

Dim num1 as integer

Dim num2 as integer

Dim num3 as integer

num1=1

num2=2

num3=num1+num2

Console.WriteLine("The output is " & num3 & vbCrLf)

End Sub

End Class






Line 4,5,6 declare integer variables. Line 7,8,9 assign values to them. Line 10 outputs the value.

Selection Control Structure

C++

#include «iostream»

using namespace std;

int main() {

int iMark;

iMark=50;

if (iMark»=40) {

cout««"Pass!"««endl;}

else {

cout««"Failed!"««endl;}

return 0;

}



Line 5 assigns the value for iMark. Line 6 evaluate whether the value is greater than or equals to 40. If TRUE, PRINT “Pass!”. Otherwise, PRINT “Failed!”

Selection Control Structure

Visual Basic .NET

Imports System

Public Class Test

Public Shared Sub Main()

Dim iMark as integer

iMark=50

if (iMark»=40) then

Console.WriteLine("Pass!" & vbCrLf)

else

Console.WriteLine("Failed!" & vbCrLf)

end if

End Sub

End Class



Line 5 assigns the value for iMark. Line 6 evaluate whether the value is greater than or equals to 40. If TRUE, PRINT “Pass!”. Otherwise, PRINT “Failed!”

Loop Control Structure

C++

#include «iostream»

using namespace std;

int main() {

int iMark;

iMark=30;

while (iMark«40) {

cout««iMark««endl;

iMark++;

}

return 0;

}



Line 5 assigns the value for iMark. Line 6 evaluate whether the value is less than 40. If it is TRUE, run a loop and add 1 to iMark in each loop. Line 8 uses increment operator ++

Loop Control Structure

Visual Basic .NET

Imports System

Public Class Test

Public Shared Sub Main()

Dim iMark as integer

iMark=30

do while (iMark«40)

Console.WriteLine(iMark & vbCrLf)

iMark+=1

loop

End Sub

End Class



Line 5 assigns the value for iMark. Line 6 evaluate whether the value is less than 40. If it is TRUE, run a loop and add 1 to iMark in each loop. Line 8 use increment operator +=

Summary

We have learned:

Console window output via ideone.com

3 basic control structures:

Sequential

Selection [IF … ELSE …]

Loop [WHILE (…) DO …]

Increment operator

Newline character



Next topics:

SWITCH SELECTION

DO (…) WHILE (…) LOOP

FOR LOOP

Monday, October 26, 2009

E-Commerce Introduction Tutorials

bp_turban_ec_2004Overview of Electronic CommerceEssay.mhtbp_turban_ec_2004Overview of Electronic CommerceEssay.mht
12K
bp_turban_ec_2004Overview of Electronic CommerceMultiple Choice.mhtbp_turban_ec_2004Overview of Electronic CommerceMultiple Choice.mht
36K
bp_turban_ec_2004Overview of Electronic CommerceTrue or False.mhtbp_turban_ec_2004Overview of Electronic CommerceTrue or False.mht
24K
bp_turban_ec_2004E-Marketplaces Structure, Mechanisms, EEssay.mhtbp_turban_ec_2004E-Marketplaces Structure, Mechanisms, EEssay.mht
12K
bp_turban_ec_2004E-Marketplaces Structure, Mechanisms, EMultiple Choice.mhtbp_turban_ec_2004E-Marketplaces Structure, Mechanisms, EMultiple Choice.mht
36K
bp_turban_ec_2004E-Marketplaces Structure, Mechanisms, ETrue or False.mhtbp_turban_ec_2004E-Marketplaces Structure, Mechanisms, ETrue or False.mht
24K

Sunday, October 26, 2008

Intro to MS Access DAO and ADO


Objectives
At the end of this section you should understand the terms
  • RecordSource
  • Recordset
  • RecordsetClone
and how you can work with the underlying data of a form (read only) using the RecordsetClone and the use of the debug window.

Reading

In the Access 2003 help file read the entries for
  • RecordsetClone
  • RecordSource
  • Recordset (DAO)
  • Bookmark
  • Me object
  • FindFirst, FindNext, FindLast, FindPrevious methods
Balter
  • Balter pages 684 - 700 DAO Recordset properties and methods, the RecordsetClone property and Bookmarks, 721 - 724 using the immediate window
  • (for Access 2000 Balter pages 468-482 DAO Recordset properties and methods, the RecordsetClone property, Bookmarks page 501-2 immediate window)

Introduction - DAO and ADO

Background Reading (for interest only)

Balter pages 672 - 684
( Access 2000 Balter pages 430 - 468 Jennings Chapter 27, pages 1032 - 1082 )

Commentary

Microsoft currently provide two different ways of accessing data (strictly interface definitions), Data Access Objects (DAO) and ActiveX Data Objects (ADO). DAO was the only model in Visual Basic up to VB5 and in Access up to Access 97. With VB6 and from Access 2000, Microsoft have made both DAO and ADO available.

Microsoft have also made it clear that their intention is to move to ADO exclusively in the long term. Certain important products/environments such as ASP scripting using VB Script only support ADO and anyone planning to use Microsoft products long term should learn the use of ADO.

As stated below, a database created in Access 2003 uses DAO by default as does a database created in Access 97 but Access2000 uses ADO. So the default in the databases we have used so far, which were converted from Access 97.

For this reason, and because it is a bit simpler, the rest of this module will use DAO objects and methods to be consistent with the methods on the default objects created by Access.

Very Important - References

Access 2003 is set up so that when you create a database it expects you to use DAO to access data. You can see this as follows.

If you set up a new database in Access 2003 and then open the VBA IDE (either from a Form or a new Module) and then choose Tools, References from the Toolbar, you will get a window showing the places VBA will look for definitions of language methods etc. What you will see is

Native Access 2003 references - Data Access Objects (DAO) compliant but ActiveX Data Objects (ADO) also referenced 

Access2003 references - DAO

Native Access 2000 references - ActiveX Data Objects (ADO) compliant

 

References present in databases converted from Access 97 to Access 2000 format

If you create a database in Access 97 and then convert it to Access 2000 format and then look at the references here you will see

Access 97 created references

Both the references selected and the order in which they appear are important. The ones selected will be the only ones searched and they will be searched in the order given, so you see an Access 2000 created database will only look for ADO data manipulation, while an Access 97 database converted to Access 2000 will only look for DAO! If you add extra references and these lead to duplicate definitions then the first in the list will be used, so, if you had ADO 2.1 and DAO 3.6 in that order, since both contain definitions of the OpenDatabase method for Database objects, Access 2000 would use the ADO definition even though the DAO one was there. So it is no good just adding the DAO 3.6 to the end of the list!

Note that an Access2003 created database has both the DAO and ADO libraries selected (very dangerous) which means that DAO classes will be accessed first but, if you use a class which is defined for ADO only, that class will be used. Thus, if you think you are programming using ADO then any classes which are also defined for DAO will be created as DAO objects, while classes which are unique to ADO will be created as ADO objects, leading to impenetrable bugs.

Probably it is the most sensible to deselect (untick) the reference to the data access classes you don't intend to use.

The PROJCONT (and other) databases supplied with these notes originate from Access 97 databases and so have the DAO references and will execute the DAO code.

Very Important  If you create a database in Access 2000 and you want to write DAO code in it, you must change the references from the native Access 2000 list to the native Access 97 list as shown above - otherwise you will get a series of inexplicable and meaningless error messages as Access tries to execute the commands in their ADO form! 

If you create a database in Access2003 and intend to write data access VBA, you should unselect the library for the data access objects you don't intend to use.

File format

Microsoft has implemented (at least) 3 different file formats with different compatibilities


Access 97 can develop
Access2000 can develop
Access2003 can develop
Access97 format
Yes
Can convert (both ways) but not develop
Can convert (both ways) but not develop
Access2000 format
No
Yes
Yes (can also create)
Access2002-3 format
No
No
Yes

In this module we'll try to stick to Access2000 format files compatible with both Access200 and Access2003.

Discussion

Underlying any data bound form there is a table or query which supplies the data displayed on the form. In VBA or Visual Basic terminology, this virtual table is called a Recordset. Recordsets can be free standing or, as in the case of Access forms (and Reports) properties of other objects. The contents of the Recordset are determined by the RecordSource property and you can change the Recordset by changing the Recordsource (or, as we will see later, by applying a Filter expression). You can work with a read-only copy of a Form's Recordset by referencing it's RecordsetClone property and you can perform actions (such as repositioning) on the RecordsetClone without changing the current position on the underlying data which determines what is being displayed.

If you want to match the position of the displayed data with that in the RecordsetClone, you can do this with the Bookmark properties of the two Recordsets by assigning the value of the Bookmark of the clone to Me.Bookmark (implicitly the Bookmark property of the recordset underlying the form).

The following is an extract from the Help entry for RecordsetClone. 


You can use the RecordsetClone property to refer to a form's Recordset object specified by the form's RecordSource property.

Setting

The RecordsetClone property setting is a copy of the underlying query or table specified by the form's RecordSource property. If a form is based on a query, for example, referring to the RecordsetClone property is the equivalent of cloning a Recordset object by using the same query. If you then apply a filter to the form, the Recordset object reflects the filtering. 
This property is available only by using Visual Basic and is read-only in all views. 


Practical Example

In this practical example you will see how to move through  the RecordsetClone and how to synchronise the underlying data with the operation being shown by using the Debug window.
  • Create a form based on the CUSTOMER table using the Form Wizard and call it frmCUSTOMERrecordsetclone
  • Put two buttons on the form and type into them the code below for the click events (here for Command10_Click and Command11_Click)
  • View the form and type G to show the immediate window - NB in Access 2003 the VBA IDE is a separate window and, so, the Immediate Window opens within this, not within the Access window (unlike Access 97), so you will have to swap between them to see what is going on.
  • Click each of the buttons. You will see with the first button the table data is displayed in the debug window but the data displayed is not changed while the second button displays the last row in the table after looping through the table

Option Compare Database 
Option Explicit

Private Sub Command10_Click()

With Me.RecordsetClone 
.MoveFirst 
Me.Bookmark = .Bookmark 
Do While Not .EOF 
    Debug.Print !Custno, !Customer 
    .MoveNext 
Loop 
End With

End Sub

Private Sub Command11_Click()

With Me.RecordsetClone 
.MoveFirst 
Me.Bookmark = .Bookmark

Do While Not .EOF 
    Debug.Print !Custno, !Customer 
    .MoveNext 
    If Not .EOF Then 
        Me.Bookmark = .Bookmark 
        End If 
Loop 
End With

End Sub


Commentary on Code

Command10_Click()
  • The With block sets up all the references (preceded with a .) inside the block refer to Me.RecordsetClone
  • .MoveFirst positions in the RecordsetClone at the first row
  • Me.Bookmark = .Bookmark synchronises the recordset underlying the data with the position in the RecordsetClone (ie at the first row)
  • The Do While loop executes as long as the program is not at the end of the RecordsetClone recordset (after the last row)
  • The Debug.Print prints each Customer Number and Customer name to the debug window
  • .MoveNext moves to the next row in RecordsetClone
Command11_Click()
  • Does exactly the same but synchronises the underlying data with the RecordsetClone each time through the loop, except when the RecordsetClone has gone past the last row, thus leaving the underlying data displaying the last row at the end of the loop
The form and debug immediate window will look like 

displayed formimmediate window






Why You Should Use Recordsets and Object Orientation in VBA Instead of Arrays


When I starting working with VBA 4 years ago arrays were my best friends, they’re easy to use, easy to understand, and getting data to / from an Excel sheet is a piece of cake as cell (0, 1) can map nicely to array position 0, 1. However I’ve developed the opinion that arrays should be avoided in favour of other forms of data lists / collections.

What’s Wrong With Arrays
It’s not that arrays are particularly wrong, it’s more that in comparison to the alternatives they’re limited and frustrating to use and support;

·         Size declaration – You have to declare how big you’re array is going to be, even if you don’t know yet. This is a key limitation regardless of the fact you can ‘re-declare’ to increase the size.

·         No in-built sorting / filters / search functions. An array is an array, it doesn’t have any methods associated with it. It doesn’t even have it’s only object type.

·         It can only hold strings, numeric values, characters etc.

·         Moving through an array is arduous with large amounts of data and there’s no standard ‘structure’ an array must take (I can declare a 0 based array, a co-worker may declare a 1 based array, and there’s no standard slot for each piece of information etc)

 
So What are the Alternatives?
 
ADODB.RECORDSETS
Instead of using arrays to store string and numerical values I’ve recently been using ADODB.recordsets (recordsets).

Recordsets are slightly more object oriented than arrays, as in essence they are collections of record objects, although they can be manipulated without even using or referring to the record object.

Benefits of Recordsets:
·         No size declaration, you can type myRecordset.AddNew to create a new record and populate it with values.

·         You define fields to specify what data is where, instead of having to remember which position in the array the address is you can just move to a record and use myRecordset.Fields(“myField”).value to retrieve it.

·         You also don’t have to iterate through a recordset to output the contents to a worksheet:

‘ Move to the beginning of the recordset

myRecordset.MoveFirst
‘output the contents to a worksheet starting at cell A1

someWorksheet.Range(“A1”).value = myRecordset.GetRows()

·         They have inbuilt search functions

·         They’re easier to iterate over if you need to, or you can do bulk updates

·         The list goes on....

There are of course limitations to recordsets, they’re frustrating to work out at times, and can be counter intuitive, but I’ve found the benefits of having a more structured set of data are fantastic.

Add Microsoft ActiveX Data Objects Library 2.8 to your VBA references and have a play.

OBJECTS AND COLLECTIONS
If you’ve declared a customer object and given that customer properties such as FirstName, LastName and CustomerID there’s no confusion on how to find that information.

If you combine that benefit with the use of the Collection object you make your code so much easier to understand, use and support.

Collections and object lists figure more in strongly typed object oriented languages such as C# than in VBA, but we can still take advantage of VBA’s generic collection object.

Dim c as New Collection
The more you object orient your code in VBA the more you’ll use collections, and the easier your life will become, you won’t have to pass around huge arrays and remember between procedures which attribute is where, as you’ll have strongly typed objects to work from and the re-use and quality of your code will vastly improve.

Overall object orientation is the best path to take for storing data, however, even the use of recordsets can drastically improve the speed, reliability, supportability and expandability of your code.


Stats