CS137Part01 Introduction Post
CS137Part01 Introduction Post
Introduction to C Programming
Introducing Team
You may (and probably should) bring your laptop to class however
if you are in the first 4 rows you must use only text based
applications. Your peers can feel free to remind you of this should
you forget during class. If you want to goof around, please do so
not in the class or in the very last row. Remember no one is
forcing you to be here.
Tips for Surviving 1A
• My office hours.
• Your ISA’s office hours.
• Your IA’s office hours
• Friends! (Discuss cheating and how to avoid it.)
• Piazza, our online forum www.piazza.com
• Math Tutorial Centre
https://uwaterloo.ca/math/current-undergraduates/
mathematics-tutorial-centre (MC 4066 8:30-5:30
starting either Next Monday or the one after)
• The course webpage https:
//www.student.cs.uwaterloo.ca/~cs137/index.shtml.
Course Outline
% nano hello . c
% gcc hello . c
% ./ a . out
% gcc - o hello hello . c
% ./ hello
With Explanation
# Create hello . c
% nano hello . c
# Compile into executable ( a . out )
% gcc hello . c
# ./ refers to the current directory ;
# after this is the executable name
% ./ a . out
# This sets the output file
# to the first parameter
% gcc - o hello hello . c
% ./ hello
IDE Options
There are some options for an IDE if you like GUI based learning.
I’ve heard good things about Eclipse (and CodeBlocks but I’ve had
bug problems with CodeBlocks). You can also just go online and
use repl.it which works great for single file programs. You are
free to use whatever editor you would like.
Homework For Day 1
1080
• How do we reduce 1920 ?
Our First Algorithm - The Euclidean Algorithm
1080
• How do we reduce 1920 ?
• Similarly, how to we compute the greatest common divisor of
1080 and 1920, denoted by gcd(1080, 1920)?
Our First Algorithm - The Euclidean Algorithm
1080
• How do we reduce 1920 ?
• Similarly, how to we compute the greatest common divisor of
1080 and 1920, denoted by gcd(1080, 1920)?
• Turns out geometrically this has a neat interpretation.
Youtube Video
https://www.youtube.com/watch?v=AVrtH6m2wcU
How Does It Work?
• What the video shows is that at each step, we are dividing the
larger number by the smaller number using the division
algorithm (more on this in MATH 135).
• When Euclid performed this, he used repeated subtraction
(which is basically division).
• In fact, we have
while ( expr_is_true ){
// do something
}
Iteration a b r
0 1920 1080 0
1 1080 840 840 (=1920%1080)
2 840 240 240 (=1080%840)
3 240 120 120 (=840%240)
4 120 0 0 (=240%120)
Making the Code User Friendly
• The above code will look for user input of fractions of the
form num/denom and store the numerator and denominator
in the appropriate variables.
• This idea will work not just for / but for any special type of
inputted format.
• The inputted format however must match exactly in order to
work.
• This includes white spaces in the matching.
Euclidean Algorithm With User Input
# include < stdio .h >
int main ( void ) {
int a = 0;
int b = 0;
int r = 0;
scanf ( " % d " , & a );
scanf ( " % d " , & b );
while ( b != 0){
r = a%b;
a = b;
b = r;
}
printf ( " % d \ n " ,a );
return 0;
}
Euclidean Algorithm With User Input
short int a = 1;
b7 b6 b5 b4 b3 b2 b1 b0
The value of a number stored in this system is the binary sum,
that is
b7 27 + b6 26 + b5 25 + b4 24 + b3 23 + b2 22 + b1 21 + b0
For example,
01010101 = 26 + 24 + 22 + 20 = 64 + 16 + 4 + 1 = 8510
or
11111111 = 27 + 26 + 25 + 24 + 23 + 22 + 21 + 20
= 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1
= 25510
Converting to Binary
• Question: Write 38 and in binary.
Converting to Binary
• Question: Write 38 and in binary.
• One way: Take the largest power of 2 less than 38, subtract
and repeat.
• For example, 32 is the largest power of two less than 38,
subtracting gives 6. Next, 4 is the largest power of two less
than 6 and subtracting gives 2. This is a power of two hence
38 = 32 + 4 + 2 = (100110)2 .
• Another way is to constantly divide by 2:
Number Quotient Remainder
38 19 0
19 9 1
9 4 1
4 2 0
2 1 0
1 0 1
• ..and in binary (reading bottom to top) this is (100110)2 .
Signed Integers
3810 = 00100110
11011001
Finally, add 1:
11011010
This last value is −3810 .
Short Cut
• http://www.youtubecutter.com/watch/26be1342/
• https://youtu.be/jm4wqRj4Lm8?t=3h17m12s
What happened?
http://en.cppreference.com/w/c/language/operator_precedence
De Morgan’s Theorem
De Morgan’s Theorem in C
Let P and Q be logical statements. Then
!(P&&Q) = = !P||!Q
!(P||Q) = = !P&&!Q
Proof:
P Q P &&Q !(P&&Q) !P !Q !P || !Q
0 0 0 1 1 1 1
0 1 0 1 1 0 1
1 0 0 1 0 1 1
1 1 1 0 0 0 0
Bit-Wise Operators
if ( expression ) {
// statement
}
else // optional
{
// statement when expression is false
}
Braces are optional if there is only one statement (brackets are not
optional!) Can also write this on one line:
if ( expression ) statement else statement
First If Example
if ( expression ){
// statement
} else if ( expression2 ) {
// statement
} else if ( expression3 ) {
// statement
} // ...
} else {
// final statement
}
If Example
Syntax:
switch ( expr ){
case const_expr : statements break ;
...
case const_expr : statements break ;
default : statements
}
Write a program that requests any integer and prints the sum of
the digits to the screen. For example, if the user enters 1234 then
the answer 10 (=1+2+3+4) is printed to the screen.
Special Cases