How we write code visually matters to the humans who are going to be reading it. How could we rewrite this code to have better style?
// Print the size of a rectangleintmain(void){printf("Our rectangle is %d by %d and its area is %d squared",2,4,2*4);}
Input/Output
scanf() and printf() are similar functions.
printf(): print to the terminal
scanf(): scan in from the terminal
Fill in the following table with the correct syntax to
print out a variable called a with value 12/
scan in a variable called a.
printf()
scanf()
int a = 12;
printf();
int a;
scanf();
Write a program rectangle_area.c that scans in 2 integers
which are the side-length of a rectangle, and then prints the area of the rectangle.
C Arithmetic Operators
The value of C arithmetic operations depends on the types of the operand.
If the types of the operands differ, C automatically converts the types as appropriate.
Determine the value and type of each expression and sub-expression:
1 / 2 * 500
1 / 2.0 * 500
(17 / 5) * 5 + (17 % 5)
(12 - 17) % 6 - 4
'A' + 5 (Assume we treat this as a char)
'A' + 32 (Assume we treat this as a char)
Program Flow
What is the syntax of C if statements? What is the role of
if statements in programs?
Assuming num=2, what is the difference between the following if statement structures?
if(num>=1){printf("num is 1 or greater\n");}if(num>1){printf("num is greater than 1\n");}--------------------------------------if(num==1){printf("num is 1\n");}else{printf("num is greater than 1\n");}--------------------------------------if(num==1){printf("num is 1\n");}elseif(num>1){printf("num is greater than 1\n");}else{printf("num is smaller than 1\n");}
A leap year is a year that has 366 days instead of 365. There is a leap
year every year divisible by four except for years which are both divisible
by 100 and not divisible by 400.
For more detail information of leap years see:
Leap Year.
In groups, try drawing a simple flow chart showing the branches of possibilities to decide
if a year is a Leap Year.
Can we then write some simple code using if/else statements in C that decides
if a year is a Leap Year? (hint: finding remainders is a useful way to decide
whether a number is divisible by another) This code question will be continued
in this week's lab, so try to remember or keep a copy/photo of the diagram and
code you've written.
For some humourous information about flow charts see:
Flow Charts
(xkcd).
Complex if conditions
The AND (&&) and OR (||) operators can be used to combine multiple statements
into one (such as the statements created in the leap year activity).
(Source: Penguins of Madagascar, 2014)
Is this true or false: (There are 4 penguins) || (They are the same height)?
What values do these have in C?
Is this true or false: (There are 4 penguins) && (They are the same height)?
What values do these have in C?
Is this true or false: ((There are 4 penguins) && (There are 5 penguins)) || (They are of different heights)?
What values do these have in C?
Is this true or false: ((There are 4 penguins) && (There are 5 penguins)) || !(They are of different heights)?
What values do these have in C?
Debugging
This program 'won't work'. What's wrong with it? Split off into groups and discuss why. Line numbers are included for reference.
1#include<stdio.h>23intmain(void){4printf("Players that have casted a yes vote to continue the game: ")5voted_yes=0;6scanf("%d\n",vouted_yes);78printf("Players that have casted a yes vote to continue the game: ");9intvoted_no=0;10scanf("%f",voted_no);1112printf("There were % votes total...\n",voted_yes+voted_yes);13printf("%d voted no, and %d voted yes",voted_no,voted_yes);14ifvoted_no>voted_yes{15printf("The Squid Game has ended\n.");16}17if{18printf("The Squid Game continues\n.");19}2021return0;22}
Topics Covered: Complex Arithmetic expressions, order of operations.
Part 3: Diagramming
Topics Covered: Flow charts, If statements
Part 4: Practical Programming Exercise
Topics Covered: If statements, scanf, procedures.
Part 1: Calculating Values in Programs (5 mins)
Objectives: Understand the different types of operators, and how to use them.
In this short section, we will review what tools we have for doing mathematics in our programs.
This section will be completed quickly, to make sure we have time for the rest of the tutorial.
Part 2: Weird Data Types and Arithmetic (10 mins)
In this short section, we will review how you can store values in programs.
These are short arithmetic expressions we will use during this tutorial:
(7 / 2)
(3.0 / 2) + 1
'a' + 5
'F' - 'A' + 'a'
Part 3: Diagramming (20 mins)
Objectives: Understand Flowcharts, and how to use them to break down a problem.
In this section, we'll work on diagramming a small program.
Flow Charts:
We'll start by discussing the following flowchart:
Leap Years:
The three rules of a leap year may help with this activity:
Years divisible by 4 are leap years. (e.g. 1904 was a leap year)
Except, years divisible by 100 are not leap years. (e.g 1900 was NOT a leap year)
Except, years divisible by 400 are always leap years. (e.g. 2000 was a leap year)
Part 4: Practical Programming Exercise (rest of tutorial)
Objectives: Utilising Flowcharts and pseudocode to plan a program.
In this activity, you'll be writing the following program.
It should:
Scan in two integers (a and b).
If the first integer is less than the second, print out a short error message using a procedure.
If the second integer is 0, print out a different short error message.
If the first integer is larger than the second, prints a / b and (a * 1.0) / (b * 1.0).
The following (unrelated) example of "pseudocode" might be useful for this activity:
// C style pseudocode example.
// Prints out "Hurrah!" if the entered number is 5
int n = 0
print "Enter a number"
scan a number into n
if (n == 5) {
print "Hurrah!"
}