Topic 2: C++ Programming fundamentals

Save this PDF as:
Size: px
Start display at page:

Download "Topic 2: C++ Programming fundamentals"

Transcription

1 Topic 2: C++ Programming fundamentals Learning Outcomes Upon successful completion of this topic you will be able to: describe basic elements of C++ programming language compile a program identify and correct errors execute a program test and debug a program describe and use C++ program conventions. Introduction to the Topic This topic introduces the basic components of a C++ program, such as identifiers, special symbols and functions. It explains different data types, and how to declare variables and use operators to manipulate them. You will also learn how to structure a program and use C++ library functions. Background Skills and Knowledge Knowledge gained in Topic C++ identifiers, expressions and constants The basic components of a C++ program include: identifiers special symbols functions. Identifiers (or variables) in C++ are allocated to various storage sizes in a computer memory. The location size depends on the type of the variable to which the identifier refers. EEET 2280 Computing Engineering 35

2 Naming rules: An identifier may consist of letters, digits, and the underscore character (_) An identifier must begin with a letter or underscore Uppercase and lower case letters are considered different (C++ is case sensitive) C++ has some predefined identifiers, such as cout and cin A reserved word (keyword) cannot be used as identifier. For a complete list of reserved words in C++ see Appendix 1 (Appendix 1 is located in the References section of this guide) Activity 2.1 A Discussion/ Problem solving In a group of 2 to 3 students, discuss which of the following names cannot be used as identifiers; explain the reasons, and suggest a valid name. volume Time 1_side mynumber km/hour void degrees_c static $AUS number 1 num_1 grade Grade Grade(%) EEET 2280 Computing Engineering 36

3 2.2 Data types The data type of a variable determines a range of values that can be stored in it, and operations that can be performed. The C++ built-in (simple) data types allow you to define and manipulate variables corresponding to integers, characters, floating-point values, and Boolean values. Three categories of simple data are: integral (char, short, int, long, bool, unsigned char, unsigned int etc. ) floating-point (float, double, long double) user-defined (or enumeration type) allows programmers to define their own simple data type. Fig. 2 demonstrates amount of memory allocated for int, bool, and char variables, and their range of values. Data type Values Storage (in bytes) int to bool True or false 1 char -128 to Figure 2. Data types: range of values and memory allocation Appendix 2 shows built-in numerical data types and their minimum guaranteed ranges as it is specified by the ANSI/ISO C++ Standard. (Appendix 2 is located in the References section of this guide) Variables in C++ must be declared before they are used in the program. You may initialise (assign a value) to the variable after declaration, or at the same time: EEET 2280 Computing Engineering 37

4 int length; length = 7; int width = 12; float voltage, resistance; The assignment operator (equal sign = ) assigns the value of the right-side operand or expression to the variable at the left side. It copies the value to the memory location where the variable is stored. Constants refer to memory locations whose values cannot be changed during the program s execution. For example: const double PI = ; const char COURSE[] = "EEET 2280"; const int MAX_RESULT = 100; Activity 2.2 A Discussion/ Problem solving 1. Which data types would you use for the following variables: A person s age Voltage Number of people on Earth Average Melbourne temperature in a month Student ID Gender 2. Discuss the best suitable names for their identifiers. 3. Declare variables from the above list. EEET 2280 Computing Engineering 38

5 2.3 Operators After you have declared and initialised variables you can manipulate those using operators. All arithmetic operations can be performed in a C++ program using the standard notations, such as + for addition, - for subtraction, * for multiplication, and / for division. If operands are of the same type the result will also be of this type. That is, if int i = 5, and int k = 2, the result i/k is the integer 2. However, the expression i/2.0 will return a double value 2.5, because in this case value in variable i used in this expression will be automatically promoted (or converted) to the default, double value. This does not affect the original value in the variable i which retains an integer type. You can also explicitly convert a data type of a variable or expression using the cast operator (type): expression i/(double)k will return 2.5. NOTE that the cast operator has a higher precedence than arithmetic operators. This means that in the example above k will be converted to a double type before the division occurs. In addition to standard arithmetic operators, C++ defines increment (++) and decrement (- -) operators. The increment operator adds 1 to the variable. The statement x++ is equivalent to x = x+1. This means that 1 is added to the existing value of x, and the new value is assigned to the variable x. Similarly, the decrement operator subtracts 1 from the existing value: y-- is equivalent to y = y-1. These operators are shortcuts which used when a variable is to be incremented or decremented by 1. If other value needs to be added or subtracted the increment and decrement operators are combined with the assignment operator =. For instance, x += 5 will increment the existing value of x by 5; y -= 2.5 will subtract 2.5 from y and re-assign the resulting value to y. In C language this type of operation can be performed by using the arithmetic assignment operator which combines the arithmetic EEET 2280 Computing Engineering 39

