Pages

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

No comments:

Stats