Presentation is loading. Please wait.

Presentation is loading. Please wait.

UMBC CMSC 104 – Section 01, Fall 2016

Similar presentations


Presentation on theme: "UMBC CMSC 104 – Section 01, Fall 2016"— Presentation transcript:

1 UMBC CMSC 104 – Section 01, Fall 2016
Assignment Operators

2 Notes & Announcements Project 5 due… now! Project 6 posted tonight
Due before class next Tuesday, 11/15 We will discuss later

3 Project 5 Review

4 Coding Project Strategies
GOOD IDEA: Code Incrementally Start small and implement one piece at a time. Get each piece working before moving on to the next. BAD IDEA: Code the entire application at once Inundated with a sea of errors & warnings. Something’s wrong… somewhere…

5 Coding Project Strategies
GOOD IDEA: Use your resources Start with code samples from the slides, the book, the internet, etc. Modify to meet your needs. BAD IDEA: Trying to go completely from memory C, and all programming languages, are VERY VERY picky about syntax. One misplaced character and everything is broken – or worse, you’ve introduced a hard-to-find bug.

6 Coding Project Strategies
GOOD IDEA: Practice with small sample programs When you’re new to a construct, keep it small and simple to start. In large programs, problems can come from lots of places. In small, simple programs, things are easier to understand BAD IDEA: Just come to lecture and do nothing else The more you code, the better you’ll be. You can run gcc as many times as you want. Don’t be scared of it.

7 Coding Project Strategies
GOOD IDEA: Take a break! Coding is sometimes frustrating for everyone. Sometimes we get hung up on the wrong issue. Taking a break often gives us perspective. BAD IDEA: Marathon coding sessions My junior year roommate loved these. He also had a keyboard he called his “programming keyboard” which doubled as a punching bag. He went through several each semester.

8 Coding Project Strategies
GOOD IDEA: Start Early! Plenty of time to take breaks, do research, re-read slides, etc. Can go to TA or Instructor if you need additional assistance BAD IDEA: Starting a project 2 hours before it is due At this point in the class, this is a recipe for disaster. Zero time to reach out for help if you need it.

9 Coding Project Strategies
GOOD IDEA: Review the project description before submitting! Double check to make sure you’ve met all the guidelines and requirements. Why lose easy points when you don’t have to? BAD IDEA: Read the description once, then implement and submit. Good chance you missed or misunderstood something.

10 Coding Project Strategies
GOOD IDEA: Reach out to the Instructor or TA for further assistance Try the previous strategies first. We’re here to help you. BAD IDEA: Working with (or copying code from) others Your work should be your own! Cheating will NOT be tolerated.

11 Code Editors

12 Developing More Efficiently
Emacs and Nano work, and Emacs is considered a professional development tool, but… Very steep learning curve Poor support for non-*nix based OSes Quirky There are lots of other options!

13 Some Other Options One option is to install Linux on your own machine
Good option for CMSC majors and career developers Bad option for everyone else Another option is to install a development tool built for your OS Plenty available for Windows & macOS …but you still need to compile and run on UMBC GL Linux machine 

14 The Best of Both Worlds An ideal solution is one that allows you to develop locally AND save your data to UMBC GL Many of these exist. Probably. Let’s talk about the one I use: Microsoft Visual Studio Code Works on Windows, macOS, and Linux

15 Visual Studio vs Visual Studio Code

16 Installing Visual Studio Code
Go to code.visualstudio.com Click the download link Follow the prompts to install

17 How It Looks

18 Installing Necessary Plugins
You’ll need at least two plugins for C development with UMBC GL C/C++ Plugin SSH Plugin To install plugins, go to View>Extensions or push Ctrl+Shift+X Search for C/C++ Plugin by Microsoft and Install Search for ftp-simple by humy2833 and Install

19 Configuring ftp-simple
After loading ftp-simple: Press F1 Type “ftp-simple” and choose the Config option Fill in the config options

20 Loading a file Log into GL using a terminal (PuTTY, etc) and create your file Within VS Code Press F1 Type “ftp-simple” Select option to open file Find your file and open it When you save the file in VS Code, it will save on the UMBC GL server You still need to compile and run your code using a terminal

21 Assignment Operators

22 C Programmers are lazy…
Early in the life of the language, C programmers discovered they were doing certain things quite frequently One of those things is incrementing and decrementing variables count = count + 1; What if there was an easier way?

23 Increment and Decrement Operators
The increment operator ++ The decrement operator -- Precedence: lower than (), but higher than * / and % Associativity: right to left Increment and decrement operators can only be applied to variables, not to constants or expressions

24 Increment Operator If we want to add one to a variable, we can say:
count = count + 1 ; Programs often contain statements that increment variables, so to save on typing, C provides these shortcuts: count++ ; OR count ; Both do the same thing. They change the value of count by adding one to it.

25 Postincrement Operator
The position of the ++ determines when the value is incremented. If the ++ is after the variable, then the incrementing is done last (a postincrement). int amount, count ; count = 3 ; amount = 2 * count++ ; amount gets the value of 2 * 3, which is 6, and then 1 gets added to count. So, after executing the last line, amount is 6 and count is 4.

26 Preincrement Operator
If the ++ is before the variable, then the incrementing is done first (a preincrement). int amount, count ; count = 3 ; amount = 2 * ++count ; 1 gets added to count first, then amount gets the value of 2 * 4, which is 8. So, after executing the last line, amount is 8 and count is 4.