6 operation with the assignment operation: x += y. There are five arithmetic assignment operators defined in the table below: Operator Description Example Equivalent Statement += Additional assignment operator -= Subtraction assignment operator *= Multiplication assignment operator /= Division assignment operator %= Remainder assignment operator x+=y; x-=y; x*=y; x/=y; x%=y x=x+y; x=x-y; x=x*y; x=x/y; x=x%y; The increment and decrement operators can be part of an arithmetic or logic expression. In this case they work differently when placed in front of a variable (pre-increment/decrement) or after a variable (post-increment/decrement). For example, in the statement y = x++, the value of x will be assigned to y first, and incremented by 1 next. This is equivalent to two separate statements: y = x; x++; Alternatively, the statement y = ++x will result in the incrementing of x by 1 first, and assigning the new value to y next, which is the same as EEET 2280 Computing Engineering 40

7 x++; y = x; The order of arithmetic operations in C++ follows precedence rules. It is important to understand the precedence rules to obtain a correct output from the expression. Modulus ( or remainder) operator ( % ) can be applied only to integer operands. It returns a remainder of integer division. NOTE, if you divide two integer values using the arithmetic division operator (/) any remainder will be truncated. Thus, the result of 9/2 is 4, and 9%2 is 1. Activity 2.3 A Discussion/ Problem solving 4. Declare a constant for inch as Declare a float variable and initialise it using inch as a constant 6. What is the value of 41/4? What is the value of 41%4? Examine the following fragment of code: int x, y; int current = 72; x = current++; y = ++x; What are the values of the variables x, y, and current after this fragment has been executed? C++ program can interact with the user using the standard input and output operations. In most computers standard input refers to the keyboard, and the standard output refers to the screen. To read information from the keyboard and output it onto the screen, C++ uses the insertion << and extraction >> operators, together with the predefined objects cin (for the standard input) and cout (standard output). EEET 2280 Computing Engineering 41

8 cout << "enter the resistance in ohms\n"; cin >> resistance; cout << "enter the voltage in volts"<<"\n"; cin >> voltage; cout << "current = "<<voltage/resistance<<" amp\n"; In the code fragment above we have used a new-line character \n to start the next line on the screen. In a similar way a special function endl can be used: cout << "enter the resistance in ohms"<<endl; This function is one of the input/output manipulators which will be discussed in detail in the Topic Statements and blocks The end of a statement in C++ is defined by the semicolon. A statement can occupy a small part of a line or be spread over two or more lines. Two or more logically connected statements can be combined in a code block. All statements in a block are enclosed within opening and closing curly braces { }. There is no semicolon at the end of a block; a closing brace } terminates the block. For example, the body of a function is a block. As we will see in Topic 3, a block can also define a target of the for loop or if statement. 2.5 Functions A function is a subprogram which contains one or more statements. A function can perform some actions on data and may return a value. Each C++ program contains at least one main() function. EEET 2280 Computing Engineering 42

9 A function name is used to make a function call. When a function is called from the main() function the program execution transfers to the body of this function, and statements within the function are executed. When the function completes (returns), the program flow resumes from the next line in the main() function after the function call. The use of the IDE s debugger allows you to track the program execution and observe values of the variables when the program control moves to a function. C++ functions can be built-in or user-defined. C++ comprises an extensive set of predefined functions which are organised into separate libraries. This includes functions used in input/output operations, mathematical computations, and string manipulations. Some mathematical functions are sqrt(x)and pow(x,y). Every library has a name and is referred to by a header file. I/O functions are defined in iostream.h header; mathematical functions - in cmath.h header. Preprocessor directives are instructions supplied to the preprocessor. All preprocessor instructions start with # and do not have a semicolon at the end. Thus, a line of code #include <cmath> instructs the preprocessor to explicitly add the cmath.h file to the program code A user-defined function has the following format: Return-type name (parameter-list) { //body of function } In the example below we define a function that displays a student name and ID on onto the screen. This function does not take any parameters and does not return a value. A function prototype is placed before main() function. void signature (); //function prototype EEET 2280 Computing Engineering 43

