csc 107 – programming for science. announcements

38
LECTURE 3: ACTUAL ASSIGNMENTS CSC 107 – Programming For Science

Upload: maximillian-norman

Post on 13-Dec-2015

223 views

Category:

Documents


1 download

TRANSCRIPT

LECTURE 3:ACTUAL ASSIGNMENTS

CSC 107 – Programming For Science

Announcements

Memorization is not important, but… … you will all still be responsible for

information Instead use your resources: notes, books,

slides, etc. Instructions from lab #1 correct; I

messed up While not required, bringing thumb drive

helpful During the day, tutors available in WTC

206/208 Weekly assignment #2 available now

on Angel

No lecture on Thursday – I will not be here

The Week’s Goal

At end of today’s lecture, you will be able to

Write (small, useless) C++ programs

Variable Declarations

Variables must be declared before can be used Way of getting computer to make space for

variable States how to interpret memory in future

uses Allows the compiler to check if uses are

legal

Variables, Constants, & More

General Cases Examples

Variable DeclarationdataType name;dataType name = value;dataType name, anotherName;dataType name = value, anotherName;

int count;bool monkey = true;char help,letter;char a=‘a’,letter;

Constant Declarationconst dataType name = value; const double PI=3.1;

Symbolic Constant #define NAME value #define AGE 34

Data Types

Each variable also has data type How program treats variable’s value

defined by this Single true or false value held by bool C/C++ defines 7 numeric data types

Integer types: short, int, long, long long Decimal types: float, double, long double

char data type used to store a character

Variable Names

Begin with letter or underscore (_) Then use any letters, numbers, or

underscore Unique name* needed for each variable

Computer wouldn't know which of 1,000 bobs to use

Reserved words are… reserved and can't be used Includes all type names on p. 83 of book void, unsigned, class also reserved words

Program Outline

Once upon a time… … some stuff happens… … and they all lived happily ever after

Program Outline

Once upon a time… All programs must begin somewhere Defines what is worked upon during rest of

program For non-trivial programs, requires receiving

input

When starting program, first steps always same:1. What is the input?2. What will the input look like?3. How will the data be entered?

Reading From The Keyboard

Easiest to get input from the keyboard Reading from files possible; discussed later

in term C++ lacks standard, so writing GUI much

harder

C++ defines cin to get user’s input As easy to use as delivering food to

Granny’s house When cin hit, program waits until it has

input User must press enter for line to be able to

be read Editing not seen by program; only receives

final line

Programming Using cin

Used to read one or more values at once:

cin >> variable;cin >> variable1 >> variable2;

Reads where last cin stopped reading input Automatically skips past whitespace

Data type of variable determines what is read Stops reading at first non-usable value in

input If input is not usable, will set variable equal

to 0

cin Example

>>

>>

>>

cin Example

>>

>>

>>

cin Example

>>

>>

>>

Program Outline

Once upon a time… Get the input using cin from the keyboard Can use cin any time, but usually precedes

cool stuff

Program Outline

Once upon a time… … some stuff happens…

Want to do something with variable declared

C++ has large variety of statements to do this

First statement covered in today’s lecture

Program Outline

Once upon a time… … some stuff happens…

Want to do something with variable declared

C++ has large variety of statements to do this

First statement covered in today’s lecture

Assignments

Assignments

Variable declaration creates “box” to store data Box can get values placed in it using

assignments General form of assignment is

variable = expression; Computer works by first evaluating

expression Single value must result from this

expression Value of variable set to this result

What Is The Expression?

Simplest expressions are literal values

Examples:double d;int i;char doe;i = 6;i = 7;d = -7;d = 34.5691;doe = ‘a’;doe = ‘0’;

12 56 12.345 -56 ‘a’

What Is The Expression?

Examples of other simple expressionsdouble d;int i;char doe; i = 6;i = 7;d = -i;i = d;d = 34.5691;i = d;doe = ‘0’; // 0 == ASCII 48i = doe;doe = i;

Data Types

Assignments are legal only if always safe C++ defines ordering of legal assignments

long doubledoublefloatlongintshortchar

Lega

l to

assi

gn to

hig

her

type

What Is The Expression?

Can also include basic arithmetic operators Addition + i = 4 + 6; Subtraction - d = i – 2.3; Multiplication * i = 120 * 8; Division / d = 4.0 / i; Modulus % i = 120 % 8;

Modulus computes remainder between two integers:4 % 5 equals 45 % 4 equals 19 % 3 equals 012823 % 812 equals 643

Tracing A Program

