Showing posts with label cpp. Show all posts
Showing posts with label cpp. 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
Subscribe to:
Posts (Atom)