Chapter One Introduction to Programming



Similar documents
Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

PROG0101 Fundamentals of Programming PROG0101 FUNDAMENTALS OF PROGRAMMING. Chapter 3 Algorithms

Sources: On the Web: Slides will be available on:

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

Chapter 2: Elements of Java

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

C++ Language Tutorial

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Informatica e Sistemi in Tempo Reale

ALGORITHMS AND FLOWCHARTS

Appendix K Introduction to Microsoft Visual C++ 6.0

Introduction to Visual C++.NET Programming. Using.NET Environment

JavaScript: Control Statements I

Java Basics: Data Types, Variables, and Loops

Algorithms, Flowcharts & Program Design. ComPro

QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall

Computer Science 281 Binary and Hexadecimal Review

Lecture 1 Introduction to Java

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Algorithm & Flowchart & Pseudo code. Staff Incharge: S.Sasirekha

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Flowchart Techniques

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 3: Input/Output

ADVANCED SCHOOL OF SYSTEMS AND DATA STUDIES (ASSDAS) PROGRAM: CTech in Computer Science

Moving from CS 61A Scheme to CS 61B Java

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Chapter 5. Selection 5-1

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

Sequential Program Execution

Application. Outline. 3-1 Polynomial Functions 3-2 Finding Rational Zeros of. Polynomial. 3-3 Approximating Real Zeros of.

2 SYSTEM DESCRIPTION TECHNIQUES

Computer Programming Lecturer: Dr. Laith Abdullah Mohammed

PIC 10A. Lecture 7: Graphics II and intro to the if statement

The While Loop. Objectives. Textbook. WHILE Loops

Copy in your notebook: Add an example of each term with the symbols used in algebra 2 if there are any.

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Answers to Review Questions Chapter 7

Variables, Constants, and Data Types

Comp151. Definitions & Declarations

13 Classes & Objects with Constructors/Destructors

Visual Logic Instructions and Assignments

1.6 The Order of Operations

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Computer Programming C++ Classes and Objects 15 th Lecture

Lecture 2 Notes: Flow of Control

Passing 1D arrays to functions.

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

Programming Languages & Tools

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Basics of I/O Streams and File I/O

(!' ) "' # "*# "!(!' +,

POLYNOMIAL FUNCTIONS

The Method of Partial Fractions Math 121 Calculus II Spring 2015

Notes on Algorithms, Pseudocode, and Flowcharts

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

2) Write in detail the issues in the design of code generator.

This unit will lay the groundwork for later units where the students will extend this knowledge to quadratic and exponential functions.

C++ INTERVIEW QUESTIONS

ALGORITHMS AND FLOWCHARTS. By Miss Reham Tufail

arrays C Programming Language - Arrays

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Chapter 3. Input and output. 3.1 The System class

Introduction to Python

Appendix M: Introduction to Microsoft Visual C Express Edition

Pseudo code Tutorial and Exercises Teacher s Version

Fundamentals of Programming

VHDL Test Bench Tutorial

SYSTEMS OF EQUATIONS AND MATRICES WITH THE TI-89. by Joseph Collison

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Numeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals:

Regular Expressions and Automata using Haskell

Computer Programming I & II*

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Clock Arithmetic and Modular Systems Clock Arithmetic The introduction to Chapter 4 described a mathematical system

While Loop. 6. Iteration

C++ Input/Output: Streams

Computer Science 217

How To Write Portable Programs In C

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

Math Review. for the Quantitative Reasoning Measure of the GRE revised General Test

The C Programming Language course syllabus associate level

Common Beginner C++ Programming Mistakes

8 Divisibility and prime numbers

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Compiler Construction

I PUC - Computer Science. Practical s Syllabus. Contents

Formal Languages and Automata Theory - Regular Expressions and Finite Automata -

Formulas, Functions and Charts

Moving from C++ to VBA

NUMBER SYSTEMS. William Stallings

Visual Studio 2008 Express Editions

Binary Division. Decimal Division. Hardware for Binary Division. Simple 16-bit Divider Circuit

Computer Programming Tutorial

Transcription:

Chapter One Introduction to Programming 1-1 Algorithm and Flowchart Algorithm is a step-by-step procedure for calculation. More precisely, algorithm is an effective method expressed as a finite list of well-defined instructions for calculating a function. Starting from an initial state, the instructions describe a computation that, when executed, will proceed through a finite number of well-defined successive states, eventually producing "output" and terminating at a final ending state. Flowchart is a graphical representation of an algorithm. These flowcharts play a vital role in the programming of a problem and are quite helpful in understanding the logic of complicated and lengthy problems. Once the flowchart is drawn, it becomes easy to write the program in any high level language. Often we see how flowcharts are helpful in explaining the program to others. Hence, it is correct to say that a flowchart is a must for the better documentation of a complex program. Flowcharts are usually drawn using some standard symbols; however, Start or end of the program Computational steps or processing function of a program Input or output operation Decision making and branching Connector or joining of two parts of program 1

