201Spring08-L5 - CMPSC 201C Spring 2008 Lecture 5 January...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CMPSC 201C Spring 2008 Lecture 5 January 25, 2008 Input C++ utilizes the cin object and extraction operator to read data from keyboard and store in memory locations. cin>> variablename; Variable must be declared before it can be used. Input stops when white space is encountered. Can enter multiple values by separated by extraction operators. cin>>x>>y; //data is stored in order given Usually users are prompted by a cout statement to enter data. Examples cout << "Please enter the radius of the sphere.\n"; cin >> radius; // must be declared prior cout<<"Please enter length and width.\n"; cin >> length >>width; // must be declared prior ----or---cin>>length; cin >>width; Caution cin does not confirm data type. Given the following code segment int alpha; cout<<"Please enter a number.\n"; cin>>alpha; What would happen if the user entered a floating point number or a character? Try it some time! Questions???? Arithmetic Operators Used to manipulate numeric values and perform arithmetic operations. Classified as unary, binary, or ternary based on the number of operands needed. Unary - requires one operand, positive or negative of a number. -a Binary - requires two operands. X+Y c/b Ternary operators requires three operands. (Will discuss later). Binary Arithmetic Operators + Addition sum = y + x - Subtraction diff = x - y * Multiplication numb = c * b / Division rate = part / total % Modulus remainder = d % 4 Integers operated on by integers result in an integer Floating points operated on by integers or floats result in floating points Precedence Defines the order in which operations are performed. For arithmetic operations: Operations in parentheses are performed first Unary negation * / % multiplication, division, modulus + addition and subtraction Operations of equal precedence are performed in order they are written from left to right. Number Representation When using a literal constant in a arithmetic expression, a number without a decimal portion is assumed to be an integer. A number with a decimal even if it is 0 is a floating point number. 4 is an integer 4.0 is a floating point number (double) Caution Integer division usually results in an integer that has been truncated from a floating number. A literal number without a decimal point is considered an integer by default. Consider the following: double alpha, beta; // declares 2 variables alpha = 4 / 3; 1.0 would be stored in alpha beta = 5 / 3; 1.0 would be stored in beta A floating point number stored in an integer variable will be truncated, not rounded! Changing either numerator or denominator to a double would change the result. Test Your Knowledge What would be the result of the following arithmetic expressions (both data type and value)? 1*2 1/2 1.0/2 1/2*5.0 5.0*1/2 5/9*4.5 + 32 Questions ??? Assignment Statements Assignment statements are used to store values in the memory locations. Use the assignment operator "=". radius = 5; // 5 is a literal constant; Named constants must be assigned at the time they are declared. Assignments to variables can occur at the same time as declaration or after. int numberincase = 12; Values stores in constants cannot be changed. Caution Remember that assignment statements are not the same as equations in algebra. The left and right side cannot be interchanged! X = 5; is not the same as 5 = X; The assignment statement evaluates what is on the right hand side of the operator and then stores it in the variable that is on the left hand side. The left hand side must be a single variable. Storing "Wrong" Data Types Suppose your declare an integer variable int var1; then try to store a floating point number in this variable var1 = 4.57; // by default 4.57 is a double A syntax error may occur with some compilers. However, with many compilers the data would just be modified to conform with data type of the variable. The value 4.57 will be truncated to the integer 4 to store. Assignment Statements Used to store data in memory locations. Are NOT equations Use the = operator. Must have a single variable (or at time of declaration a constant) on the left hand side. Right hand side may be a variable, arithmetic expression, function call, etc. alpha = 5 * 6.0; //valid 5 * 6.0 = alpha; //not valid Caution Be careful of integer division! Suppose alpha has already been declared alpha = 4 / 3 * 5.0 ; yields a different result than alpha = 4 * 5.0 / 3; Multiple Assignments A single assignment statement may be used to store the same value in multiple variables at one time. x = y = h = d = k = 4.0; Could be used to replace the statements x = 4.0; y = 4.0; d = 4.0; h = 4.0; k = 4.0; Questions ??? ...
View Full Document

This note was uploaded on 03/19/2008 for the course CMPSC 201 taught by Professor Susanquick during the Spring '08 term at Pennsylvania State University, University Park.

Ask a homework question - tutors are online