Programming Funamental slides

Slides:



Advertisements
Similar presentations
Programming Funamental slides1 Control Structures Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing Sequencing.
Advertisements

1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Basic Elements of C++ Chapter 2.
Programming is instructing a computer to perform a task for you with the help of a programming language.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Control Structures (A) Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
Control Structures (B) Topics to cover here: Sequencing in C++ language.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
Introduction to Programming Lecture 4. Key Words of C main main if if else else while while do do for for.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Chapter 3 – Variables and Arithmetic Operations. First Program – volume of a box /************************************************************/ /* Program.
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
A Sample Program #include using namespace std; int main(void) { cout
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Bill Tucker Austin Community College COSC 1315
Chapter 1.2 Introduction to C++ Programming
TK1913 C++ Programming Basic Elements of C++.
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
What Actions Do We Have Part 1
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
Completing the Problem-Solving Process
Programming Fundamental
Computing Fundamentals
Basic Elements of C++.
Chapter 2 Assignment and Interactive Input
Introduction to C++.
Introduction to C++ October 2, 2017.
Chapter 2: Introduction to C++
Basic Elements of C++ Chapter 2.
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
2.1 Parts of a C++ Program.
Compound Assignment Operators in C++
Introduction to C++ Programming
Programming Funamental slides
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Chapter 3 Input output.
Introduction to C++ Programming
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
Programs written in C and C++ can run on many different computers
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Review of C++ Language Basics
What Actions Do We Have Part 1
Introduction to Programming
Programming Fundamental-1
Presentation transcript:

Programming Funamental slides Control Structures Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing Sequencing in C++ language Programming Funamental slides

Programming Funamental slides Control Structures In your algorithm (or program), you can use different types of statements. There are 3 categories of control structures: 1- Sequencing 2- Selection 3- Repetition Programming Funamental slides

Programming Funamental slides 1- Sequencing A compound statement (or a block) is a sequence of statements ordered in a way to give a solution: e.g. S1 S2 S3 is a sequence of 3 statements Programming Funamental slides

Programming Funamental slides 1- Sequencing (Cont.) The statements that have a sequential control: 1- INPUT statements 2- OUPUT statements 3- Assignment statement INPUT statement Use Input statement to input data into variables from the standard input device (e.g. a keyboard). Programming Funamental slides

Programming Funamental slides INPUT Statement Syntax: In pseudo code In C++ INPUT List of variables cin >> identifier >> identifier ; where, List of variables contains one or more variables Example: In pseudo code In C++ INPUT x cin>>x; INPUT a, b cin>>a>>b; The semantics (execution) of this statement: You can enter the values you want for the variables in the statement from the keyboard and the computer will assign these values into the variables (stores them in memory). Programming Funamental slides

Programming Funamental slides OUPUT Statement The OUTPUT statement has many uses. Use OUTPUT statement to output the following in the standard output device (EX: a screen). The values of variables stored in memory message (i.e. a string of characters). The value of an expression. Causes a new line ( Use this for output clarity) Programming Funamental slides

Programming Funamental slides OUPUT Statement 1- OUTPUT the values of variables stored in memory Syntax: In pseudo code In C++ OUTPUT List of variables cout<< identifier << identifier ; where, List of variables contains one or more variables Example: In pseudo code In C++ OUTPUT x cout<<x; OUTPUT a, b cout<<a<<b; The semantics (execution) of this statement: This statement allows the computer to access the locations of the variables mentioned in the statement and displays their contents on an output device (e.g. a screen). Programming Funamental slides

Programming Funamental slides OUPUT Statement 2- OUTPUT message where message may by any string of characters enclosed with double quotas. Syntax: In pseudo code In C++ OUTPUT message cout >> “message” ; Example: In pseudo code In C++ OUTPUT “Enter 3 values” cout<<“Enter 3 values”; The semantics (execution) of this statement: This statement will display the message on the screen. Programming Funamental slides

OUPUT Statement 3- OUTPUT expression where expression is any arithmetic expression Syntax: In pseudo code In C++ OUTPUT expression cout >> expression; Example: In pseudo code In C++ OUTPUT 3 + 6 OUTPUT x – y cout<< 3+6; cout<< x-y; The semantics (execution) of this statement: First, the expression is evaluated, then the result will be displayed on the screen. For the first example, it will display 9. 9

OUPUT Statement 4- OUTPUT a new line Syntax: In pseudo code In C++ OUTPUT endl cout << endl; 10

OUPUT Statement NOTE You can mix between the different types of the OUTPUT statements. Example: In pseudo code In C++ OUTPUT “Length = “ , length cout<< “length=”<<length; The semantics (execution) of this statement: This statement will display the message Length = on the screen and on the same line it will display the value of the variable length. 11

Assignment Statement Storing a new value in a memory location is called assignment. Syntax: In pseudo code In C++ Variable  Expression Variable = Expression Example: In pseudo code In C++ X  10 Y  X + 5 Z  y; X = 10 Y = X + 5 Z=y; The semantics (execution) of this statement: 1- The Expression on the RHS is evaluated 2- The result of the expression is assigned to the variable on the LHS 12

Programming Funamental slides Assignment Statement NOTE: The right hand side (RHS) of the assignment statement should be of the same data type of the left hand side (LHS). e.g. 1- T  true This will be correct if T is of Boolean type. 2- A  x + y * 2 This will be correct if A has a numeric data type (e.g. integer, or real) and the value of the expression on (RHS) has the same numeric data type. Programming Funamental slides

Assignment Operator L.H.S = R.H.S. X+ 3 = y + 4 Wrong Z = x +4 True x +4 = Z Wrong

Assignment Statement (Cont.) How to execute a statement like X  X + 1 ? Suppose we have: X  5 Then to execute X  X + 1, we proceed as follows: X X  5 X  X + 1 5 6 Programming Funamental slides

Assignment Statement .. cont. Dereferencing: If we want to copy a value from one memory location (say, X) into another location (say, Y), we say that we dereference a variable. e.g. X  5 Y  10 X  Y // now X has the value 10 X Y 10 5 10 Programming Funamental slides

Programming Fundamentals --> Ch1. Problem solving C++ Language Elements The general form of a C++ program // File: filename // Program description # include compiler directives void main ( ) { declarations section executable statement section } Programming Fundamentals --> Ch1. Problem solving

Programming Funamental slides 1- Comments in Programs In C++, the two symbols // are used to denote a program comment. If comments need more than one line, then you can use the symbols /* to begin comment and */ to end it. Programming Funamental slides 18

