Download presentation
Presentation is loading. Please wait.
Published byDale Jessie Mitchell Modified over 9 years ago
1
CSC 107 – Programming For Science
2
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
3
Variables, Constants, & More General CasesExamples Variable Declaration dataType 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 Declaration const dataType name = value; const double PI=3.1; Symbolic Constant #define NAME value #define AGE 34
4
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
5
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
6
Program Outline Once upon a time… … some stuff happens… … and they all lived happily ever after
7
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?
8
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
9
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
10
cin Example >>
11
cin Example >>
12
cin Example >>
13
Program Outline Once upon a time… Get the input using cin from the keyboard … some stuff happens… … and they all lived happily ever after
14
Some Stuff Happens This really depends on specific project details Focus of most of term, we will skip this today
15
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?
16
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)
17
Can Print Out Multiple Items cout can also print out value of variables int 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;
18
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
19
Real World Strikes Again TrollPrincess
20
First Way To Format Output #include 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; }
21
Second Way To Format Output #include #include 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; }
22
Your Turn Get in groups of 3 & work on following activity
23
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!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.