www hndit com HNDIT 11034 Structured Programming 4

  • Slides: 19
Download presentation
www. hndit. com HNDIT 11034 Structured Programming (4 Credits)

www. hndit. com HNDIT 11034 Structured Programming (4 Credits)

www. hndit. com Week 2 – Learning Outcomes �Design an algorithmic solution for simple

www. hndit. com Week 2 – Learning Outcomes �Design 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 program �Familiar with the escape sequence characters

www. hndit. com More flow chart examples Draw a flow chart to input Average

www. hndit. com More flow chart examples Draw a flow chart to input Average Marks and find the grade according to the grade System given below. 75 - 100 65 - 74 55 - 64 45 - 54 0 - 44 “A” “B” “C” “S” “F”

www. hndit. com Start Input Avg If Avg>=75 Yes Display A No If Avg>=65

www. hndit. com Start Input Avg If Avg>=75 Yes Display A No If Avg>=65 Yes Display B No If Avg>=55 Yes Display C No If Avg>=45 No Display F Stop Yes Display S

Pseudo Code �One www. hndit. com of the textual representation of algorithm is Pseudo

Pseudo Code �One www. hndit. com of the textual representation of algorithm is Pseudo code.

Pseudo Code for adding two numbers Begin Input first number and second number Total

Pseudo Code for adding two numbers Begin Input first number and second number Total = first Number + Second Number output Total End. www. hndit. com

www. hndit. com Write a pseudo code to take maths and science marks ,

www. hndit. com Write a pseudo code to take maths and science marks , calculate total, average and display grade. (If average >=60, “Pass” otherwise “Fail”. 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 , Grade End

www. hndit. com Write a pseudo code to input Average Marks and find the

www. hndit. com Write a pseudo code to input Average Marks and find the grade according to the grade System given below. 75 - 100 65 - 74 55 - 64 45 - 54 0 - 44 “A” “B” “C” “S” “F”

www. hndit. com Begin Input Avg Mks If Avg >=75 then Grade = “A”

www. hndit. com Begin Input 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 Display Grade End

www. hndit. com Write a Pseudo code to display numbers from Begin Number=1 Do

www. hndit. com Write a Pseudo code to display numbers from Begin Number=1 Do While Number <=10 Print Number=Number + 1 End while End

A C++ program www. hndit. com //include headers; these are modules that include functions

A C++ program www. hndit. com //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

www. hndit. com When learning a new language, the first program people usually write

www. hndit. com 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!”; }

www. hndit. com The Standard output Statement �cout << “Hello World”; �The output operator,

www. hndit. com The Standard output Statement �cout << “Hello World”; �The output operator, <<, is used to direct a value to the standard output device.

www. hndit. com The statement: cout << “I am “ << var << “

www. hndit. com 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 www. hndit. com can place comment entries to provide clarity to

Comment Entries �You www. hndit. com 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 SEQUENCES www. hndit. com ESCAPE SEQUENCE NAME DESCRIPTION a Bell (alert) Make a

ESCAPE SEQUENCES www. hndit. com ESCAPE 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 ? Question mark Displays a question mark Displays a back slash

Variable Types �A www. hndit. com variable type is a description of the kind

Variable Types �A www. hndit. com 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

www. hndit. com Rules for Naming Identifiers in C++ � The name of an

www. hndit. com 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 www. hndit. com

Q&A www. hndit. com