CSCE 110 PROGRAMMING FUNDAMENTALS

Similar documents
Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

C++ Programming: From Problem Analysis to Program Design, Third Edition

Chapter 2: Overview of C++

UNIT- 3 Introduction to C++

A First Program - Greeting.cpp

6.096 Introduction to C++ January (IAP) 2009

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Objectives. In this chapter, you will:

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

BITG 1233: Introduction to C++

Chapter 2. Outline. Simple C++ Programs

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Computer Programming : C++

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

LECTURE 02 INTRODUCTION TO C++

3.1. Chapter 3: The cin Object. Expressions and Interactivity

CHAPTER 3 BASIC INSTRUCTION OF C++

Creating a C++ Program

Fundamentals of Programming CS-110. Lecture 2

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Engineering Problem Solving with C++, Etter/Ingber

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

CS2141 Software Development using C/C++ C++ Basics

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

Introduction to C++ with content from

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

COMP322 - Introduction to C++ Lecture 01 - Introduction

Introduction to Programming using C++

Programming Language. Functions. Eng. Anis Nazer First Semester

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

Chapter 3. Numeric Types, Expressions, and Output

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

CSCI 123 Introduction to Programming Concepts in C++

Chapter 1 Introduction to Computers and C++ Programming

The C++ Language. Arizona State University 1

Chapter 2 Basic Elements of C++

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Introduction to Programming EC-105. Lecture 2

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Programming. C++ Basics

CS313D: ADVANCED PROGRAMMING LANGUAGE

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

CSCE 110 PROGRAMMING FUNDAMENTALS

Chapter 3: Operators, Expressions and Type Conversion

Differentiate Between Keywords and Identifiers

CS242 COMPUTER PROGRAMMING

Department of Computer Science

Scientific Computing

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

DEPARTMENT OF MATHS, MJ COLLEGE

CSI33 Data Structures

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

Input And Output of C++

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

2 nd Week Lecture Notes

Unit 3. Operators. School of Science and Technology INTRODUCTION

Tutorial-2a: First steps with C++ programming

Operators and Expressions:

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Declaration and Memory

Chapter 15 - C++ As A "Better C"

Boolean Algebra Boolean Algebra

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

SECTION II: LANGUAGE BASICS

Lecture 2 Tao Wang 1

Introduction to C++ Systems Programming

Lesson 3 Introduction to Programming in C

Summary of basic C++-commands

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

3.1. Chapter 3: Displaying a Prompt. Expressions and Interactivity

Binghamton University. CS-120 Summer Introduction to C. Text: Introduction to Computer Systems : Chapters 11, 12, 14, 13

CSC 307 DATA STRUCTURES AND ALGORITHM ANALYSIS IN C++ SPRING 2011

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

Operators. Lecture 3 COP 3014 Spring January 16, 2018

COMPUTER SCIENCE HIGHER SECONDARY FIRST YEAR. VOLUME II - CHAPTER 10 PROBLEM SOLVING TECHNIQUES AND C PROGRAMMING 1,2,3 & 5 MARKS

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

OBJECT ORIENTED PROGRAMMING

- Developed by Bjarne Stroustrup at AT&T Bell Laboratories

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

Course Outline Introduction to C-Programming

Basics of Programming

BASIC ELEMENTS OF A COMPUTER PROGRAM

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Transcription:

CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 2. Overview of C++ Prof. Amr Goneid, AUC 1

Overview of C++ Prof. Amr Goneid, AUC 2

Overview of C++ Historical C++ Basics Some Library Functions Expressions & Assignment Simple Input/Output Prof. Amr Goneid, AUC 3

Historical 1967: BCP language (Martin Richards, Cambridge) 1969: B language (Ken Thompson, Bell labs) Early 1970 s: C language & Unix Operating System (Dennis Ritchie, Bell Labs) Prof. Amr Goneid, AUC 4

Historical Mid-1980 s: Early C++ derived from C (Bjarne Stroustrup, Bell labs) 1998: Formally standardized C++, an OOP language Prof. Amr Goneid, AUC 5

