UMBC CMSC 104 – Section 01, Fall 2016

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

CS16 Week 2 Part 2 Kyle Dewey. Overview Type coercion and casting More on assignment Pre/post increment/decrement scanf Constants Math library Errors.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Introduction to C Topics Compilation Using the gcc Compiler
Using C Programming Language.  The programs that run on a computer are referred to as software.  You’ll learn key programming methodology that are enhancing.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
ITM © Port, KazmanVariables - 1 ITM 352 Expressions, Precedence, Working with Strings.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
CSC 107 – Programming For Science. Announcements.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
CMSC 104, Version 8/061L14AssignmentOps.ppt Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips Reading Section.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Last week: We talked about: History of C Compiler for C programming
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 4 – C Program Control
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
UMBC CMSC 104 – Section 01, Fall 2016
Computer Science 210 Computer Organization
ECE Application Programming
CSC201: Computer Programming
Completing the Problem-Solving Process
Chapter 2 - Introduction to C Programming
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Beginning C++ Programming
Introduction to C Topics Compilation Using the gcc Compiler
Getting Started with C.
ITM 352 Expressions, Precedence, Working with Strings Class #5
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
Intro to PHP & Variables
Computer Science 210 Computer Organization
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
2008/10/27: Lecture 13 CMSC 104, Section 0101 John Y. Park
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
Number and String Operations
Chapter 2 - Introduction to C Programming
A First Book of ANSI C Fourth Edition
Introduction to C Topics Compilation Using the gcc Compiler
Creating your first C program
Assignment Operators Topics Increment and Decrement Operators
Chapter 2 - Introduction to C Programming
Assignment Operators Topics Increment and Decrement Operators
Programming Fundamentals Lecture #3 Overview of Computer Programming
UMBC CMSC 104 – Section 01, Fall 2016
2008/10/27: Lecture 13 CMSC 104, Section 0101 John Y. Park
elementary programming
Assignment Operators Topics Increment and Decrement Operators
Data Types and Expressions
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
More Loops Topics Relational Operators Logical Operators for Loops.
Assignment Operators Topics Increment and Decrement Operators
An Overview of C.
Assignment Operators Topics Increment and Decrement Operators
Introduction to C Programming
Assignment Operators Topics Increment and Decrement Operators
Instructor: Alexander Stoytchev
Assignment Operators Topics Increment and Decrement Operators
Data Types and Expressions
Assignment Operators Topics Increment and Decrement Operators
Data Types and Expressions
Programming for Business Computing Introduction
Presentation transcript:

UMBC CMSC 104 – Section 01, Fall 2016 Assignment Operators

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

Project 5 Review

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…

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.

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.

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.

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.

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.

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.

Code Editors

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!

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 

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

Visual Studio vs Visual Studio Code

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

How It Looks

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

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

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

Assignment Operators

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?

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

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.

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.

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.

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 ;

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

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

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

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

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.

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.

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.

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 ;

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?

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?

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?

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;

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 ;

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

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 ;

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.

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.

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

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

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

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

Project 6

Questions?