2- The include compiler directive The line begins with #include represents a compiler directive. A compiler directive is processed at compilation time. C++ syntax: # include <filename> Programming Funamental slides 19

2- The include compiler directive e.g. #include <iostream> using namespace std; iostream is the name of a C++ library header file whose contents are inserted in place of the #include line during compilation. iostream is used to manipulate input/output operations The standard I/O stream objects, cin and cout are already defined in iostream Programming Funamental slides 20

Programming Funamental slides 3- Declaration Section In the algorithmic language, we will use identifiers without declaring them In C++ the declaration section tells the compiler what data are needed in the program. Declarations are based on the problem data requirements identified during the problem analysis. All identifiers must be declared before they are used. Every identifier associated with a problem data element must be declared only once in the declaration section. Programming Funamental slides 21

Programming Funamental slides 3- Declaration Section Syntax: <type> List-of-identifiers where, type is any predefined type in C++, and List-of-identifiers is a list that contains one or more identifiers. 1- Declaring identifiers of integer type : int x ; int x, y; 2- Declaring identifiers of character type: char c1, c2 ; 3- Declaring identifiers to hold real numbers: float sum ; double total ; Programming Funamental slides 22

Declaration and initialization int c=5; int n1=3, n2=7; float c1=3.5, c2; int z; z=10; In a program a variable has: Name,Type,Size,Value Programming Funamental slides 23

Examples on Simple Algorithms Write an algorithm to Compute and print the summation of two numbers. First, we have to analyze the problem to understand what is the input, output of the problem, and which formula to use to solve the problem (if any). Programming Funamental slides

Programming Funamental slides Example1 .. cont. 1- Analysis stage: Problem Input: - num1 - num2 Problem Output: - summation of two numbers Formula: sum=num1+num2 Programming Funamental slides

Programming Funamental slides Example1 .. cont. 2- Algorithm Design We write the algorithm by using the pseudo code ALGORITHM Summation INPUT num1, num2 sum num1+ num2 OUTPUT “sum=“ ,sum END Summation Programming Funamental slides

Programming Funamental slides Example1 .. cont. 3- Testing the algorithm We give a sample data to see whether the algorithm solves the problem correctly or not. To give a sample data, proceed as follows: 1- Prepare a table to contain all variables of the algorithm. 2- Give any data you like as input. 3- Calculate any formula in the algorithm using these data. 4- Show the output e.g. num1 num2 sum 13 50 --- 63 The output: sum= 63 Programming Funamental slides

Programming Funamental slides Flow Chart Input num1,num2 Sum = num1+ num2 Output sum Programming Funamental slides

Example1 on C++ Programs //This program Compute and print the summation of two numbers #include <iostream> using namespace std; void main() { int num1, num2, sum; cout<<"Please Enter two numbers:"; cin>>num1>>num2; sum = num1 + num2; cout<<"sum="<<sum<<endl; } Programming Funamental slides

Examples on Simple Algorithms Write an algorithm to determine the total cost of apples given the number of kilos of apples purchased and the cost per kilo of apples. First, we have to analyze the problem to understand what is the input, output of the problem, and which formula to use to solve the problem (if any). Programming Funamental slides

