csc 107 – programming for science. announcements memorization is not important, but… … you...

23
LECTURE 5: INPUT & OUTPUT FUNCTIONS CSC 107 – Programming For Science

Upload: dale-jessie-mitchell

Post on 02-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead

LECTURE 5:INPUT & OUTPUT FUNCTIONS

CSC 107 – Programming For Science

Page 2: CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead

Announcements

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

information Instead use your resources: notes, books,

slides, etc. Figured out problem using Eclipse in

labs Easy to work around problem via several

options Bringing thumbdrive easiest, but is not

required During the day, tutors available in WTC

206/208 Weekly assignment #2 posted to Angel

Page 3: CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead

Variables, Constants, & More

General Cases Examples

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

int count;bool monkey = true;float avg(10.0);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

Page 4: CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead

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 can hold a character

Page 5: CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead

Data Types

Each numeric data type can be one of 2 types Signed (both positive & negative) version is

default If data must be non-negative use unsigned _____ Upper range of variable is doubled

Warned when using signed & unsigned together

Only certain assignments allowed in system Can assign an integer to a decimal variable Error assigning decimal to integer variable,

since its hard

Page 6: CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead

Program Outline

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

Page 7: CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead

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?

Page 8: CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead

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

Page 9: CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead

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

Page 10: CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead

cin Example

>>

>>

>>

Page 11: CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead

cin Example

>>

>>

>>

Page 12: CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead

cin Example

>>

>>

>>

Page 13: CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead

Program Outline

Once upon a time… Get the input using cin from the keyboard

… some stuff happens… … and they all lived happily ever after

Page 14: CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead

Some Stuff Happens

This really depends on specific project details Focus of most of term, we will skip this

today

Page 15: CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead

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, second steps ask:1. What must be output?2. How can output be presented best?3. Will it be pretty?

Page 16: CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead

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)

Page 17: CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead

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;

Page 18: CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead

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

Page 19: CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead

Real World Strikes Again

Troll Princess

Page 20: CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead

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;}

Page 21: CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead

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;}

Page 22: CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead

Your Turn

Get in groups of 3 & work on following activity

Page 23: CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead

For Next Lecture

Read sections 6.1 – 6.7 for Friday How can we use the variables? What operations exist for us to use? What do we mean by order of operations?

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

TALK TO ME!