10 The function definition can be placed prior main(), or after main() function. /* this function displays my name and my student ID on the screen */ void signature () //function definition { cout << "Derek Hudson\n"; //function s body cout << "st.id \n"; } Random number generator Many applications that simulate engineering problems or other real-life situations require introduction of sequences of random numbers. For example, several trials of the same experiment may differ due to unpredictable external conditions, such as change of the temperature in the room or atmospheric pressure; to simulate a realistic radio signal one needs to add a background noise. We will use the C++ standard library function int rand() to generate sequences of random numbers. The use of the rand() function requires including the <cstdlib> header. This function returns an integer in the range between 0 and RAND_MAX where RAND_MAX = 32,767 in most operating systems. Consecutive calls for rand() from the same program will produce a sequence of positive random integers. However, if you run the program again you will get exactly the same sequence of numbers. This happens because each time rand() EEET 2280 Computing Engineering 44

11 generates a pseudo-random sequence using the mathematical algorithm with the same random number seed (the default value is 1). To change the seed we need to use void srand(unsigned int) function (also defined in cstdlib) before calling rand(). The following code fragment generates and displays two sets of three random numbers: srand(100); cout<<rand()<<" "<<rand()<<" "<<rand()<<endl; srand(1250); cout<<rand()<<" "<<rand()<<" "<<rand()<<endl; Each time you run this code you will receive different sequences of random numbers. Computer simulations of engineering solutions in most cases require random sequences in a specific range. To generate a random numbers within a specified range you can use the following: EEET 2280 Computing Engineering 45

12 1. random integer x between 0 and r_max: x = rand()%r_max; 2. random integer x between r_min and r_max: x = rand()%(r_max r_min+1)+r_min; 3. random double y between 0 and 1: y = (double)rand()/rand_max; 4. random double y between r_min and r_max: y = (double)rand()/rand_max*(r_max r_min)+r_min; Activity 2.6 A Discussion/ Problem solving In groups of 3-4 students discuss the four cases of random number generation listed above. Write a line of code that generates a random number in each of the following cases: a positive integer number less than 100 an integer in the rage [5, 20] an integer in the range [-20, +20] including -/+ 20 a floating-point number in the range [1.5, 2.0] a floating-point number in the range [-1.0, +1.0] Write a function int my_rand(unsigned seed, int max_number) that takes a seed and max_number as parameters, sets up a random generator with this seed, and returns a random integer number between 0 and the max_number. Write a complete program that asks a user to enter a seed and max_number from the keyboard, calls the my_rand() function and displays on the screen a random number that has been generated. EEET 2280 Computing Engineering 46

13 2.6 C++ language conventions Commonly accepted programming style that makes a C++ program readable includes the following: meaningful variable/function names where possible. capitalised letters of each word after the first in an identifier (dayofweek), or the use of underscore (day_of_week). one level indentation after each opening brace and return one level back after each closing brace. Some statements require additional indentation. These will be indicated to you later in this course. Activity 2 B Laboratory exercises 1. Following instructions in the reference text Herbert (2004), C++ A Beginner s Guide complete Project 2-1, page 53. Write, compile and run the program. 2. Write, compile and run a program that asks a user to enter a temperature in Fahrenheit, converts it to the temperature in Celsius, and display both values on the screen. Hint: To convert Fahrenheit to Celsius (Centigrade), subtract 32 and divide by 1.8. To convert Celsius (Centigrade) to Fahrenheit, multiply by 1.8 and add 32. Write two functions: one function, float FarToCels (float tempf) takes a temperature in Fahrenheit and returns the temperature in Celsius; and another function float CelsToFar(float tempc) takes a temperature in Celsius, and returns the temperature in Fahrenheit. Write a complete program which calls one function, and then another. Debug the program step-by-step and observe the sequence of execution of the program s statements. EEET 2280 Computing Engineering 47

