Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,

Slides:



Advertisements
Similar presentations
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Advertisements

Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Chapter 2: Basic Elements of C++
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Basic Elements of C++ Chapter 2.
CHAPTER 2 BASIC ELEMENTS OF C++. In this chapter, you will:  Become familiar with the basic components of a C++ program, including functions, special.
CPS120: Introduction to Computer Science Lecture 8.
Summer 2014 Chapter 1: Basic Concepts. Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, Chapter Overview Welcome to Assembly Language.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming1 Introduction to C – Part 2.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Numeric Types, Expressions, and Output ROBERT REAVES.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
Lecture #5 Introduction to C++
C++ Programming: Basic Elements of C++.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
Copyright © 2002, Department of Systems and Computer Engineering, Carleton University CONTROL STRUCTURES Simple If: if (boolean exp) { statements.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Copyright © – Curt Hill Types What they do.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
Copyright © 2002, Department of Systems and Computer Engineering, Carleton University 1 INPUT STREAMS ifstream xin; // declares an input stream.
CPS120: Introduction to Computer Science Variables and Constants.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
Unary, Binary, logical Operations, Explicit type conversion Lecture 6 Instructor: Haya Sammaneh.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Chapter 2: Basic Elements of C++. Introduction Computer program – Sequence of statements whose objective is to accomplish a task Programming – Process.
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Chapter 2: Introduction to C++
Computing and Statistical Data Analysis Lecture 2
INSPIRING CREATIVE AND INNOVATIVE MINDS
Tokens in C Keywords Identifiers Constants
Computing Fundamentals
Basic Elements of C++.
C Short Overview Lembit Jürimägi.
Basic Elements of C++ Chapter 2.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
2.1 Parts of a C++ Program.
Lectures on Numerical Methods
Elementary Programming (C++)
Chapter 2: Introduction to C++.
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
C++ Programming Basics
C++ Programming Lecture 20 Strings
Session 1 – Introduction to Computer and Algorithm (Part 2)‏
Variables and Constants
Presentation transcript:

Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27, 2008

The Department of Electrical and Computer Engineering 2 History In the beginning no programming languages existed. –5 + 2 : Programming Languages appeared: –A language that both computers and humans understand. –Able to write instructions to make the computer make some actions.

The Department of Electrical and Computer Engineering 3 History (cont.) Low level programming (Assembly) –Reads and write directly on the hardware. –Each line is equivalent to a binary code :  add 5, 2 –Needs always to have registers in the instrucion mov ax, 5 add ax, 2 High level programming –Tries to get closer to the human language. –Each line is equivalent to one or more binary codes. int x = 5 + 2;

The Department of Electrical and Computer Engineering 4 History (cont.) High Level Programming Paradigms –Functional Breaks problems into sub problems Each problem is solved by a function/method/subroutine Examples: –FORTRAN (Formula Translator): first language that allowed algebraic notation –Cobol –C

The Department of Electrical and Computer Engineering 5 History (cont.) High Level Programming Paradigms (cont.) –Object Oriented The problem can be written in objects Each object (noun) has its attributes (adjectives) and methods (verbs) (e.g. Person) Examples: –C++ (compiler translates into machine language (binary code)) –Java (runs with JVM) –C# (runs with.net framework) Note that all these are imperative languages (use of variables to control program flow)

The Department of Electrical and Computer Engineering 6 Outline Variables and Data types –Numeric –Char –Boolean Modifiers Libraries –iostream –cstdio –cstdlib –cmath –cstring Operations –Arithmetic –Logic

The Department of Electrical and Computer Engineering 7 Variables and Data Types Variable: just like in math a variable is something that stores information and changes its value if desired. –Type –Name –Memory Allocation (will be covered later) –e.g. int x = 5;

The Department of Electrical and Computer Engineering 8 Variables and Data Types (cont.) Name –Can not start with numbers –Can not contain white spaces –Typical notation Constant values are stored with names in capital Two words may be written with _ or now with the Java notation (first letter is capital) Names should describe the function of the variable –e.g. int age = 25; //The age of a person is 25 You may start a comment with //comment or /* comment */ for multiple lines All instructions end with a ; (for now )

The Department of Electrical and Computer Engineering 9 Variables and Data Types (cont.) Types –Numeric short/long/int: represents integer numbers float/double: represents real numbers –char: stores a single alphanumeric character Has an ASCII value e.g. char letter = ‘A’; int ascii = letter; //ascii = 65 –bool: can be either true or false. Use for making decisions (C must import #include ) In C/C++ every non-zero number is taken as true while zero is taken as a false. –void: has no value (will be used later in pointers)

The Department of Electrical and Computer Engineering 10 Variables and Data Types (cont.) Types –string: contains a character sequence also known in C as char* (char pointer). Need to import #include –time: represents time values Need to import #include –FILE*: data type use to read/write files (will be discussed later)

The Department of Electrical and Computer Engineering 11 Modifiers unsigned: uses numbers with no sign (positive values only). As a consequence the range is extended. const: used to defined constant variables extern: –Keyword that tells the compiler that the variable or function is defined in another file. –Also may be use to specify the compiler to be used.

The Department of Electrical and Computer Engineering 12 Libraries iostream –cout –cin –cerr –clog cstdio –Contains file handling functions –We will discuss this in full detail later

The Department of Electrical and Computer Engineering 13 Libraries (cont.) cstdlib –Contains memory management –System calls –Random functions (srand/rand) –Conversion funcions atof: converts a string to double atoi: converts a string to integer atol: converts a string to long

The Department of Electrical and Computer Engineering 14 Libraries (cont.) cmath –Contains math functions cos, sin, tan acos, asin, atan exp, log, log10 pow, sqrt ceil, floor, fabs –We can define PI and other constants with #define PI … #define EXP …

The Department of Electrical and Computer Engineering 15 Libraries (cont.) cstring (contains string related functions) –strlen: returns the length of a string int length = strlen(name); –strtok: splits a string into tokens char str[] ="- This, a sample string."; char * pch; printf ("Splitting string \"%s\" into tokens:\n",str); pch = strtok (str,",.-"); while (pch != NULL) { –printf ("%s\n",pch); –pch = strtok (NULL, ",.-"); }

The Department of Electrical and Computer Engineering 16 Operations Add: x + 3 Substract: y – 5 Mutiplication: x * 8.3 Division: –If operands are integers ½ = 0 with reminder of 1 –Remainder can be obtained 1%2 –If operands are real numbers 1.0/2.0 = 0.5

The Department of Electrical and Computer Engineering 17 Operations Shifts: –Left: z << 2; –Right: z >> 3; Logical And: x & 5 Logical Or: y | 2 Logical Not: t = ~t Logical Xor: x ^ y

The Department of Electrical and Computer Engineering 18 Operations Unary increment: ++a, a++ Unary decrement: --a, a— x += 3  x = x + 3 x -= 3  x = x - 3 x *= 3  x = x * 3 x /= 3  x = x / 3 x <<= 3  x = x << 3 x >>= 3  x = x >> 3

The Department of Electrical and Computer Engineering 19 Questions?