CS120 Week 02: Variable Initialization and Data Types, Study notes of Computer Science

A series of slides from the university of virginia (uva) cs120 course, covering topics such as variable initialization, output, input, types, and arithmetic operators. It explains the concept of setting variables before using them, the use of the insertion operator for output, and the difference between integer and real types. It also discusses the rules for numerical literals and character variables, as well as type compatibilities and mixed arithmetic.

Typology: Study notes

Pre 2010

Uploaded on 07/29/2009

koofers-user-5dn-1
koofers-user-5dn-1 🇺🇸

5

(1)

10 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS120: week 02 slide(1)
DEPARTMENT OF COMPUTER SCIENCE
UVA
Variable Initialization
A variable has no meaningful value unless assigned
#include <iostream.h>
int main()
{
int i;
int j;
int b;
j = i + 1; // <- problem
cout << b; // <- problem
return 0;
}
Rule: Set each variable before its value is used!
GARBAGE, otherwise!
(The container has no predictable value)
One way of avoiding uninitialized variables:
initialize at the time of declaration
int your_sum = 20;
double rate = 0.1, balance = 0.00;
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download CS120 Week 02: Variable Initialization and Data Types and more Study notes Computer Science in PDF only on Docsity!

UVA

Variable Initialization

  • A variable has no meaningful value unless assigned #include <iostream.h> int main() { int i; int j; int b; j = i + 1; // <- problem cout << b; // <- problem return 0; }
  • Rule: Set each variable before its value is used! GARBAGE, otherwise! (The container has no predictable value)
  • One way of avoiding uninitialized variables: initialize at the time of declaration int your_sum = 20; double rate = 0.1, balance = 0.00;

UVA

Output

cout << "Hello, World" ; cout << "Enter the Number:\n" ;

Syntax cout << exp 1 << exp 2 << ... << exp (^) n;

Note

  • << is the insertion operator

Semantics

  • Expressions exp 1 through expn are displayed to screen Expressions are normally variables and literals Newline
  • Just another character Symbolic representation: \n Alternative version endl (p. 49)

UVA

Input

Syntax cin >> var 1 >> var 2 >> ... >> var (^) n ;

Semantics

  • Variables var 1 through var (^) n are assigned the n values from standard input var 1 first
  • Input values are separated by whitespace (space, tab, CR)

Examples cin >> fahrenheit; cin >> x_coordinate >> y_coordinate; BETTER: Issue a prompt for input. cout << "Current temperature? "; cin >> fahrenheit;

UVA

Types

Literals and variables have types

  • Literal: compiler can figure out type e.g. "This is a string" is a string e.g. 243 ----- compiler knows it is an int literal
  • Variable: must tell compiler type Done through declaration Helps compiler determine efficient storage Helps ensure type safety (advanced topic)
  • Remember, literals can be numeric, character or string
  • Variables can be any basic or user-defined type

Basic types of variables:

  • character ( char )
  • numeric (See table in textbook p. 57 [ int range is wrong!!]) focus on int and double for now

UVA

Numeric Types

5, -3.14159, 3.125267E+3, 2147483648

Why so many?

  • e.g. int, long int, float, double, long double
  • efficiency vs convenience
  • int ’s much more efficient than long ints
  • but, long ints can hold bigger integer values
  • similar for float , double , long double

What to do?

  • learn about int and double for now
  • but recognize all of the numeric types in text (p. 57)

Beware!

  • Don’t count on range specified in book. Why?

UVA

Numerical Literals: Rules

Common sense, for the most part

  • If you've used a calculator, you know most of the rules

Integers ( int , long , ..)

  • Use only digits 0..9 (and + or - or nothing) 31 -1729 32767
  • Beware of size limitations --- in Borland C++ the values which can be represented in a variable of type int (16 bits) range from -32768 to +32767 (again: error in Savitch, p. 57)

Reals ( float , double ...)

  • Digits and embedded decimal point (and sign)
  • 9.323 0.179 -2.0 91700.
  • Scientific notation
  • 9.17E+
  • Beware the common comma and $ errors
  • Don’t use "," or "$" in numeric literals
  • Data should be, e.g. 1729.56, not $1,729.

UVA

Characters and Strings

Remember:

  • Character literals: ’A’ ’5’ ’?’ ’\n’
  • Character variables: char first; char last; first = ’P’; last = ’W’;
  • String literals: "Balance due: " "1011 High Street"
  • String variables? — No

UVA

Type Compatibilities

General rule: you cannot store a value of one type in a variable of a different type --- type mismatch - convert if possible int distance, rate; distance = 33.9; - stores 33 rate = 2.0; - stores 2

  • assigning int value to double variable double growth_rate; growth_rate = 2; growth_rate will be assigned 2.

Textbook error: bottom of page 59 - what he says is wrong. One can always assign a real value to an integer variable - it will always be truncated

  • Need to be careful if you mix types and values on assignment statements or arithmetic expressions
    • especially when performing arithmetic operations

UVA

Division

Need to recognize the very distinct difference between division of two real values and the division of two integer values.

Remember the rule: in A op B if any of the operands are real, the result is real. Putting this another way, the integer operand value is first converted to an equivalent real value, and then the opera- tion (+, -, *, or /) is performed.

so, if we have 9.0/2.0, we obviously get 4.50000 as the result

if we have 9.0/2, then 2 is converted to 2.0000, gives 4.

however, if we have: 9/2, then neither operand is real, so we have to do INTEGER DIVISION --- result is 4 (with a remainder of 1)

The operator ’%’ (called "modulus") yields the remainder after division:

9/4 has value 2 (with remainder of 1) 9%4 has value 1 (the remainder after 9/4)

10/4 has value 2 10%4 has value 2

11/4 has value 2 11%4 has value 3 Trick question: value of val*(1/4)?

-11%4 has value -3.........

UVA

Formatting Numbers for Output

double price; price = 78.5; cout << "The price is $" ; cout << price << endl;

Output The price is $78. The price is $78.500000 WHICH????? The price is $7.8500000e

Output we want The price is $78.

  • Need to tell the computer how to format the numbers

Magic formula (textbook p.51) cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); Output The price is $78.

UVA

Arithmetic Operators

Common

  • Addition +
  • Subtraction -
  • Multiplication *
  • Division / — overloaded
  • Modulo %

Note

  • No exponentiation operator
  • Single division operator

Precedence Rules

  • Determines the order in which operators are performed
  • Similar to the ones used in mathemathics and algebra
  • A complete list is in Appendix 2 of the textbook (p. 771)
  • Order can be changed by inserting parentheses () (x + y) * z