14 Reading 2 Read Herbert (2004), C++ A Beginner s Guide, pp 38, 43 62, Deitel (2001), C++ How to Program, pp Summary and Outcome Checklist In Topic 2 you have learnt the basic components of a C++ program, and how to declare and use them; you have discovered C++ libraries, and learnt how to write user-defined functions. Tick the box for each statement with which you agree: I can describe basic elements of C++ programming language I can compile a program I can identify and correct errors I can execute a program I can test and debug a program I can describe and use C++ program conventions Assessment This topic will be assessed as part of the following Major Assessment tasks: Practical Test 1 and 2; and the Major project (see Assessment for more detail). This aims to ensure understanding of key concepts prior to undertaking the end of the semester examination. EEET 2280 Computing Engineering 48

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

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

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

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

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers

More information

Objectives. In this chapter, you will:

Objectives. In this chapter, you will: Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates arithmetic expressions Learn about

More information

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

C++ Programming: From Problem Analysis to Program Design, Third Edition C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 2: Basic Elements of C++ Objectives (continued) Become familiar with the use of increment and decrement operators Examine

More information

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

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols. EEE-117 COMPUTER PROGRAMMING Basic Elements of C++ Objectives General Questions Become familiar with the basic components of a C++ program functions, special symbols, and identifiers Data types Arithmetic

More information

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

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements Review: Exam 1 9/20/06 CS150 Introduction to Computer Science 1 1 Your First C++ Program 1 //*********************************************************** 2 // File name: hello.cpp 3 // Author: Shereen Khoja

More information

UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

More information

Chapter 2 Basic Elements of C++

Chapter 2 Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 2-1 Chapter 2 Basic Elements of C++ At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion

More information

The C++ Language. Arizona State University 1