1. C++ Basics Example Program Style, Declarations Data Types Constants Declaring Constants & variables Prof. Amr Goneid, AUC 6

1.1 Example Program: Sorting an Array of Integers Prof. Amr Goneid, AUC 7

Sorting an Array of Integers # include <iostream> using namespace std; // Functions Used void selectsort (int a[ ], int n); // Function Prototype int main() { const int MAX = 100; int a[max], k, n; // Start of main function // Data Declarations Prof. Amr Goneid, AUC 8

Example Program: Main Function Actions /* Begin Main Actions */ cin >> n; // Read size of data if (n > MAX) n = MAX; // to prevent array overflow for (k = 0; k < n; k++) cin >> a[k]; // Read data into array selectsort(a,n); // Call sorting module for (k = 0; k < n; k++) cout << a[k]; // Write sorted data cout << \n // Move to a new line return 0; // No errors } /* End. Main Function */ Prof. Amr Goneid, AUC 9

Example Program: Sorting Module (Function) void selectsort (int a[ ], int n) { // Begin Module int i, j, min, temp; // Module Local Data for (i = 0; i < n-1; i++ ) // Begin module action { min = i; for ( j = i+1; j < n; j++) if (a[j] < a[min] ) min = j; temp = a[min]; a[min] = a[i]; a[i] = temp; } } // End Module Prof. Amr Goneid, AUC 10

1.2 Style Lines Separators (spaces, lines, comments) Comments ( //.. Single line or /*..*/ ) Case sensitive ( e.g. MAX, max) Keywords ( e.g. for if return..) use lowercase letters User Identifiers (e.g. MAX, min, a, n, selectsort,.. etc) Prof. Amr Goneid, AUC 11

Style (continued) Constants (e.g. MAX,100, \n etc) Operators (e.g. <= ++ + >> etc) Punctuators (e.g. ( ) ; { } etc) Keywords, identifiers, constants, operators and punctuators are called TOKENS Compiler Directives Prof. Amr Goneid, AUC 12

Compiler Directives #include Compiler directive Processed at compilation time Instructs compiler on what you want in the program #include <iostream> Adds library files to program Used with < > Also user defined Prof. Amr Goneid, AUC 13

Some Declaration Keywords Modules (Functions): main() void <function name>(..) Data Declaration: const for constant data int, float, etc, for data types string for user defined strings Prof. Amr Goneid, AUC 14

1.3 A Classification of Data Types Data Type Scalar Data Structure Integer Floating Character Logical (bool) Pointer Arrays Structs Unions Classes Files Streams strings.. Prof. Amr Goneid, AUC 15

Some Scalar Data Types Integers: int short unsigned int long Floating: float double long double Character: char unsigned char Logical (Boolean): bool Prof. Amr Goneid, AUC 16

Ranges of Scalar Data Types int, short 2 bytes -32,768.. 32,767 unsigned int, unsigned short 2 bytes 0.. 65,535 long 4 bytes -2G.. 2G unsigned long 4 bytes 0.. 4G float 4 bytes ~ E +/- 38 (7 digits) double 8 bytes ~ E +/- 308 (15 digits) long double 10 bytes ~ E +/- 4932 (19 digits) Prof. Amr Goneid, AUC 17

Ranges of Scalar Data Types char 1 byte -128.. 127 unsigned char 1 byte 0.. 255 bool 1 byte true / false non-zero / zero Note: Integer, character and boolean types are called Ordinal Types because their members can be listed by rank. Prof. Amr Goneid, AUC 18

1.4 Examples of Constants Predefined: true false SHRT_MAX (32767) Integer: 79 (decimal of type int) 232467L (decimal of type long) Floating: 1.35 2.34e-3 5.705E+5 (decimal of type double) Character: A \n (type char) String Literals: Hello (class string) Prof. Amr Goneid, AUC 19

1.5 Declaring & Initializing Constants Syntax : const <type> <name> = <value>; Examples: const int MAX = 100; {integer} const bool says = true; {Boolean} const string message = warning! ; {String literal} const float KmperMile= 1.609344F; {float} const alpha = 1.2345E-15; {double} const Large = -2345678L; {long} const char Initial = H ; {char} Prof. Amr Goneid, AUC 20

Declaring & Initializing Variables Syntax: <type> <name>, <name>.. ; or <type> <name> = <value>; Examples: unsigned char pixel ; int k = 2197; float x, y ; char c ; bool test ; string name = Ann W. Wolf ; Prof. Amr Goneid, AUC 21

Assigning Constants to Variables changes initial values Examples: pixel = 212; k = -16329; x = 3.1415926; c = H ; test = false; name = John W. Wolf ; k = SHRT_MAX; Prof. Amr Goneid, AUC 22

2. Some Library Functions Prof. Amr Goneid, AUC 23

Some Library Functions To invoke: FunctionName (x) returns value of function for argument x Examples: char(65) returns A int( A ) returns 65 sqrt(4) returns 2.0 abs(-6) returns 6 sin(1.5708) returns 1.00 Prof. Amr Goneid, AUC 24

Library Functions(continued) cos(0.0) returns 1.0 atan(1.0) returns pi/4 tan(0.0) returns 0.0 log(2.0) returns 0.693147 exp(2.0) returns 7.38906 pow(4,1.5) returns 8.00 = 4 1.5 ceil(2.67) returns 3.0 floor(2.67) returns 2.0 Random(n) returns random int 0.. n-1 Prof. Amr Goneid, AUC 25

Using a Library Function //Computes w = (1+z) 2.6 z 1.45 / (1-z) 3.2 # include <cmath> // pow function # include <iostream> // I/O functions using namespace std; int main ( ) { float w, z ; cout << Enter z: ; cin >> z ; w = pow(1+z,2.6) * pow(z,1.45) / pow(1-z,3.2); cout << Value of w = << w << endl; return 0 ; } Prof. Amr Goneid, AUC 26

3. Expressions & Assignment Syntax Arithmetic Expressions Logical Expressions Relational Expressions Assignment Statement Overall Operator Precedence More on Arithmetic and Assignment Prof. Amr Goneid, AUC 27

3.1 Syntax Form1 Form2 U-Op operand operand B-Op operand e.g -5!found a + b x > y + 32 (c >= 65) && (c <=90) Prof. Amr Goneid, AUC 28

Syntax Operands: Constants e.g. 5 false Variables e.g. x a[3] Functions e.g. sqrt(7) sin(y) Expression e.g. x + y a > b Operators: Arithmetic Logical Relational Assignment Compound Prof. Amr Goneid, AUC 29

3.2 Arithmetic Expressions Operators: Unary Minus and Plus (-) (+) Multiplication, Division, Modulus ( * / % ) Addition, Subtraction ( + - ) Examples: -5 a*b sqrt(5)/2.0 x % m y - b (a+sqrt(y))/(6- sin(pi*y)) m / n - k Prof. Amr Goneid, AUC 30

Arithmetic Expressions Examples: 5 / 2 is 2 (int operands, int value) 5.0 / 2.0 (float operands, float value) 5.0 / 2 (Mixed operands, float value) 7 % 2 is 1 3 % 5 is 3 (% for int only) 2 + 6 /3 + 5 is 9 but (2 + 6) / (3 + 5) is 1 Prof. Amr Goneid, AUC 31

Arithmetic Expressions Operator Precedence: ( ) Highest unary + - * / % Add (+), Subtract (-) Lowest Prof. Amr Goneid, AUC 32

Example How many Hours, Minutes, Seconds are in n seconds? int n, hrs, mins, secs, rem; hrs = n / 3600; rem = n % 3600; mins = rem / 60; secs = rem % 60; Prof. Amr Goneid, AUC 33

3.3 Logical Expressions Operators:! (unary not) (or) && (and) Operands of Boolean type Result: Boolean ( true, false ) Examples:! true is false a b! (x<y) c && d Prof. Amr Goneid, AUC 34

Truth Table for Logical Operators 0 false, 1 true x y!x x y x && y 0 0 1 0 0 0 1 1 1 0 1 0 0 1 0 1 1 0 1 1 Prof. Amr Goneid, AUC 35

3.4 Relational Expressions Operators: == < > <= >=!= Result: Boolean Examples: a > b c <= 6 sqrt(x) >= y z!= w a+b == c+d A < a name1!= name2 Prof. Amr Goneid, AUC 36

3.5 Assignment Statement The Assignment operator ( = ) Syntax: variable = expression; Examples: int x, y ; bool a,b,c ; x = 3; y = 2*x; a = true; a = x > y; c = a b; Prof. Amr Goneid, AUC 37

3.6 Overall Operator Precedence Parentheses ( ) Highest Unary:! + - Multiplicative: * / % Additive: + - Relational: < > <= >= Relational: ==!= Logical: && Logical: Assignment = Lowest Example: d =!(x+y < z) && (2*b == c); Prof. Amr Goneid, AUC 38

3.7 More on Arithmetic and Assignment Increment and Decrement Operators: ++operand --operand (increment/decrement operand then evaluate expression) operand++ operand-- (evaluate expression then increment/decrement operand ) e.g. n++, ++n is shorthand for n = n + 1 Prof. Amr Goneid, AUC 39

Increment and Decrement Operators Examples: int a = 3; int b = 5; int c; c = a + b++; yields a == 3, b == 6, c == 8 c = a + ++b; yields a == 3, b == 6, c == 9 c = a + b--; yields a == 3, b == 4, c == 8 c = a + --b; yields a == 3, b == 4, c == 7 Prof. Amr Goneid, AUC 40

Multiple & Compound Assignment Multiple Assignment: c = a = b; d = (a = b + 3) / c; Compound Assignment: (Reassign after doing operation) e.g. a = a + b; //var = var op expr; can be written: a += b; //var compound-op expr; compound operators += -= *= /= %= e.g. c /= (x + 2); is c = c / (x + 2); Prof. Amr Goneid, AUC 41

4. Simple Input/Output Prof. Amr Goneid, AUC 42

Simple Input/Output Standard I/O Devices: cin standard input stream (keyboard) cout standard output stream (screen) Defined in #include <iostream> Extraction Operator: >> DataVariable extracts one data item from cin to a variable Insertion Operator: << DataElement inserts one data element in cout Prof. Amr Goneid, AUC 43

Simple Input/Output Keyboard Input: cin >> v ; cin >> v1 >> v2 ; Variables are entered with spaces between them. ENTER or RETURN end input. e.g. int a,b; float x; string name; cin >> name; cin >> a >> b >> x; A.W.Wolf 12 524 2.567 Prof. Amr Goneid, AUC 44

Data Types and cin Don t mix types with cin int x; cin >> x; If Keyboard input is 16.6 The value placed in x would be 16 Prof. Amr Goneid, AUC 45

Other Characteristics of cin Leading blanks ignored (floats, int, char, bool and strings) Char read 1 at a time (1 non blank) Case issues int or float will read until space Stings same as int and float Prof. Amr Goneid, AUC 46

Simple Input/Output Screen Output: cout << d; cout << d1 << d2..; Examples: int a,b,c; float z; cout << Enter a,b,c: ; Enter a,b,c: 600 2 500 cin >> a >> b >> c; cout << a << 2*b << endl; 6004 z = sqrt(a + b * c); cout << Result is ; cout << z; Result is 40.00 Prof. Amr Goneid, AUC 47

Example Program : Hello.cpp // FILE: Hello.cpp // DISPLAYS A USER'S NAME #include <iostream> #include <string> using namespace std; int main () { Prof. Amr Goneid, AUC 48

Hello.cpp char letter1, letter2; string lastname; } // Enter letters and print message. cout << "Enter 2 initials and last name: "; cin >> letter1 >> letter2 >> lastname; cout << "Hello " << letter1 << ". " << letter2 << ". " << lastname << "! "; cout << "We hope you enjoy studying C++." << endl; return 0; Prof. Amr Goneid, AUC 49

Hello.cpp Program Input/output Enter 2 initials and last name: SAWolf Hello S. A. Wolf! We hope you enjoy studying C++. Prof. Amr Goneid, AUC 50