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

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

Programming. C++ Basics

Lecture 3 Tao Wang 1

Add Subtract Multiply Divide

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

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

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

I SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK a) What is the difference between Hardware and Software? Give one example for each.

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

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

UNIT- 3 Introduction to C++

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

Reviewing all Topics this term

Outline. Review Choice Statements. Review Sequential Flow. Review Choice Before Loops. Review Choice After Loops

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

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

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

These are reserved words of the C language. For example int, float, if, else, for, while etc.

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

Creating a C++ Program

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

Eng. Mohammed S. Abdualal


Introduction to the C++ Programming Language

VARIABLES & ASSIGNMENTS

Chapter 7 Arithmetic

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

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

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

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Chapter 2: Overview of C++

WARM UP LESSONS BARE BASICS

Topic 2: C++ Programming fundamentals

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

Computer Programming CS F111

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

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.

Chapter 1 Introduction to Computers and C++ Programming

A First Program - Greeting.cpp

Programming Language. Functions. Eng. Anis Nazer First Semester

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

Computer System and programming in C

LAB 4.1 Relational Operators and the if Statement

Chapter 2 C++ Fundamentals

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

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

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

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

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

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.

Programming for Engineers Iteration

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

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

Lecture 3. Input and Output. Review from last week. Variable - place to store data in memory. identified by a name should be meaningful Has a type-

BITG 1233: Introduction to C++

CS242 COMPUTER PROGRAMMING

Discussion 1H Notes (Week 2, 4/8) TA: Brian Choi Section Webpage:

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

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

CEN 414 Java Programming

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

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

CSCI 111 Midterm 1, version A Exam Fall Solutions 09.00am 09.50am, Tuesday, October 13, 2015

IT 1033: Fundamentals of Programming Data types & variables

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

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

Objectives. In this chapter, you will:

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

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

C: How to Program. Week /Mar/05

>>> * *(25**0.16) *10*(25**0.16)

From Pseudcode Algorithms directly to C++ programs

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Ex: If you use a program to record sales, you will want to remember data:

CIS 110: Introduction to Computer Programming

1. In C++, reserved words are the same as predefined identifiers. a. True

CS Programming I: Primitives and Expressions

Engineering Problem Solving with C++, Etter/Ingber

The Hyderabad Public School, Begumpet, Hyderabad, A.P

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

Chapter 2: Introduction to C++

Expressions, Input, Output and Data Type Conversions

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

Chapter 2. C++ Basics

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

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

Data types Expressions Variables Assignment. COMP1400 Week 2

C/C++ Programming Lecture 7 Name:

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Dr. Khalil Exam II Fall 2011

Chapter 2. Outline. Simple C++ Programs

int x = 5; double y = 3; // Integer division rounds the result down to the nearest whole number. cout << "1a: " << x / 3 << endl; //1

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

Arrays in C++ Instructor: Andy Abreu

Transcription:

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 a name the identifier so that it can be referred to

Numeric Types int num_apples = 3; double pi = 3.14159; A numeric type can be either an integer type (int) or a floating point type (double). There are other integer and floating point types, but int and double are the most commonly used.

Declaration of Variables Syntax: Examples type identifier; type identifier = value; type identifier1, identifier2, etc. int grade1; int grade2 = 93; int grade3 = 92, grade4, grade5 = 71;

What the declaration does Tells the computer what kind of variable you are creating It gets placed at the bottom of the memory stack

Identifiers Identifiers must be composed of letters, digits and underscores. They cannot begin with a digit They cannot be reserved words in C++ Style: Identifiers should be meaningful, self-documenting Example: int abcde int 1abcde int abcde1 int abc_de int abc-de int double; valid invalid valid valid invalid invalid

Example A program is to calculate the wind chill for a temperature of 72.5 F and a wind speed of 20 mph. The formula for wind chill is as follows (where T is the temperature and V is the wind speed): 35.74 + 0.6215T 35.75V 0.16 + 0.4275TV 0.16

Windchill Program

Observations 1. The values for wind speed and temperature are hard coded in the program. It would be an improvement if they could be entered by the user. 2. C++ has libraries of useful functions like pow. The header file which declares the function must be #included at the top of the program. 3. To display the value of a variable to the screen, use cout << variablename << endl

Standard Input and Output int num_trees; cout << enter number of trees << endl; cin >> num_trees; cout << The user has entered << num_trees << endl;

Windchill program: data taken from user

The assignment operator (=): changing the value of a variable In math, we use = in two ways as an action: let n = 2 as a claim: if n = 3, then... In C++, there are two different operators for this Assignment: n = 2; Equality: if ( n == 3 )

Example: what happens on the memory stack int i = 1; int j; j = i; i = 2*j; j = i + 1; i = 3*i;

Floating point versus integer arithmetic Mod operator: i % j = the remainder of i when divided by j they must be integer types Ex: 17%2 1 Division operator: i/j gives the value of i divided by j if both are integer types, the result is an integer, which the fractional part truncated if either is a floating point type, the result is also a floating point number Ex: 17/2 8 17.0/2 8.5 17/2.0 8.5 17.0/2.0 8.5

Example Write a program that gets a 5 digit number from the user. If the number is not a 5 digit number, it exits with a message telling the user. If it is a 5 digit number, it displays the sum of all the digits. New C++ needed: if (A B) { } else { } exit(-1); a conditional statement, means or exit the program, -1 indicates an error has occurred.

Type casting a double can be cast to an int, or an int to a double Examples: int(2.5) 2 double(2) 2.0 2/5 0 double(2)/5 0.4 2.5 int(2.5) 0.5

Assignment #1 a. Write a program that gets the month (as a value between 1 and 12) and the day of the month from the user for two dates (in the same year). If the month or day is not valid, it gives a message to the user and exits. Otherwise, it displays a message, either that both dates are the same day of the week, or that they are different days of the week. Your program should work for non leap years.