The C++ Language. Arizona State University 1 The C++ Language CSE100 Principles of Programming with C++ (based off Chapter 2 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University

More information

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

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program Chapter 2: Introduction to C++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Parts of a C++ Program Parts of a C++ Program // sample C++ program

More information

Chapter 2: Introduction to C++

Chapter 2: Introduction to C++ Chapter 2: Introduction to C++ Copyright 2010 Pearson Education, Inc. Copyright Publishing as 2010 Pearson Pearson Addison-Wesley Education, Inc. Publishing as Pearson Addison-Wesley 2.1 Parts of a C++

More information

LECTURE 02 INTRODUCTION TO C++

LECTURE 02 INTRODUCTION TO C++ PowerPoint Slides adapted from *Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* Copyright 2012 Pearson Education Inc. COMPUTER PROGRAMMING LECTURE 02 INTRODUCTION

More information

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

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen Chapter 2: Introduction to C++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Copyright 2009 Publishing Pearson as Pearson Education, Addison-Wesley Inc. Publishing as Pearson Addison-Wesley

More information

Computer Programming : C++

Computer Programming : C++ The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2003 Muath i.alnabris Computer Programming : C++ Experiment #1 Basics Contents Structure of a program

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

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

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York CSc 10200! Introduction to Computing Lecture 2-3 Edgardo Molina Fall 2013 City College of New York 1 C++ for Engineers and Scientists Third Edition Chapter 2 Problem Solving Using C++ 2 Objectives In this

More information

Lab # 02. Basic Elements of C++ _ Part1

Lab # 02. Basic Elements of C++ _ Part1 Lab # 02 Basic Elements of C++ _ Part1 Lab Objectives: After performing this lab, the students should be able to: Become familiar with the basic components of a C++ program, including functions, special

More information

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

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 1 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 variables. Apply C++ syntax rules to declare variables, initialize

More information

Lecture 3 Tao Wang 1

Lecture 3 Tao Wang 1 Lecture 3 Tao Wang 1 Objectives In this chapter, you will learn about: Arithmetic operations Variables and declaration statements Program input using the cin object Common programming errors C++ for Engineers

More information

BITG 1233: Introduction to C++

BITG 1233: Introduction to C++ BITG 1233: Introduction to C++ 1 Learning Outcomes At the end of this lecture, you should be able to: Identify basic structure of C++ program (pg 3) Describe the concepts of : Character set. (pg 11) Token

More information

Engineering Problem Solving with C++, Etter/Ingber

Engineering Problem Solving with C++, Etter/Ingber Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs C++, Second Edition, J. Ingber 1 Simple C++ Programs Program Structure Constants and Variables C++ Operators Standard Input

More information

IT 1033: Fundamentals of Programming Data types & variables

IT 1033: Fundamentals of Programming Data types & variables IT 1033: Fundamentals of Programming Data types & variables Budditha Hettige Department of Computer Science Exercise 3.1 Write a C++ program to display the following output. Exercise 3.2 Write a C++ program

More information

Programming. C++ Basics

Programming. C++ Basics Programming C++ Basics Introduction to C++ C is a programming language developed in the 1970s with the UNIX operating system C programs are efficient and portable across different hardware platforms C++

More information

Visual C# Instructor s Manual Table of Contents

Visual C# Instructor s Manual Table of Contents Visual C# 2005 2-1 Chapter 2 Using Data At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion Topics Additional Projects Additional Resources Key Terms

More information

BASIC ELEMENTS OF A COMPUTER PROGRAM

BASIC ELEMENTS OF A COMPUTER PROGRAM BASIC ELEMENTS OF A COMPUTER PROGRAM CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING LOGO Contents 1 Identifier 2 3 Rules for naming and declaring data variables Basic data types 4 Arithmetic operators

More information

CHAPTER 3 BASIC INSTRUCTION OF C++

CHAPTER 3 BASIC INSTRUCTION OF C++ CHAPTER 3 BASIC INSTRUCTION OF C++ MOHD HATTA BIN HJ MOHAMED ALI Computer programming (BFC 20802) Subtopics 2 Parts of a C++ Program Classes and Objects The #include Directive Variables and Literals Identifiers

More information

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 2 : C# Language Basics Lecture Contents 2 The C# language First program Variables and constants Input/output Expressions and casting

More information

Chapter 2: Using Data

Chapter 2: Using Data Chapter 2: Using Data Declaring Variables Constant Cannot be changed after a program is compiled Variable A named location in computer memory that can hold different values at different points in time

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 3 Constants, Variables, Data Types, And Operations Department of Computer Engineering

More information

Creating a C++ Program

Creating a C++ Program 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. 1 Creating a C++ Program created using an

More information

Chapter 2. Outline. Simple C++ Programs

Chapter 2. Outline. Simple C++ Programs Chapter 2 Simple C++ Programs Outline Objectives 1. Building C++ Solutions with IDEs: Dev-cpp, Xcode 2. C++ Program Structure 3. Constant and Variables 4. C++ Operators 5. Standard Input and Output 6.

More information

Data Types & Variables

Data Types & Variables Fundamentals of Programming Data Types & Variables Budditha Hettige Exercise 3.1 Write a C++ program to display the following output. Exercise 3.2 Write a C++ program to calculate and display total amount

More information

Fundamentals of Programming

Fundamentals of Programming Fundamentals of Programming Lecture 3 - Constants, Variables, Data Types, And Operations Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad Outline C Program Data types Variables

More information

CS242 COMPUTER PROGRAMMING

CS242 COMPUTER PROGRAMMING CS242 COMPUTER PROGRAMMING I.Safa a Alawneh Variables Outline 2 Data Type C++ Built-in Data Types o o o o bool Data Type char Data Type int Data Type Floating-Point Data Types Variable Declaration Initializing

More information

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

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords. Chapter 1 File Extensions: Source code (cpp), Object code (obj), and Executable code (exe). Preprocessor processes directives and produces modified source Compiler takes modified source and produces object

More information

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

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING KEY CONCEPTS COMP 10 EXPLORING COMPUTER SCIENCE Lecture 2 Variables, Types, and Programs Problem Definition of task to be performed (by a computer) Algorithm A particular sequence of steps that will solve

More information

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++ CS101: Fundamentals of Computer Programming Dr. Tejada stejada@usc.edu www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++ 10 Stacks of Coins You have 10 stacks with 10 coins each that look and feel

More information

A First Program - Greeting.cpp

A First Program - Greeting.cpp C++ Basics A First Program - Greeting.cpp Preprocessor directives Function named main() indicates start of program // Program: Display greetings #include using namespace std; int main() { cout

More information

Introduction to Programming EC-105. Lecture 2

Introduction to Programming EC-105. Lecture 2 Introduction to Programming EC-105 Lecture 2 Input and Output A data stream is a sequence of data - Typically in the form of characters or numbers An input stream is data for the program to use - Typically

More information

Unit 3. Operators. School of Science and Technology INTRODUCTION

Unit 3. Operators. School of Science and Technology INTRODUCTION INTRODUCTION Operators Unit 3 In the previous units (unit 1 and 2) you have learned about the basics of computer programming, different data types, constants, keywords and basic structure of a C program.

More information

Chapter 1 Introduction to Computers and C++ Programming

Chapter 1 Introduction to Computers and C++ Programming Chapter 1 Introduction to Computers and C++ Programming 1 Outline 1.1 Introduction 1.2 What is a Computer? 1.3 Computer Organization 1.7 History of C and C++ 1.14 Basics of a Typical C++ Environment 1.20

More information

Programming in C++ 5. Integral data types

Programming in C++ 5. Integral data types Programming in C++ 5. Integral data types! Introduction! Type int! Integer multiplication & division! Increment & decrement operators! Associativity & precedence of operators! Some common operators! Long

More information

Chapter 2: Overview of C++

Chapter 2: Overview of C++ Chapter 2: Overview of C++ Problem Solving, Abstraction, and Design using C++ 6e by Frank L. Friedman and Elliot B. Koffman C++ Background Introduced by Bjarne Stroustrup of AT&T s Bell Laboratories in

More information

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation. Lab 4 Functions Introduction: A function : is a collection of statements that are grouped together to perform an operation. The following is its format: type name ( parameter1, parameter2,...) { statements

More information

C: How to Program. Week /Mar/05

C: How to Program. Week /Mar/05 1 C: How to Program Week 2 2007/Mar/05 Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers

More information

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

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char Week 1 Operators, Data Types & I/O Gaddis: Chapters 1, 2, 3 CS 5301 Fall 2016 Jill Seaman Programming A program is a set of instructions that the computer follows to perform a task It must be translated

More information

3. Java - Language Constructs I

3. Java - Language Constructs I Educational Objectives 3. Java - Language Constructs I Names and Identifiers, Variables, Assignments, Constants, Datatypes, Operations, Evaluation of Expressions, Type Conversions You know the basic blocks

More information

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

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1 300580 Programming Fundamentals 3 With C++ Variable Declaration, Evaluation and Assignment 1 Today s Topics Variable declaration Assignment to variables Typecasting Counting Mathematical functions Keyboard

More information

Fundamentals of Programming CS-110. Lecture 2

Fundamentals of Programming CS-110. Lecture 2 Fundamentals of Programming CS-110 Lecture 2 Last Lab // Example program #include using namespace std; int main() { cout

More information

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE a) Mention any 4 characteristic of the object car. Ans name, colour, model number, engine state, power b) What

