Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016
Algorithms’ Operations Algorithmscanbeconstructedby the following operations: Sequential Operation Each step is performed in the order in which it is written. Conditional Operation Each operation is a control operations that allow us to alter the normal sequential flow of control in an algorithm. Iterative Operation Allow the repetition of a block of statements according to a condition. Iteration is sometimes called looping.
Quiz 3 Question: Write a pseudo-code & draw flowchart that will calculate the sum for the first 100 odd integers.
Pseudo-codeFlowchart set count to 1 set value of sum to 0 while (count < 100) set sum to sum + count set count to count + 2 endwhile print value of sum Illustrated in the following slide Solution of Quiz 3
Solution of Quiz 3 (Cont…) Start Count =1 Sum=0 Sum=sum+count Count=count+2 Count<100 Print Sum End No Yes
Assignment III Write a pseudo-code and draw a flowchart for the following: 1. Calculate the salary of 100 workers in a factory.
Pseudo-codeFlowchart set value of count to 1 L: if (count > 100) then endwhile get values for name, wage, and hours set pay to wage * hours print value of name, pay set count to count + 1 goto L Illustrated in the following slide Assignments’ III Solution
Assignments’ III Solution (Cont…)
Task of Programming When you write programs, you write program statements that are instructions that are similar to English-language sentences. The statements you write in a programming language must be subsequently translated into machine language. Machine language is the language that computers can understand; it consists of 1s and 0s. A translator program (called either a compiler or an interpreter) checks your program for syntax errors. If there are no errors, the translator changes your written program statements into machine language. Therefore, syntax errors are not a big problem; you always have an opportunity to fix them.
Task of Programming (Cont…) A logical error occurs when you use a statement that, although syntactically correct, doesn’t do what you intended. Finding logical errors is much more time consuming for a programmer than finding syntax errors.
Programming Universals All programming languages provide methods for directing output and methods for sending input. All programming languages provide a way to name locations in computer memory. These locations are commonly called variables. Each variable have a data type which define the type of data to be store in this location. Example: int(0, 1, 2, 3), float(1.5, 2.3), char(a, b, c).
Cin Function
Cout Function
People and Programs User: an individual who runs, or executes, a program. Programmer: an individual who creates, or writes, a program.
Programming Language The selected programming language for this course is C++. C++ evolved from C. C++ designed by Bjarne Stroustrup at Bell Laboratories in early 1980s. C++ programs were not always portable from one compiler to another. In mid-1998, ANSI/ISO C++ language standards were approved.
General form of a C++ Program // Program description #include directives int main() { constant declarations variable declarations executable statements return 0; } Header for main function States… data type for the return value identifier for function list of arguments between parenthesis (none for this function) Braces enclose the body of the function They represent the start and end of the function Declarations and statements Main body of function (or main part) “//” represents the start of a comment All (almost) declarations and Statements end with a semi-colon “;”
Comments Comments appear in green in Visual C++. Comments are explanatory notes; they are ignored by the compiler. These remarks are useful to later program users because they might help explain the intent of a particular statement or the purpose of the entire program. There are two ways to include comments in a program: A line comment begins with two slashes (//) and continues to the end of the line on which it is placed. A block comment begins with a single slash and an asterisk (/*) and ends with an asterisk and a slash (*/); it might be contained on a single line or continued across many lines.
C++ Compiler Directives Compiler directives appear in blue in Visual C++. Header files are files that contain predefined values and routines, such as sqrt( ). In order for your C++ program to use these predefined routines, you must include a preprocessor directive, a statement that tells the compiler what to do before compiling the program. In C++, all preprocessor directives begin with a pound sign (#), which is also called an octothorp. The #include directive tells the compiler to include some already existing C++ code in your program.