Week 1 Algorithmization and Programming Languages.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C++ Programming. A Simple Program: Print a Line of Text // My First C++ Program #include int main( ) { cout
 C++ programming facilitates a disciplined approach to program design. ◦ If you learn the correct way, you will be spared a lot of work and frustration.
Introduction to C Programming
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 1 – Introduction to Computers and C++ Programming Outline 1.6 Machine Languages, Assembly Languages,
Introduction Kingdom of Saudi Arabia Shaqra University
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline History of C and C++ C++ Standard Library Object Technology Basics.
 2008 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
Three types of computer languages
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Machine Languages, Assembly Languages, and High-level Languages Three types of computer languages 1.Machine.
Introduction to C Programming
Introduction to C++ Programming
Introduction to C++ Programming
Chapter 01: Introduction to Computer Programming
Introduction to C++ Programming
COMPUTER SCIENCE I C++ INTRODUCTION
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
 2008 Pearson Education, Inc. All rights reserved. 1 CISC 1600 – Computer Science I Fall 2010 Introduction to C++ Programming Chapters 1 and 2 (Deitel.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
INTRODUCTION Kingdom of Saudi Arabia Princess Nora bint Abdul Rahman University College of Computer Since and Information System CS240.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
C++ How to Program, Late Objects Version, 7/e © by Pearson Education, Inc. All Rights Reserved.
Announcements Starting next week class 6-8 on Thursday Homework 1 on the web  Due January 29 – next class meeting  Homework policy No late assignments.
 2006 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CHAPTER 1: INTRODUCTION C++ Programming. CS 241 Course URL: Text Book: C++ How to Program, DETITEL & DEITEL, eighth Edition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
 2008 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Chapter 2 of C++ How to Program, 10/e © by Pearson Education, Inc. All Rights Reserved.
© by Pearson Education, Inc. All Rights Reserved.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 6 JavaScript: Introduction to Scripting
Chapter 1.2 Introduction to C++ Programming
Chapter 1: Introduction to computers and C++ Programming
Introduction to C++ Programming
Introduction to C++ Programming
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Chapter 2, Part I Introduction to C Programming
Chapter 2 part #1 C++ Program Structure
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
1.13 The Key Software Trend: Object Technology
Chapter 2 - Introduction to C Programming
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Programs written in C and C++ can run on many different computers
Capitolo 1 – Introduction C++ Programming
Chapter 2 - Introduction to C Programming
Presentation transcript:

Week 1 Algorithmization and Programming Languages

Introduction About C++ Facilitates a disciplined approach to program design C++ programs consist of pieces called classes and functions Provides C++ Standard Library

First Program in C++: Printing a Line of Text

Comments - document program - improve program readability - ignored by compiler - single-line comment starts with // (it terminates at the end of the current line) - multiple-line comments starts with /* and ends with */

Preprocessor directive - begins with a hash sign ( # ) - processed before the program is compiled - includes in the program the contents of the input/output stream header file - preprocessor directives (like #include ) do not end with a semicolon

int main () - a part of every C++ program - is a program building block called a function - exactly one function in every program must be main - C++ programs begin executing at function main, independently of its location within the source code - the keyword* int that main "returns" an integer *A keyword is a word in code that is reserved by C++ for a specific use

BODY - the left brace, {, must begin the body of every function - the right brace, }, must end each function's body - the entire line is called a statement - instructs the computer to perform an action - C++ statement must end with a semicolon( ; ) - output and input are accomplished with streams of characters.

INPUT/OUTPUT cin - Standard input stream - Normally keyboard cout - Standard output stream - Normally computer screen cerr - Standard error stream - Display error messages

Standard output stream object std::cout - “Connected” to screen - Stream insertion operator (<<) - Value to right (right operand) inserted into output stream Namespace std:: specifies using name that belongs to “namespace” std std:: removed through use of using statements Escape characters ( \ ) - Indicates “special” character output

return 0; The return statement causes the main function to finish. return may be followed by a return code (in our example is followed by the return code with a value of zero). A return code of 0 for the main function is generally interpreted as the program worked as expected without any errors during its execution. This is the most usual way to end a C++ console program.

Escape sequence Description \n Newline. Position the screen cursor to the beginning of the next line. \t Horizontal tab. Move the screen cursor to the next tab stop. \r Carriage return. Position the screen cursor to the beginning of the current line; do not advance to the next line. \a Alert. Sound the system bell. \\ Backslash. Used to print a backslash character. \' Single quote. Use to print a single quote character. \'' Double quote. Used to print a double quote character. Escape sequences

 1 // Fig. 1.4: fig01_04.cpp  2 // Printing a line with multiple statements.  3 #include  4  5 // function main begins program execution  6 int main()  7 {  8 std::cout << "Welcome ";  9 std::cout << "to C++!\n";  10  11 return 0; // indicate that program ended successfully  12  13 } // end function main  1 // Fig. 1.4: fig01_04.cpp  2 // Printing a line with multiple statements.  3 #include  4  5 // function main begins program execution  6 int main()  7 {  8 std::cout << "Welcome ";  9 std::cout << "to C++!\n";  10  11 return 0; // indicate that program ended successfully  12  13 } // end function main Welcome to C++! Multiple stream insertion statements produce one line of output. Comments Preprocessor directive BODY

 1 // Fig. 1.5: fig01_05.cpp  2 // Printing multiple lines with a single statement  3 #include  4  5 // function main begins program execution  6 int main()  7 {  8 std::cout << "Welcome\nto\n\nC++!\n";  9  10 return 0; // indicate that program ended successfully  11  12 } // end function main  1 // Fig. 1.5: fig01_05.cpp  2 // Printing multiple lines with a single statement  3 #include  4  5 // function main begins program execution  6 int main()  7 {  8 std::cout << "Welcome\nto\n\nC++!\n";  9  10 return 0; // indicate that program ended successfully  11  12 } // end function main Welcome to C++! Welcome to C++! Using newline characters to print on multiple lines.

Adding Integers Output:

Standard input stream object: std::cin - obtain values typed at the keyboard - stream extraction operator ( >> ) Example: int number1; int number2; cin>>number1>>number2;

Variables - Location in memory where value can be stored - Common data types int – integer numbers (0, 7, 876, 1998) char – characters ('a', 'G', '#') double – floating point numbers (0.2, 3.667, 8.65) Declare variables with name and data type before use int integer1; int integer2; int sum; Can declare several variables of same type in one declaration Comma-separated list int integer1, integer2, sum;

Variables names Valid identifier - Series of characters (letters, digits, underscores) that begin with a letter or underscore - Neither spaces nor punctuation marks or symbols can be part of an identifier - Cannot match any keyword of the C++ Example: char abc, _abc, abc3, a3bc, a_3b_c; - Cannot begin with digit - Case sensitive

Initialization of variables When declaring a regular local variable, its value is by default undetermined. There are two ways to initialize variables in C++: 1. c-like initialization: type identifier = initial_value ; Example: int a = 0; 2. constructor initialization: type identifier (initial_value); Example: int a(0); Both ways of initializing variables are valid and equivalent in C++

6 6

Memory Concepts - Variable names correspond to locations in the computer's memory. - Every variable has a name, a type, a size and a value - When new value placed into variable, overwrites previous value - Reading variables from memory nondestructive - Placing a new value into a memory location is said to be destructive

Memory Concepts std::cin >> integer1; Assume user entered 45 std::cin >> integer2; Assume user entered 72 sum = integer1 + integer2;

Fundamental data types When programming, we store the variables in our computer's memory, but the computer has to know what kind of data we want to store in them, since it is not going to occupy the same amount of memory to store a simple number than to store a single letter or a large number, and they are not going to be interpreted the same way. The memory in our computers is organized in bytes. A byte is the minimum amount of memory that we can manage in C++

Specifiers Specifiers modify the meanings of the basic built-in types and expand them to a much larger set. There are four specifiers: Long Short Signed Unsigned modify the maximum and minimum values that a data type will hold. tell the compiler how to use the sign bit with integral types and characters (floating- point numbers always contain a sign).

Fundamental data types in C++ * The values of the columns Size and Range depend on the system the program is compiled for. The values shown above are those found on most 32-bit systems.

Arithmetic operators

Integer division - yields an integer quotient - fractional part is discarded (i.e., truncated) no rounding occurs Example: int a = 7, b = 4; cout<<a/b; Modulus operator ( % ) - yields the remainder after integer division - can be used only with integer operands * Example: int a = 7, b = 4; cout<<a%b; *Attempting to use the modulus operator (%) with noninteger operands is a compilation error. 1 3

Rules of operator precedence Operators in parentheses evaluated first Nested/embedded parentheses Operators in innermost pair first Multiplication, division, modulus applied next Operators applied from left to right Addition, subtraction applied last Operators applied from left to right

REFERENCES: 1. C++ How to Program, By H. M. Deitel Chapter 1. Introduction to Computers, the Internet and World Wide Web Chapter 2. Introduction to C++ Programming 2.

THANK YOU FOR YOUR ATTENTION