More information

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C Overview The C Programming Language (with material from Dr. Bin Ren, William & Mary Computer Science) Motivation Hello, world! Basic Data Types Variables Arithmetic Operators Relational Operators Assignments

More information

Topic 1: Programming concepts

Topic 1: Programming concepts Topic 1: Programming concepts Learning Outcomes Upon successful completion of this topic you will be able to: identify stages of a program development implement algorithm design techniques break down a

More information

Add Subtract Multiply Divide

Add Subtract Multiply Divide ARITHMETIC OPERATORS if AND if/else AND while LOOP Order of Operation (Precedence Part 1) Copyright 2014 Dan McElroy Add Subtract Multiply Divide + Add - Subtract * Multiply / Divide = gives the quotient

More information

COMP 202 Java in one week

COMP 202 Java in one week COMP 202 Java in one week... Continued CONTENTS: Return to material from previous lecture At-home programming exercises Please Do Ask Questions It's perfectly normal not to understand everything Most of

More information

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science) The C Programming Language (with material from Dr. Bin Ren, William & Mary Computer Science) 1 Overview Motivation Hello, world! Basic Data Types Variables Arithmetic Operators Relational Operators Assignments

More information

Lecture 2 Tao Wang 1

Lecture 2 Tao Wang 1 Lecture 2 Tao Wang 1 Objectives In this chapter, you will learn about: Modular programs Programming style Data types Arithmetic operations Variables and declaration statements Common programming errors

More information

CSI33 Data Structures

CSI33 Data Structures Outline Department of Mathematics and Computer Science Bronx Community College October 24, 2018 Outline Outline 1 Chapter 8: A C++ Introduction For Python Programmers Expressions and Operator Precedence

More information

Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in:

Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in: CS 215 Fundamentals of Programming II C++ Programming Style Guideline Most of a programmer's efforts are aimed at the development of correct and efficient programs. But the readability of programs is also

More information

Topic 10: Introduction to OO analysis and design

Topic 10: Introduction to OO analysis and design Topic 10: Introduction to OO analysis and design Learning Outcomes Upon successful completion of this topic you will be able to: design classes define class member variables and functions explain public