27 Code Example Using ++ /* count from 1 to 10 */
#include <stdio.h> int main ( ) { int i = 1 ; /* count from 1 to 10 */ while ( i < 11 ) printf (“%d ”, i) ; i++ ; /* same as ++i */ } return 0 ;

28 OR Which looks better? Which looks better?
for ( i = 0 ; i < 5 ; i = i + 1 ) OR for ( i = 0 ; i < 5 ; i++ )

29 Another example int i = 0; while ( i < 5 ) { printf("This is iteration %d!\n", i); i++; }

30 This does the same thing…
int i = 0; while ( i < 5 ) { printf("This is iteration %d!\n", i++); }

31 But what about this? int i = 0; while ( i < 5 ) { printf("This is iteration %d!\n", ++i); }

32 Decrement Operator If we want to subtract one from a variable, we can say: count = count - 1 ; Programs often contain statements that decrement variables, so to save on typing, C provides these shortcuts: count-- ; OR count ; Both do the same thing. They change the value of count by subtracting one from it.

33 Postdecrement Operator
The position of the -- determines when the value is decremented. If the -- is after the variable, then the decrementing is done last (a postdecrement). int amount, count ; count = 3 ; amount = 2 * count-- ; amount gets the value of 2 * 3, which is 6, and then 1 gets subtracted from count. So, after executing the last line, amount is 6 and count is 2.

34 Predecrement Operator
If the -- is before the variable, then the decrementing is done first (a predecrement). int amount, count ; count = 3 ; amount = 2 * --count ; 1 gets subtracted from count first, then amount gets the value of 2 * 2, which is 4. So, after executing the last line, amount is 4 and count is 2.

35 A Hand Trace Example int answer, value = 4 ; Code Value Answer
4 garbage value = value + 1 ; value++ ; ++value ; answer = 2 * value++ ; answer = ++value / 2 ; value-- ; --value ; answer = --value * 2 ; answer = value-- / 3 ;

36 Practice Given int a = 1, b = 2, c = 3 ;
What is the value of this expression? ++a * b - c-- What are the new values of a, b, and c?

37 More Practice Given int a = 1, b = 2, c = 3, d = 4 ;
What is the value of this expression? ++b / c + a * d++ What are the new values of a, b, c, and d?

38 More Practice Given int a = 1, b = 2, c = 3, d = 4 ;
What is the value of this expression? c-- / --b + --a * ++d What are the new values of a, b, c, and d?

39 Multiples The ++ and -- operators are great for adding or subtracting 1, but what if we want to add or subtract more? We can do this too! Want to add 2 to x? x += 2; Want to subtract 3 from y? y -= 3; How about multiplication, division, and mod? Yup! Want to multiply a by 4? a *= 4; Want to divide b by 5? b /= 5; Want to mod c by 6? c %= 6;

40 Assignment Operators = += -= *= /= %= Statement Equivalent Statement
= += -= *= /= %= Statement Equivalent Statement a = a + 2 ; a += 2 ; a = a - 3 ; a -= 3 ; a = a * 2 ; a *= 2 ; a = a / 4 ; a /= 4 ; a = a % 2 ; a %= 2 ; b = b + ( c + 2 ) ; b += c + 2 ; d = d * ( e - 5 ) ; d *= e - 5 ;

41 Practice with Assignment Operators
int i = 1, j = 2, k = 3, m = 4 ; Expression Value i += j + k j *= k = m + 5 k -= m /= j * 2

42 Code Example Using /= and ++ Counting the Digits in an Integer
#include <stdio.h> int main ( ) { int num, temp, digits = 0 ; temp = num = 4327 ; while ( temp > 0 ) { printf (“%d\n”, temp) ; temp /= 10 ; digits++ ; } printf (“There are %d digits in %d.\n”, digits, num) ; return 0 ;

43 Debugging Tips Trace your code by hand (a hand trace), keeping track of the value of each variable. Insert temporary printf() statements so you can see what your program is doing. Confirm that the correct value(s) has been read in. Check the results of arithmetic computations immediately after they are performed.

44 Escape Sequences From Wikipedia: Huh?
An escape sequence is a sequence of characters that does not represent itself when used inside a character or string literal, but is translated into another character or a sequence of characters that may be difficult or impossible to represent directly. Huh? A good example (that we’ve already covered) is a newline, which is represented as \n Escape sequences in C begin with a \ character.

45 Common C Escape Sequences
Character represented \n Newline \r Carriage Return \t [Horizontal] Tab \v Vertical Tab \\ Backslash (\) \' Single quotation mark (') \" Double Quotation Mark (") \a Alert Bell

46 Newline Example C Statement: printf("Hello\nThere!\n\nHow are\nYou?"); Output: Hello There! How are You?

47 Tab Example C Statement: Output: one two three four five six seven
printf("one\ttwo\tthree\nfour\tfive\tsix\tseven"); Output: one two three four five six seven

48 Quote Example C Statement: Output: Quoth the Raven, "Nevermore"
printf(“Quoth the Raven, \"Nevermore\"\n"); Output: Quoth the Raven, "Nevermore"

49 Project 6

50 Questions?


Download ppt "UMBC CMSC 104 – Section 01, Fall 2016"

Similar presentations


Ads by Google