Presentation is loading. Please wait.

Presentation is loading. Please wait.

CISC 181: Introduction to Computer Science An introduction to the language of C++ for students with programming experience. Meeting times: Tuesday/Thursday,

Similar presentations


Presentation on theme: "CISC 181: Introduction to Computer Science An introduction to the language of C++ for students with programming experience. Meeting times: Tuesday/Thursday,"— Presentation transcript:

1 CISC 181: Introduction to Computer Science An introduction to the language of C++ for students with programming experience. Meeting times: Tuesday/Thursday, 4:30 - 6:15PM Lab time: Tuesdays, 6:30 – 8:00 PM TA: Scott Grauer-Gray Instructor: Eric Schrag

2 Welcome! ● A little about me: – I am a graduate student here at UD – I received my M.S. in Computer Science in 2005 ● Things to know (also on your syllabus) – Course website – TA's email address – My email address – Course and Lab meeting time

3 What will CISC 181 cover? ● We will – Review UNIX use and C programming practices – Discuss proper programming standards – Learn about memory in computers – Learn basic data structures – Learn about Object Oriented Programming and the use of Classes – Discuss some basic algorithms like sorting

4 About C++ ● C++ was made by Bjarne Stroustrup in 1970 ● C++ is a compiled language – The code's syntax is checked in advance – Then object files are created – Which are then compiled into an executable file – Compiled code is different for every machine type ● C++ is “Unmanaged” – This means that YOU, the programmer, are responsible for memory!

5 Today's Lecture ● Since the course is 10 weeks instead of 14, we're going to dive right in today – Unix refresher – C refresher – Intro to basic C++ Input/Output – Coding Standards ● There will be a lab today after this lecture! – A basic introductory lab to get you back into the swing of things