Programming Funamental slides Example2 .. cont. 1- Analysis stage: Problem Input: - Quantity of apples purchased (in kilos) - Cost per kilo of apples (in dinar/fils per kilo) Problem Output: - Total cost of apples (in dinar/fils) Formula: Total cost = Number of kilos of apples × Cost per kilo Programming Funamental slides

Programming Funamental slides Example2 .. cont. 2- Algorithm Design We write the algorithm by using the pseudo code ALGORITHM apples INPUT quantity, cost total_cost  quantity * cost OUTPUT “Total cost = “ , total_cost END apples Programming Funamental slides

Programming Funamental slides Example2 .. cont. 3- Testing the algorithm We give a sample data to see whether the algorithm solves the problem correctly or not. To give a sample data, proceed as follows: 1- Prepare a table to contain all variables of the algorithm. 2- Give any data you like as input. 3- Calculate any formula in the algorithm using these data. 4- Show the output e.g. quantity cost total_cost 3 0.9 --- 2.7 The output: Total cost = 2.7 Programming Funamental slides

Programming Funamental slides Flow Chart INPUT quantity, cost total_cost  quantity * cost OUTPUT “Total cost Programming Funamental slides

Example2 on C++ Programs // File: apples.cpp /*This program calculates the price of purchasing some kilos of apples */ #include <iostream> using namespace std; void main ( ) { int quantity ; float cost , total_cost ; cin >> quantity >> cost ; total_cost = quantity * cost ; cout << “Total cost = “ << total_cost << endl ; } Programming Funamental slides

Programming Funamental slides Example 3 The problem statement: Write an algorithm that Compute and print the average of three numbers 1- Analysis stage: Problem Input: - n1,n2,n3 Problem Output: - average Formula: sum= n1 + n2 + n3 average = sum / 3 Programming Funamental slides

Programming Funamental slides Example 3 .. cont. 2- Algorithm Design ALGORITHM Avg INPUT n1, n2, n3 sum n1 + n2 + n3 average  sum / 3 OUTPUT average END Avg Programming Funamental slides

Programming Funamental slides Example 3 .. cont. 3- Testing the algorithm n1 n2 n3 sum average 2 6 1 9 3 The output: average= 3 ----------------------------------------------------------- Programming Funamental slides

Programming Funamental slides Flow Chart Input n1,n2,n3 sum = n1+n2+n3 Average = sum / 3 Output average Programming Funamental slides

Example3 on C++ Programs /*This program Compute and print the average of three numbers */ #include <iostream> using namespace std; void main() { int n1, n2, n3; float s, average; cout<<"Please Enter three integers"; cin>>n1>>n2>>n3; s = n1 + n2 + n3; average = s / 3; cout<<"\n Average = \t"<<average<<endl; } Programming Funamental slides

Integrated Development Environment Programming Fundamentals --> Ch1. Problem solving

b2 - 2a 4c Discriminant = b*b - 4*a*c /2 *a Incorrect answer Solution = (b*b - 4*a*c) /(2 *a) Correct answer

Programming Funamental slides Examples: Q1: Given the following declarations Indicate which C++ statements below are valid and find the value of each valid statement. Also Indicate which are invalid and why. x=j / i ; y=j % i ; z=k / i ; w=k % i ; int x,y,z,w; int i = 10; int j = 15; float k = 15.0; Programming Funamental slides

Q2: Find the output of the following program #include <iostream> using namespace std; main ( ) { int x ; int y ; int z ; x = 10 ; y = 20 ; z = x + y ; cout << " x = " ; cout << x ; cout << " y = " ; cout << y ; cout << " z =x + y = " ; cout << z ; }

Q3) Writ algorithm and c++ program to find the Area of the Ring Area of Outer Circle

Problem Given a four-digit integer, separate and print the digits on the screen

Analysis Input: Number = 1234 Process: Take the remainder of the above number after dividing by 10 Eg 1234 / 10 gives remainder 4 1234 % 10 = 4 Remove last digit 1234/10 = 123.4 123 (Truncation due to Integer Division) 123 %10 gives 3 123/10 = 12.3 12 (Truncation due to Integer Division) 12 % 10 gives remainder 2 12/10 = 1.2 1 (Truncation due to Integer Division) Final digit remains Output: print the digits on the screen one digit in each line

Code #include <iostream.h> main ( ) { int number; int digit; cout << “Please enter a 4 digit integer : ”; cin >> number; digit = number %10; cout <<“The digit is: “ << digit << ‘\n’; // first digit; and then << ‘\n’ number = number / 10; digit = number % 10; cout <<“The digit is: “ << digit << ‘\n’; cout <<“The digit is: “ << digit; }