More information

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

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style 3 2.1 Variables and Assignments Variables and

More information

Chapter 2. C++ Basics

Chapter 2. C++ Basics Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style Slide 2-2 2.1 Variables and Assignments Variables

More information

Homework #3 CS2255 Fall 2012

Homework #3 CS2255 Fall 2012 Homework #3 CS2255 Fall 2012 MULTIPLE CHOICE 1. The, also known as the address operator, returns the memory address of a variable. a. asterisk ( * ) b. ampersand ( & ) c. percent sign (%) d. exclamation

More information

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program? Intro to Programming & C++ Unit 1 Sections 1.1-4 and 2.1-10, 2.12-13, 2.15-17 CS 1428 Spring 2019 Jill Seaman 1.1 Why Program? Computer programmable machine designed to follow instructions Program a set

More information

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

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs. I Internal Examination Sept. 2018 Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs. [I]Very short answer questions (Max 40 words). (5 * 2 = 10) 1. What is

More information

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

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics 1 Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style Slide 2-3 2.1 Variables and Assignments 2

More information

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

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language 1 History C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC

More information

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions Faculty of Engineering Computer Engineering Department Islamic University of Gaza 2013 C++ Programming Language Lab # 6 Functions C++ Programming Language Lab # 6 Functions Objective: To be familiar with

More information

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski) Class 2: Variables and Memory Variables A variable is a value that is stored in memory It can be numeric or a character C++ needs to be told what type it is before it can store it in memory It also needs

More information

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

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable Basic C++ Overview C++ is a version of the older C programming language. This is a language that is used for a wide variety of applications and which has a mature base of compilers and libraries. C++ is

More information

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay C++ Basics Data Processing Course, I. Hrivnacova, IPN Orsay The First Program Comments Function main() Input and Output Namespaces Variables Fundamental Types Operators Control constructs 1 C++ Programming

More information

Review for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure

Review for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure Computer Systems Computer System Computer Structure C++ Environment Imperative vs. object-oriented programming in C++ Input / Output Primitive data types Software Banking System Compiler Music Player Text

More information

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

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1 NAGERCOIL COMPUTER SCIENCE Grade: IX C++ PROGRAMMING 1 C++ 1. Object Oriented Programming OOP is Object Oriented Programming. It was developed to overcome the flaws of the procedural approach to programming.

More information

Basics of Java Programming

Basics of Java Programming Basics of Java Programming Lecture 2 COP 3252 Summer 2017 May 16, 2017 Components of a Java Program statements - A statement is some action or sequence of actions, given as a command in code. A statement

More information

CSCE 110 PROGRAMMING FUNDAMENTALS

CSCE 110 PROGRAMMING FUNDAMENTALS 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

More information

Reserved Words and Identifiers

Reserved Words and Identifiers 1 Programming in C Reserved Words and Identifiers Reserved word Word that has a specific meaning in C Ex: int, return Identifier Word used to name and refer to a data element or object manipulated by the

More information

C/C++ Programming for Engineers: Working with Integer Variables

C/C++ Programming for Engineers: Working with Integer Variables C/C++ Programming for Engineers: Working with Integer Variables John T. Bell Department of Computer Science University of Illinois, Chicago Preview Every good program should begin with a large comment

More information

Chapter 2 - Introduction to C Programming

Chapter 2 - Introduction to C Programming Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers 2.4 Memory Concepts 2.5 Arithmetic

More information

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

PART I.   Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++. Unit - III CHAPTER - 9 INTRODUCTION TO C++ Choose the correct answer. PART I 1. Who developed C++? (a) Charles Babbage (b) Bjarne Stroustrup (c) Bill Gates (d) Sundar Pichai 2. What was the original name

More information

REVIEW. The C++ Programming Language. CS 151 Review #2

REVIEW. The C++ Programming Language. CS 151 Review #2 REVIEW The C++ Programming Language Computer programming courses generally concentrate on program design that can be applied to any number of programming languages on the market. It is imperative, however,

More information

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018 C++ Basics Lecture 2 COP 3014 Spring 2018 January 8, 2018 Structure of a C++ Program Sequence of statements, typically grouped into functions. function: a subprogram. a section of a program performing

More information

CS102: Variables and Expressions

