+ All Categories
Home > Documents > Libraries Programs that other people write that help you. #include // enables C++ #include //...

Libraries Programs that other people write that help you. #include // enables C++ #include //...

Date post: 21-Dec-2015
Category:
View: 215 times
Download: 0 times
Share this document with a friend
36
Libraries Programs that other people write that help you. #include <iostream> // enables C++ #include <string> // enables human-readable text #include <math.h> // enables math functions using namespace std; // enables cout<< and cin>>
Transcript

Libraries

Programs that other people write that help you.#include <iostream> // enables C++#include <string> // enables human-readable text#include <math.h> // enables math functionsusing namespace std; // enables cout<< and cin>>

Keywords vs. myWords

• Keywords are C++ defined:– int– main– char– double– while– cin– cout– many more

• variables, file names are your choice

• Convention:– lowerUpper

cout - decimal precision

• cout only displays 6 decimal characters• cout attempts to reduce output to save characters

• use "cout.precision( n ); " to set displayable values to n decimal characters

Scientific notation

Try this:double x;x = 35.62e6;cout.scientific;cout << x << endl;

cout.fixed;//eliminates scientific notationcout.precision(12);cout << x << endl;

The Assignment Statement “=”int main ( ) { int A; A = 4; A = A + 3; }

NOT “A equals 4”instead: “A is assigned the value 4”

semicolon, brackets, main… must be C++

Operators

The usual: + - / *

Precedence: use ( ) e.g. (y + b) * c not the same as y + b * c

Negative numbers: -8

Announcements

Please read all of Chapter 1 in the text• comments• operators• variable types• libraries• if statement and comparisons

Variables

Variables are used to set and change values, which represent quantities,

text, or states.

How do we use numbers?• Counting• Measuring• Non-numerical designations:

• Finally, they can designate a “state” unrelated to the number (“1 if by land, 2 if by sea”)

So computers

• Must deal with each type of number in it’s own way

• Must store those numbers in their own way• Must make those numbers available, though,

through a common interface• That interface, is the “VARIABLE”

How computers use info

• Numbers!• Everything is a number!• The letter “A” is stored as the number 65• The letter “B” is stored as the number 66• The color RED is stored in three numbers as

255 0 0

Variables

• x, y, z• i, j, k• totalCountOfNewEmployees• any_sensible_name // usually for what

// the variable holds

They Stand for Something, can be named anything except reserved C++ words

Variable Types (primitive, common)• Integers – meant to be a whole number (used for

counting) C++: int• Floating Pt. – meant to be a detailed number (used

for measuring) – called a double• Character – has no meaning other than to be read

by a human – C++: char• Strings of Characters – have no meaning other than

to be read by a human: string– Not native to C++, but libraries exist to help…

• Boolean – reflects a state of true or false – C++: bool

"x is 14"

• Could mean 14 • Could mean 13.999995• Could mean 14.000001• Could mean 20 (honestly), might not be base-

10, more later• Could mean “Fourteen”, which has no

meaning other than to be read by humans

“Typing” a variable – clarifies what it does and its precision

int x;x = 14;Therefore, 14 means 14

double x; x = 14.0000;Therefore, x has meaning, 14 to 4 places

note

• Should be initialized at the declaration

int x = 14; // type name = initValue;

• "typed" = declared• Can only be “typed” once

• Value can be reset (that’s why it’s called a “variable”).

floating point (i.e. double)?

• 14.6567• 128.1231• 154367.001423• 3.14159• 987987465986543.• “adequate precision”• cout rounds numbers for space

int x = 45678.34342;cout << x;

will DISPLAY as 45678.3, although x will retain its value for calculations

once a variable is designedyou can get it from the operator (if you want)

int x = 1000000;x=0;cout << “enter x: ”;cin >> x;

chars and strings#include <iostream>#include <string>using namespace std;char Id = 'm'; // chars are single-quotedstring name = "mikeb"; // strings are double-

quotesint main( )

{ cout << Id << endl; cout << name << endl; }

New Types

Boolean – true or false (really means presence or absence), used to signify a condition, or “state” of being.

the “bool” type can have values true or false (note: WITHIN the computer, false is zero, and true is any non-zero value).

operations are OR and AND

bool X, Y, Z;

X = true; (same as = 1, means “X is present”)Y = false; (same as = 0, means “Y is NOT present”)

Z = X OR Y; (what is Z?, i.e. does Z signify some presence?)

Z = X AND Y; (what is Z?)

George Boole

said that all life could be explained as a combination of things that were present or absent under certain conditions. Really, just a way of describing things with simple math symbols.

A smoke, water, burglar alarm

Boolean variables W, B, S, AW is true if waterB is true if burglarS is true if smokeA means “sound the alarm”

A = W and B and SDoes W and B and S make sense?

A = W or B or S

The "if..else" statementif ( a == b ) { // do this }else if ( a < b ) { // do this }else { // do that }

conditionals

"logical" comparisons

if ( a == b )

if ( a != b )

if ( a < b )

if ( a > b ) if ( a <= b )

if ( a >= b )

if demo#include <iostream>

a simple menu

#include <iostream>using namespace std;int main(){bool quit = false;int choice = 0;

while ( quit == false ) { cout << "0. quit" << endl; cout << "1. keep looping" << endl; cout << "enter a choice: " << endl; cin >> choice; if (choice == 0) { quit = true; } else { cout << "loop again..." << endl; } } // end while

return 0; } // end main

Loop (iteration)

conditional

Legality

Variable value and variable type must be the same.

Deterministic behavior - All computers yield the same result with the same variables

Computers DO NOT completely check our programs

• Called “Compilation”• Must be successful before a program can

“run”• But not all errors are caught in compilation

So, Illegality is...

• “Compiler” Errors – however, some languages and compilers allow mis-designations of variables

• Indeterminate results• “Run-time” Errors - only show up when you

run the program

Illegal #1 – mis-defined variablesint y;y = 14.0001;

char q;q = "abcdef";

double z;z = "hello";

Why “Type” Variables

• Deterministic behavior of variables and the numbers they represent.

• New definition of “Legality” : We will never perform mathematical or logical operations on variables of different types.

Illegal #2 – mixing variables in same equation

int x;double y;char z;x = 12;y = 16.001;z = x + y;

Illegal #3 - Operation wrong for variable type:

char Q, R, S;Q = R / S; // remember "/" is divide


Recommended