Upload
maude-horn
View
213
Download
1
Embed Size (px)
Citation preview
Structured Programming (4 Credits)
HNDIT11034
Week 2 – Learning OutcomesDesign an algorithmic solution for
simple problem such as computation of a factorial, total of a series etc. using pseudo codes
Identify Basic Structure of a C++ program
Use comments in a programFamiliar with the escape
sequence characters
More flow chart examples
75 - 100 “A”65 - 74 “B”55 - 64 “C”45 - 54 “S”0 - 44 “F”
Draw a flow chart to input Average Marks and find the grade according to the grade System given below.
Input Avg If Avg>=75
If Avg>=65
If Avg>=55
If Avg>=45
Stop
Start Display A
Display B
Display C
Display S
Yes
Yes
Yes
Yes
No
No
No
No
Display F
Pseudo Code
One of the textual representation of algorithm is Pseudo code.
Begin
Pseudo Code for adding two numbers
End.
Input first number and second number
Total = first Number + Second Number
output Total
Begin Input Maths Marks ,Science Marks Total = Maths + Science Average = Total / 2 If Average >= 60 then Grade= “PASS” Else Grade= “FAIL” End if Display Total , Average , GradeEnd
Write a pseudo code to take maths and science marks , calculate total, average and display grade.(If average >=60, “Pass” otherwise “Fail”.
75 - 100 “A”65 - 74 “B”55 - 64 “C”45 - 54 “S”0 - 44 “F”
Write a pseudo code to input Average Marks and find the grade according to the grade System given below.
BeginInput Avg Mks
If Avg >=75 then Grade = “A” Else If Avg >=65 then Grade =“ B” Else
If Avg >=55 then Grade = “C” Else If Avg >= 45 then Grade =“ S”
Else Grade = “W”
End if End if
End ifEnd if
Display GradeEnd
BeginNumber=1Do While Number <=10Print NumberNumber=Number + 1End while
End
Write a Pseudo code to display numbers from 1 to 10
A C++ program //include headers; these are modules that include functions that
you may use in your program; we will almost always need to include the header that defines cin and cout; the header is called iostream.h
#include <iostream.h>
void main() {
//variable declaration
//read values input from user
//computation and print output to user
}
After you write a C++ program you compile it; that is, you run a program called compiler that checks whether the program follows the C++ syntax ◦ if it finds errors, it lists them
◦ If there are no errors, it translates the C++ program into a program in machine language which you can execute
When learning a new language, the first program people usually write is one that salutes the world
Here is the Hello world program in C++.
#include <iostream.h>void main() {
cout << “Hello world!”;}
The Standard output Statementcout << “Hello World”;The output operator, <<, is used
to direct a value to the standard output device.
The statement:cout << “I am “ << var << “ years old.”;Can also be written as:
cout << :”I am “;cout << var;cout << “years old.”;
Or as:cout << “ I am “
<< var << “years old.”var is a variable. When we want to display
the content of a variable it should not written inside quotes.
Comment Entries
You can place comment entries to provide clarity to the code. Comment entries are ignored by the compiler and are meant to describe the code. Comments can be written between /* and */, or can be preceded by a //.
ESCAPE SEQUENCESESCAPE SEQUENCE
NAME DESCRIPTION
\a Bell (alert) Make a sound from computer
\b Backspace Takes the cursor back
\t Horizontal tab Takes the cursor to the next tab position
\n New line Takes the cursor to tne beginning of the next line
\r Carriage return
Cause a carriage return
\” Double quotes Displays a quotation mark
\\ Backslash Displays a back slash
\? Question mark Displays a question mark
Variable TypesA variable type is a description of the kind
of information a variable will store. Programming languages vary regarding how strict they require you to be when declaring a variable's type. Some languages, like Perl, python ,do not require you to announce the type o fa variable. Other languages require you to declare some variables as numbers and others as text-strings, for example. C++, a strongly typed language, requires you to be even more specific than that. Instead of declaring a variable as a number, you must say whether it will store integers or decimals
Rules for Naming Identifiers in C++
The name of an identifier needs to be without any embedded space or symbols such as ? ! - + @ # & () [] {}. However, underscores can be used wherever a space is required.
Identifier names must be unique.An identifier name can have any number of
characters.An identifier name must begin with a letter or an
underscore (‘-‘), which may be followed by a sequence of letters, digits (0-9), or ‘-‘. The first character in an identifier name cannot be a digit. Starting an identifier with an underscore is not recommended.
Keywords cannot be used as identifiers. Example : main , include, int, float etc.
Q & A