CS102: Variables and Expressions CS102: Variables and Expressions The topic of variables is one of the most important in C or any other high-level programming language. We will start with a simple example: int x; printf("the value of

More information

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java Chapter 3 Syntax, Errors, and Debugging Objectives Construct and use numeric and string literals. Name and use variables and constants. Create arithmetic expressions. Understand the precedence of different

More information

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types Data Types Declarations and Initializations Larry Caretto Computer Science 16 Computing in Engineering and Science February 7, 25 Outline Review last week Meaning of data types Integer data types have

More information

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

Introduction to C++ Programming Pearson Education, Inc. All rights reserved. 1 2 Introduction to C++ Programming 2 What s in a name? that which we call a rose By any other name would smell as sweet. William Shakespeare When faced with a decision, I always ask, What would be the

More information

Chapter 3 - Functions

Chapter 3 - Functions Chapter 3 - Functions 1 Outline 3.1 Introduction 3.2 Program Components in C++ 3.3 Math Library Functions 3.4 Functions 3.5 Function Definitions 3.6 Function Prototypes 3.7 Header Files 3.8 Random Number

More information

Introduction to Programming using C++

Introduction to Programming using C++ Introduction to Programming using C++ Lecture One: Getting Started Carl Gwilliam gwilliam@hep.ph.liv.ac.uk http://hep.ph.liv.ac.uk/~gwilliam/cppcourse Course Prerequisites What you should already know

More information

Systems and Principles Unit Syllabus

Systems and Principles Unit Syllabus Systems and Principles Unit Syllabus Level 2 Creating an object oriented computer program using C++ 7540-004 www.cityandguilds.com October 2010 Version 2.0. About City & Guilds City & Guilds is the UK

More information

Chapter 3. Numeric Types, Expressions, and Output

Chapter 3. Numeric Types, Expressions, and Output Chapter 3 Numeric Types, Expressions, and Output 1 Chapter 3 Topics Constants of Type int and float Evaluating Arithmetic Expressions Implicit Type Coercion and Explicit Type Conversion Calling a Value-Returning

More information

Chapter 1 INTRODUCTION

Chapter 1 INTRODUCTION Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware consists of the physical components of the system. The software is the collection of programs that a computer

More information

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

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3 Programming - 1 Computer Science Department 011COMP-3 لغة البرمجة 1 011 عال- 3 لطالب كلية الحاسب اآللي ونظم المعلومات 1 1.1 Machine Language A computer programming language which has binary instructions

More information

Function Call Stack and Activation Records

Function Call Stack and Activation Records 71 Function Call Stack and Activation Records To understand how C performs function calls, we first need to consider a data structure (i.e., collection of related data items) known as a stack. Students

More information

Computer System and programming in C

Computer System and programming in C 1 Basic Data Types Integral Types Integers are stored in various sizes. They can be signed or unsigned. Example Suppose an integer is represented by a byte (8 bits). Leftmost bit is sign bit. If the sign

More information

Program Fundamentals

Program Fundamentals Program Fundamentals /* HelloWorld.java * The classic Hello, world! program */ class HelloWorld { public static void main (String[ ] args) { System.out.println( Hello, world! ); } } /* HelloWorld.java

More information

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types

More information

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2 Numerical Computing in C and C++ Jamie Griffin Semester A 2017 Lecture 2 Visual Studio in QM PC rooms Microsoft Visual Studio Community 2015. Bancroft Building 1.15a; Queen s W207, EB7; Engineering W128.D.

More information

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018 CSE 1001 Fundamentals of Software Development 1 Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018 Identifiers, Variables and Data Types Reserved Words Identifiers in C Variables and Values

More information

Systems and Principles Unit Syllabus

Systems and Principles Unit Syllabus Systems and Principles Unit Syllabus Level 2 Creating an event driven computer program using Java 7540-007 www.cityandguilds.com October 2010 Version 2.0 About City & Guilds City & Guilds is the UK s leading

More information

Introduction of C++ OOP forces us to think in terms of object and the interaction between objects.

Introduction of C++ OOP forces us to think in terms of object and the interaction between objects. Introduction of C++ Object Oriented Programming :- An Object Oriented Programming (OOP) is a modular approach which allows the data to be applied within the stipulated area. It also provides reusability

More information

JAVA Programming Fundamentals

JAVA Programming Fundamentals Chapter 4 JAVA Programming Fundamentals By: Deepak Bhinde PGT Comp.Sc. JAVA character set Character set is a set of valid characters that a language can recognize. It may be any letter, digit or any symbol

More information