The following are some guidelines in flowcharting: a. In drawing a proper flowchart, all necessary requirements should be listed out in logical order. b. The flowchart should be clear, neat and easy to follow. c. The usual direction of the flow of a procedure or system is from left to right or top to bottom. d. Only one flow line should come out from a process symbol. or e. Only one flow line should enter a decision symbol, but two or three flow lines, one for each possible answer, should leave the decision symbol. f. Only one flow line is used in conjunction with terminal symbol. h. If the flowchart becomes complex, it is better to use connector symbols to reduce the number of flow lines. Avoid the intersection of flow lines if you want to make it more effective and better way of communication. i. Ensure that the flowchart has a logical start and finish. j. It is useful to test the validity of the flowchart by passing through it with a simple test data. 2

Ex 1-1: Write an algorithm and draw the flowchart for finding the sum of two numbers Algorithm: Input: two numbers x and y Output: the sum of x and y Steps: 1. input x 2. input y 3. sum = x + y 4. output sum START Input x Input y Sum = x + y Output Sum END Q1-1: Write an algorithm and draw the flowchart for finding the area of a rectangle. 1-2 C++ Programming Language Programming is a core activity in the process of performing tasks or solving problems with the aid of a computer. [problem or task specification] - COMPUTER - [solution or completed task] Unfortunately things are not (yet) that simple. In particular, the "specification" cannot be given to the computer using natural language. Moreover, it cannot (yet) just be a description of the problem or task, but has to contain information about how the problem is to be solved or the task is to be executed. Hence we need programming languages. There are many different programming languages, but all of them can be classified into: "High-level" programming languages: These are languages whose syntax is relatively close to natural language. Low-level languages: These include many technical references to the 0's and 1's of the computer. 3

C++ is one of the high-level languages; it was developed at Bell Laboratories in the early 1980's, and is based on the C language. C++ is intended as an incremental improvement of C. Most of C is a subset of C++, so that most C programs can be compiled using a C++ compiler. 1-3 Introduction to Programming Ex 1-2: Printing line of text. Line 1: // A first program in C+ begins with //, indicating that the remainder of the line is a comment which describes the purpose of the program. A comment beginning with // is called a single-line comment because it terminates at the end of the current line. Note: You also may use C s style in which a comment (possibly containing many lines) begins with /* and ends with */ /* A first program in C++ Date 2 Oct. 2012 */ Line: #include <iostream.h> notifies the preprocessor to include in the program the contents of the input/output stream header <iostream.h>. This header must be included for any program that outputs data to the screen or inputs data from the keyboard using C++ s stream input/output. 4

The main function: int main ( ) is a part of every C++ program. The parentheses after main indicate that main is a program building block called a function. The left brace, {, must begin the body of every function. A corresponding right brace, }, must end each function s body. An Output Statement cout << Welcome to C++!\n"; instructs the computer to perform an action, to print the string of characters contained between the double quotation marks. The entire line, including cout, the << operator, the string "Welcome to C++!\n" and the semicolon ( ; ), is called a statement. Note: Every C++ statement must end with a semicolon, omitting the semicolon at the end of a C++ statement is a syntax error. Executing the last statement will print the string ( Welcome to C++!) on the screen. You noticed that \n has not been printed, it called a escape sequence. The escape sequence \n means newline. It causes the cursor (i.e., the current screen-position indicator) to move to the beginning of the next line on the screen. Some common escape sequences are listed below: The return Statement return 0; // indicate that program ended successfully is one of several means we ll use to exit a function. The right brace, }, indicates the end of function main. 5

* The string Welcome to C++! can be printed by a different way: Ex 1-3: Adding two integer numbers. This program uses the input stream object cin to obtain two integers typed by a user at the keyboard, computes the sum of these values and outputs the result using cout. 6

Variable Declarations The identifiers integer1, integer2 and sum are the names of variables. A variable is a location in the computer s memory where a value can be stored for use by a program. These declarations specify that the variables integer1, integer2 and sum are data of type int, meaning that these variables will hold integer values. Note: 1. We could have declared all three variables in one declaration by using a comma-separated list as follows: 2. Declarations of variables can be placed almost anywhere in a program, but they must appear before their corresponding variables are used in the program, for example: Displaying the Result The statement above displays the character string Sum is followed by the numerical value of variable sum followed by endl, the name endl is an abbreviation for end line. Note: Calculations can also be performed in output statements: 7

1-4 Arithmetic Operators Integer division yields an integer quotient; for example, the expression 7 / 4 evaluates to 1. C++ provides the modulus operator, %, that yields the remainder after integer division. The modulus operator can be used only with integer operands; for example, 7 % 4 yields 3. Parentheses are used in C++ expressions in the same manner as in algebraic expressions. For example, to multiply a times the quantity b + c we write: a * ( b + c ) C++ applies the operators in arithmetic expressions in a precise order determined by these rules of operator precedence, which are generally the same as those in algebra: 8

Ex 1-4: Determine arithmetic mean (average) of five terms. Note: If the parentheses are omitted, we obtain a + b + c + d + e / 5, which evaluates as: Ex 1-5: Convert the following algebraic expression to C++ expression and indicate the order in which C++ applies the operators. Q1-2: Convert the second-degree polynomial y = ax 2 + bx + c to C++ expression and indicate the order in which C++ applies the operators. Ex 1-6: Write a C++ program to calculate the sum and average of three integer numbers. 9

Note: The variables sum and average are data of type float, meaning that these variables will hold floating point values. Ex 1-7: Write a C++ program to calculate the circumference of a circle. Note: Constant variables can be declared either: const int S=179; const float PI=3.14; or #define S 179 #define PI 3.14 10