Important for understanding & debugging code Step-by-step execution of program shown To see what is happening, done via pencil-

and-paper Execute each line of program like

computer does Within trace, add row whenever variable

declared Update variable’s value each time it is

assigned Off to side, show any output from cout

statements

Program Trace

1 int x = 4 + 2;

2 int y = 8 * 1;

3 double z = y – 3;

4 x = x + 1;

5 y = 7 % x;

6 z = y + 1.0 / 2.0;

7 z = 8.0 / 4 + x * x;

8 y = (x – 3) * (y + 2);

9 y = x / 4;

10 cout << x << “ ” << y << “ ” << z << endl;

Integer Division

Dividing two integers computes an integer Literals or variables does not matter, only

their type Important to remember, 12 is integer & 12.0 is not

C++ ignores result after decimal to get integer2 / 5 equals 0 (the .4 was thrown away) 5 / 2 equals 2 (the .5 was thrown away)16 / 4 equals 4 -5 / 2 equals -2 (the .5 was thrown away)2.0 / 5 equals 0.4 (2.0 is not an integer!)

Floating Point Arithmetic

Operations using decimal has decimal result Even if whole number is result of the

operation For example, all the assignments to i are

illegal:int i;double d = i;i = 6.0 / 3.0;i = 2.0 * d;i = d + 1;i = 4 * 2.0;i = (d * 1) + 5;i = 8 + (9 * 3) – (2 / 1.0) * 4 + 2;

Priority of Operations

Equations can become very complex 4 + 5 * 6 * 9 - 2 + 1 = …?

Very strict order of operations used by computer ( ) Solve from inner- to

outermost + (positive) & - (negative) Solve from right to left * & % & / (division) Solve from left to right + (addition) & - (subtraction) Solve from left to

right

My suggestion: use lots of parentheses

Compound Assignment Operators Short simple operators that allow us to

be lazy Save some typing for several common

actions Lowest priority operation; expression

evaluated first

Operator Equivalent C++ Expression

a += 2; a = a + 2;b -= d; b = b – d;c *= 4 + 5.6; c = c * (4 + 5.6);d /= 0.3 * e; d = d / (0.3 * e);

How To Shoot Yourself in Foot Also increment (++) & decrement (--)

operators Use with variables only; no exceptions

possible Used anywhere to save typing an additional

line Two different ways these operators

applied

v = ++b % c; b = b + 1;v = b % c;

c = f * --h; h = h – 1;C = f * h;

a = b++ * c; a = b * c;b = b + 1;

Program Outline

… and they all lived happily ever after All good processing comes to end & report

results Shows program worked and provides

feedback Results takes many forms, focus on printing

today

When starting program, last part asks:1. What must be output?2. How can output be presented best?3. Will it be pretty?

Using cout to Print

Already seen how to print text using cout

cout << “Hello World” << endl; Prints out whatever is placed between

quotes endl goes to next line and prints out

immediately

Use escape sequences for fancier text output\n newline (move to start of next line)\t tab (go to next column that is multiple of 8)\\ \ (backslash character)\” “ (quotation mark)

Can Print Out Multiple Items cout can also print out value of

variablesint bob = 2;double j = 4.5;char var = ‘a’;cout << “Hello ”;cout << bob << endl;cout << j << endl;cout << var << endl;cout << j << “ is not ” << bob << endl;cout << bob << “ equals bob” << endl;cout << var << bob << j << endl;

But Can It Be Used?

cout built to provide basics needed to work Prints out as many digits as needed No extra spaces or tabs used Scientific notation cannot be used

Often want to format results Significant digits can matter Tables make reading faster

Real World Strikes Again

Troll Princess

First Way To Format Output

#include <iostream>using namespace std;

int main() { int k = 4; cout << k << endl; cout.width(4); cout << k << endl; cout << k << endl; cout.setf(ios::showpos); cout << k << endl; cout.width(3); cout.setf(ios::left); cout << k << k << endl;}

Second Way To Format Output

#include <iostream>#include <iomanip>using namespace std;

int main() { int k = 4; cout << k << endl; cout << setw(4) << k << endl; cout << k << endl; cout.setf(ios::showpos); cout << k << endl; cout.setf(ios::left); cout << setw(3) << k << k << endl;}

Your Turn

Get in groups of 3 & work on following activity

For Next Lecture

Read sections 6.7 for Thursday What do we mean by order of operations?

Week #1 weekly assignment due today Problems available on Angel If problem takes more than 10 minutes,

TALK TO ME!

Week #2 weekly assignment posted today Will be due week from today