Andrew Taylor
andrewt@cse.unsw.edu.au
This subject introduces:
After completing this subject, you should be able to:
If you failed COMP1011 or COMP1711, you must repeat COMP1011 and pass it before taking COMP1021.
If you passed COMP1011 or COMP1711 but obtained a mark less than 75% can only enrol in COMP1021 not COMP1721.
You can check your enrollment at:
https://www.student.online.unsw.edu.au/student/
| Day | Time | Room |
|---|---|---|
| Thursday | 11:00-12:00 | EE LG1 |
| Thursday | 13:00-14:00 | EE LG1 |
| Friday | 12:00-13:00 | EE LG1 |
http://www.cse.unsw.edu.au/~cs1721/
Urgent information will be sent by e-mail.
Use NSS to change your tute-lab allocation
https://www.student.online.unsw.edu.au/student/
Do so by Sunday.
Choose carefully!
Tute/labs begin in week 2.
These are for students who would find the pace of COMP1721 far too slow.
If you have a HD from 1011/1711 and experience with C or similar languages, you may wish to attend an accelerated tute-lab.
Tentative times on class web page.
The exam questions will ask you to write all or part of a C program.
You will be able to compile and test your answers.
The rest of the final exam will be on paper.
Part of the exam will be shared with COMP1021.
The C Programming Language, Second Edition
by Brian Kernighan and Dennis Ritchie, 2nd Edition.
If you wish to purchase second C book look at:
For many applications you are better off using one of sh, perl, Prolog, Haskell, Java, ...
See http://cm.bell-labs.com/cm/cs/who/dmr/chist.html for more detail.
#include <stdio.h>
int
main(void) {
printf("Hello world.\n");
return 0;
}
square x = x * x
It is essentially an equation and a Haskell program is essentially a set of equations.
We say functional programming languages such as Haskell are declarative.
A Haskell program defines more what is to be computed rather than how to compute it.
A C program must defines explicitly how to compute a result.
We say procedural programming languages such as C are imperative.
The state of a program:
The program comprises:
State is dynamic; program is static.
Variables are implemented as memory cells.
Each variable has three properties:
Variables are defined by giving name, type and (optionally) an initial value.
int i, j, k; int x, y, z; char ch, ch1; double length, width;
Some examples:
i j12 X nchars numberOfChars Student square_root squareRoot BoolUpper and lower case are distinguished.
C has 32 reserved words (keywords) that you cannot use as names.
Its best to avoid these C++ keywords as well:
Assignment is the basic state-change operation.
Syntax: var = expression ;
E.g. i = 0; sets current value of i to zero.
If the value is not quite the right type, it may be converted.
The operator = in Haskell implies equality not state change.
For example, this is not legal Haskell:
f x = a + b
where
y = x - 1
a = y^2
y = x + 1
b = y^2
We are making a declarative statement that y is equal to another expression.
``Compute the sum of the first 1000 integers''
Our overall approach for solving it:
Note Haskell solution:
sum [1..1000]
``Compute the sum of the first 1000 integers''
How would you do it e.g. using a hand-held non-programmable calculator?
clear calculator display press 1 press + press 2 press + press 3 press + ... press 1000 press =
total = 0; total = total + 1; total = total + 2; total = total + 3; total = total + 4; total = total + 5; total = total + 6; .... .... total = total + 1000; ....
In each repetition
In C:
Thus, we write:
total = total + number;
number = number + 1;
We describe them as variables of type integer.
In C, we need to declare all such objects.
We declare them as:
int total; /* accumulated sum */ int number; /* next summand */
total = 0; number = 1; total = total + number; number = number + 1; total = total + number; number = number + 1; total = total + number; number = number + 1; total = total + number; number = number + 1; total = total + number; number = number + 1; total = total + number; number = number + 1; total = total + number; number = number + 1; total = total + number; number = number + 1; ....
The keyword while allows us to have statments executed many times:
This gives us a practical solution to our problem:
while (number <= 1000) {
total = total + number;
number = number + 1;
}
Beware: of the while loop condition must be chosen carefully.
Compare this Haskell solution:
sum1000 = addup 0 1
addup total number
| number > 1000 = total
| otherwise = addup (total + number) (number + 1)
We also must also print the result:
total = 0;
number = 1;
while (number <= 1000) {
total = total + number;
number = number + 1;
}
printf("%d\n", total);
After this executes,
the value of total will be the required answer.
#include <stdio.h>
int
main(void) {
int number;
int total;
total = 0;
number = 1;
while (number <= 1000) {
total = total + number;
number = number + 1;
}
printf("%d\n", total);
return 0;
}
One form of the if statement:
if (condition)
then_statement
The boolean_expression is evaluated. If it evaluates to true the then_statement is executed,
Braces can be used to group multiple statements into a single then_statement
Second form of the if statement:
if (condition)
then_statement
else
else_statement
The boolean_expression is evaluated. If it evaluates to true the then_statement is executed, otherwise the else_statement is executed.
Braces can be used to group multiple statements into a single then_statement or else_statement.
if (condition)
then_statement
if (condition)
then_statement
else
else_statement
if (condition1) {
statements1;
}
else if (condition2) {
statements2;
}
...
else if (conditionn-1) {
statementsn-1;
}
else {
statementsn;
}
Evaluates conditions until finds a True one; then executes corresponding statements;
then finishes if statement.
f:: Int -> [Char]
f x
| x < 0 = "negative"
| x == 0 = "zero"
| otherwise = "positive"
A corresponding C program might contain these statements:
if (x < 0) {
printf("negative\n");
} else if (x == 0) {
printf("zero\n");
} else {
printf("positive\n");
}
int age;
printf("How old are you: ");
scanf("%d", &age);
if (age < 18) {
printf("Do you have an older sister/brother?");
} else if (age < 25) {
printf("Doing anything tonight?");
} else if (age < 35) {
printf("Do you have an younger sister/brother?");
} else if (age < 65) {
printf("Do you have a daughter/son?");
} else {
printf("Do you have a granddaughter/grandson?");
}
% a.out Temperature in Celsius: 0 0.0C = 32.0F % a.out Temperature in Celsius: 100 100.0C = 212.0F
float celsius, farenheit;
printf("Temperature in Celsius: ");
scanf("%f", &celsius);
farenheit = 9.0 / 5.0 * celsius + 32;
printf("%fC = %fF\n", celsius, farenheit);