6 Unix Refresher ● You'll be working this summer on the Sun Blades when you're in lab. ● Outside of lab, you can either use machines in the lab, in Smith Basement, or you can connect from home. ● To connect from home (and this is also what I'll demo things on in class) use Secure Shell SSH.

7 Secure Shell SSH ● Available at http://udeploy.udel.edu/index.html

8 Basic UNIX commands ● mkdir – Creates a new directory ● cd – Changes to a different directory ● pwd – Tells you what directory you are in ● ls – Lists the files in your directory ● more – a way to read files interactively ● cat – A fast file display ● script – A command to capture screen I/O ● And more. ● Let's do a demo!

9 C/C++ refresher ● A lot of the things you learned in C are the same or very similar in C++ – Variables and Assignment – Conditional Statements – Control Structures – Mathematical Operators – Basic data types

10 Making a C++ program ● There are six basic steps for a program – Editing code – This is when you write code. – Preprocessing \ – Compiling |- This is the compiler's job. – Linking / – Loading – Bringing it into memory – Executing – The actual runtime

11 C++ - Basic Variables ● int testInteger, testIntegerTwo; ● char letter = 'a'; ● const float pi = 3.142; ● Declaring variables is telling the compiler: – The type of data you want to store – An identifier for the data (a name) ● Variables can be declared constant if they should not change.

12 C++ - Assignment ● testInteger = 7; ● testInteger = 4 + (testInteger2 = 6); ● Assignment is done using the assignment operator: = ● Assignments always have a variable on the left hand side, and an expression on the right hand side

13 Assignment ● The right hand side is evaluated, then the left hand variable is set to the right hand side. ● The right hand side can be complex! ● Order of operations can come in to play here; we will discuss it more later.

14 Basic Math Operators + : Addition operator - : Subtraction operator * : Multiplication operator / :Division operator % : Modulus (or Remainder) operator ++: Increment operator -- : Decrement operator

15 Conditional Statements ● These are operators that evaluate differently depending on a comparison – < – <= – >= – > – == – != ● They compare the left and right side, and evaluate to true or false (and if you use that as an int, 1 or 0)

16 Operator Precedence ● 1: () ● 2: *,/,% ● 3: +,- ● 4: > ● 5:, >= ● 6: ==, != ● 7: =

17 Assignments and Equality ● == and = look very similar, but are very different. What will happen in the code below? ----------------------------------------------------- int a = 4; if(a = 7) cout << “Equal to 7!” << endl; else cout << “Not equal to 7!” << endl;

18 Type Casting ● We've discussed division with floats versus division with ints and seen the difference. ● What if you need to divide two integers and get an accurate value, though? ● You can cast one of the variables to a float or double, and get the precisions you need! ● Casting is a method that allows you to change the type of a value.

19 How can you cast variables? ● Old ways: (Don't use these anymore!) – (double)feetWalked – double(feetWalked) ● New ways: – static_cast (expression) – const_cast (expression) – dynamic_cast (expression) – reinterpret_cast (expression) ● For now, static_cast is all you need to know.

20 Coding Style ● Before we move on, let's look at some code. ● It's important to get into good coding habits now!

21 Is this code easy to understand? #include using namespace std; float calc(int, int); int main(){ int x = 4; int y = 7; cout << calc(x,y) << endl; } float calc(int x, int y){ return ((static_cast (x)/y)*9); }

22 How about this code? #include using namespace std; float calculateERA(int, int); int main(){ int earnedRunsToday = 4; int inningsPitchedToday = 7; cout << calculateERA(earnedRunsToday, inningsPitchedToday) << endl; } float calculateERA(int ER, int IP){ float runsPerInning = static_cast (ER)/IP; return (runsPerInning*9); }

23 How about this code? /***************************** * Author: Eric Schrag * This is a simple earned run average calculator. * You can change the earned runs and innings pitched * variables to find out what an ERA for that game is. *****************************/ #include using namespace std; float calculateERA(int, int); int main(){ int earnedRunsToday = 4; int inningsPitchedToday = 7; cout << calculateERA(earnedRunsToday, inningsPitchedToday) << endl; } /*************************** * calculateERA takes two arguments: Earned Runs and * Innings Pitched. It then calculates ERA by the formula (ER/IP)*9. ***************************/ float calculateERA(int ER, int IP){ float runsPerInning = static_cast (ER)/IP; return (runsPerInning*9); }

24 Coding Standard ● Following good coding standards is crucial! ● A common standard lets other people: – Understand your code easier – Learn what's going on faster – Maintain and reuse code better – Helps YOU remember what your code is doing! ● Following a standard is a good general practice.

25 A Coding Standard to Follow ● A Comment Block at the top of each file – Author Name – Date – Description of the file's purpose: what does the code in here do? ● Descriptive Variable Names ● Descriptive Function Names ● Consistent Case: namesLikeThis ● Indent in loops ● No magic numbers ● Use constants for numbers that are always the same

26 Basic C++ Input/Output ● Using the library (more on libraries later) you can access cin, cout, and cerr. ● cout is the standard output stream ● cin is the standard input stream ● cerr is the standard error stream ● You can use > (the insertion and extraction operators) to manipulate these.

27 Printing using cout ● cout is the stream that outputs to the console. ● You can print to the screen using cout with the insertion operator: – cout << “Hello, class.” << endl; – cout << “You “ << “can “ << “print many things”; – cout << “Even “ << (2 + 2 + x) << “ expressions”; ● You can print newlines by using either the endl statement, or “\n”.

28 Formatting in cout ● If you want to format your output, there are special stream functions to do so. ● For now, this is your “magic formula” – cout.setf(ios::fixed); – cout.setf(ios::showpoint); – cout.precision(2) ● This will let you have only the last two decimal points showing. ● Whatever number is in precision will be the amount of decimal places shown. ● After this code, anything to cout after will use this format.

29 Output with cerr ● cerr is the standard error stream. ● It also generally writes to the screen. ● It is useful to know in that you can send different streams to different files. ● So, if you want to capture regular output, but not errors – Put normal things on cout – Put error statements on cerr – You can then pipe them to different files.

30 Input using cin ● cin is the standard input stream ● It lets you capture input from the keyboard. ● You can use it like you use cout, but backwards: – cin >> aUsersNumber; – cin >> userNumberOne >> userNumberTwo; ● It will wait for input from the keyboard – after an enter press, it then reads what was typed into the variable(s). ● Input can be separated by spaces, or entered one at a time with return presses each time.

31 C++ Libraries ● C++ has many standard libraries that contain useful code. ● We've seen this already with ● You can include these libraries with #include – #include ● Libraries also have namespaces – these are collections of name definitions. Functions can have different definitions in different namespaces. ● For now, we can just discuss the standard namespace.

32 C++ libraries ● Access namespaces with the using directive – #include using namespace std; ● If you didn't want to use everything from std – #include using std::cin; using std::cout; ● C++ is in transition. The compilers on Strauss work right, you may need to use different library names. (p.38, Savitch)


Download ppt "CISC 181: Introduction to Computer Science An introduction to the language of C++ for students with programming experience. Meeting times: Tuesday/Thursday,"

Similar presentations


Ads by Google