Download presentation
Presentation is loading. Please wait.
Published bySara Sharon Norman Modified over 9 years ago
1
CS114-009 Class 03 Topics Sequence statements Input Output Assignment Expressions Read pages Read pages 40 – 49 for next time
2
Sequence Statements statement; Sequence statements statement; No decisions Just perform one statement after another
3
Sequence Statements Can write entire programs just using sequence statements Basics Sequence statements Input Output Assignment (computation) #include using namespace std; int main ( ) { declaration (s); stmt 1; stmt 2; stmt 3; … stmt N; }
4
Input and Output >> << Arrows indicate direction that the data flows >> data flows from the input device (user) into a variable <<data flows from the program to the output device (screen)
5
Input statements Standard input is cin Send item from input to a variable via the >> operator Can send one at a time Can sent lots at once System skips “white space” to find variable White space is: tabs, spaces, new- lines 13 5 27 U a 7 int a, b, c; cin >> a >> b; cin >> a >> c; cin >> b; char a, b, c; cin >> c; cin >> b >> a;
6
Output statements Note the “double” Note the “=“ Standard output is cout Send item to output via << operator Can send one at a time Can send lots at once endl means end of line Can send special characters (see book for details) int a=5, b=10, c, d; double x=3.14, y=2.718; cout << a << b << endl; cout << a << “ “<< b << endl; cout << x; cout << y << endl; cout << endl << a << endl; cout << ‘\t’ << a << endl; cout << c << “ ? “ << endl;
7
Class Exercises Write a C++ program that reads in five integer values, storing them in five variables, and then prints them out in the opposite order that you entered them. Write it using five separate input statements and five separate output statements. Write it using one input statement (multiple >> operators) and one output statement (multiple << operators). Repeat the exercise for five “char” variables.
8
Assignment statements Comments Assignment operator is = Operands include + and – * and / % (mod operator) think “remainder” from third grade division 20 / 6 = 3 remainder 2 20 % 6 = 2 int a=5, b=10, c=2, d, e, f; d = a * 2 + (c – 3); e = b / c; e = e + 1; f = (a % c) + b;
9
Basic Data Types (variables) We focus on int, double and char Use these more than any other types in CS 114 Integers (int) Can store numbers from +/- 2 billion (roughly) Occupies 4 bytes (32 bits) Real numbers (double) store an approximation (exponent and mantissa) Occupies 8 bytes (64 bits) Actual internal bit representation for 2 (int) and 2.0 (double) are completely different
10
Assignment and Variables C++ does arithmetic based on the type of the operands Integer arithmetic is much faster than real (double) arithmetic Impacts division 5/ 2 = 2 (integers only) 5./ 2. = 2.5 (real numbers) int a=5, b=10, c=2, d, e, f; d = a * 2 + (c – 3); e = b / c; e = e + 1; f = (a % c) + b; double x; x = 2 / 5;
11
Converting numbers When dealing with numbers double num; num = 1.0 / 3.0; num = 1.0 / 3; num = 1. / 3; Tell compiler to convert this value to a double “casting” num = double (1) / double (3); num = double (1) / 3; num = 1 / double (3); Integer to character A char is an ASCII value in the range of 1 to 255 Chars are “small” ints char int double
12
Class Exercise What are the final values for e, f, g, and y? #include using namespace std; int main( ) { int a=2, b=4, c=8, d=5, e, f, g; double x = 2.5, y; e = (a + b * c) / (d + 12); f = x / 2; g = c % d; y = c / d + x; }
13
Code a program in.NET Steps involved Invoke Microsoft’s Visual Studio.NET 2005 Create an “empty project” for this program Save this project on your own home disk (“L:” drive). We’ll use this same code on Wednesday. Enter the C++ code Run the program Save this program
14
Homework #2 Due: e-mail your.cpp file to vrbsky@cs.ua.edu Tues. 9/2 before class vrbsky@cs.ua.edu Run the program using the instructions at the link below Print an additional line after hello that says goodbye http://cs.ua.edu/~vrbsky/CS114/Instr.NET.htm
15
Info for.NET 2005 Before we start next exercise, some hints about using.NET 2005 You must create/save a project on the L: drive to use for the class exercises You only need to create/save one project Use this project for all of the programs you write for class exercises
16
When Creating Your Project… In location, type L: L:\ represents your file space on COE It is important to save your files to L:\ as the lab machines are wiped clean on a daily basis.
17
Info for.NET 2005 For each class exercise can create a new.cpp file Save this new.cpp and add as an existing file to your project However, you can only have one.cpp file with a “main ()” in it Must delete the previous.cpp Right click on file name to remove, select remove option file will still exist on your :L drive, just not in the project Source folder (you must add it if you want to run it again)
18
End of Class 03 Read pages 40 – 49 for next class.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.