Professional Documents
Culture Documents
COMS11500: Introduction To C: Julian Gough
COMS11500: Introduction To C: Julian Gough
COMS11500: Introduction To C: Julian Gough
Julian Gough
October 7, 2013
Problem Solving
Outline
1 Problem Solving
Help
Labs
There is a team of teaching assistants in the labs. What they can do:
Help you with any Visual Studio issues.
Explain the lab exercises/coursework problem.
Explain general C syntax etc.
Discuss possible solutions at a high level.
Point out simple bugs and suggest debugging routines.
The TAs are instructed to monitor style actively!
Problem Solving
Help
Cohort codes
There are shorthand codes that represent
Your year of study
The degree programme you are following, e.g.
G161 MEng Engineering Mathematics
H600 BEng Electrical and Electronic Engineering
H606 MEng Electrical and Electronic Engineering
H623 MEng Electronic and Communications Engineering
H640 BEng Electronic and Communications Engineering
J925 BEng Engineering Mathematics
We assume the code always has the form xUyyy where x and yyy are
numbers and U is an upper case character.
We are only interested in the six codes mentioned previously.
Problem Solving
Outline
1 Problem Solving
Evening Out?
17.00
Go to pub
Dine out
Dancing
Bed
Conditional Flow Control What is control flow?
Evening Out?
17.00
17.00 no
Thirsty?
yes
Go to pub Go to pub Go swimming
no
Tired?
Dancing
yes
Cocktails Dancing
Bed
Bed
Conditional Flow Control What is control flow?
The if statement
Start
true
test
false
statements
End
if (test) { statements }
Use if to make execution conditional:
First the expression test is evaluated
Only if this returns true, the statements are performed
Otherwise the statements are skipped
Conditional Flow Control Booleans
Tests
Representing Booleans
True and false are the values a Boolean datatype has.
C normally uses ints to represent Boolean variables.
True and false as int
0 represents false
non-zero means true (defaulting to 1)
Conditional Flow Control Booleans
Representing Booleans
True and false are the values a Boolean datatype has.
C normally uses ints to represent Boolean variables.
True and false as int
0 represents false
non-zero means true (defaulting to 1)
Representing Booleans
True and false are the values a Boolean datatype has.
C normally uses ints to represent Boolean variables.
True and false as int
0 represents false
non-zero means true (defaulting to 1)
But careful!
Boolean Arithmetic
Or how to test whether x = y = z
Logic operators
Let a and b be boolean variables (either true or false). Then
a b is true iff a AND b are both true
a b is true iff a OR b (at least one of a and b) is true
Boolean Arithmetic
Or how to test whether x = y = z
Logic operators
Let a and b be boolean variables (either true or false). Then
a b is true iff a AND b are both true
a b is true iff a OR b (at least one of a and b) is true
Boolean Arithmetic
Or how to test whether x = y = z
Logic operators in C
Consider ints a and b as boolean variables (either true or false). Then
a && b is true (1) iff a AND b are both true (nonzero)
a || b is true (1) iff a OR b (at least one of a and b) is true.
Boolean Arithmetic
Or how to test whether x = y = z
Logic operators in C
Consider ints a and b as boolean variables (either true or false). Then
a && b is true (1) iff a AND b are both true (nonzero)
a || b is true (1) iff a OR b (at least one of a and b) is true.
Boolean Arithmetic
Or how to test whether x = y = z
Logic operators in C
Consider ints a and b as boolean variables (either true or false). Then
a && b is true (1) iff a AND b are both true (nonzero)
a || b is true (1) iff a OR b (at least one of a and b) is true.
The if statement
if (test) { statements }
Use if to make execution conditional:
First the expression test is evaluated
Only if this returns true, the statements are performed
Otherwise the expression is skipped
Conditional Flow Control The if statement
The if statement
if (test) { statements }
Use if to make execution conditional:
First the expression test is evaluated
Only if this returns true, the statements are performed
Otherwise the expression is skipped
The if statement
if (test) { statements }
Use if to make execution conditional:
First the expression test is evaluated
Only if this returns true, the statements are performed
Otherwise the expression is skipped
Start
int a=3;
int a=3; int b=4;
int b=4;
if (a<b)
true {
a<b
printf("a is smaller than b");
false
}
printf(...);
// "a is smaller than b" is printed
End
Conditional Flow Control The if statement
Start
int a=7;
int a=7; int b=4;
int b=4;
if (a<b)
true {
a<b
printf("a is smaller than b");
false
}
printf(...);
// nothing is printed
End
Conditional Flow Control The if statement
Nesting ifs
Using Boolean arithmetic to simplify
if (a>0) {
if (a<5) {
printf("a is in range");
}
}
Nesting ifs
Using Boolean arithmetic to simplify
if (a>0) {
if (a<5) {
printf("a is in range");
}
}
Start
true
test
false
alt-
statements
statements
End
Conditional Flow Control The if statement
if (a<0) {
printf("a is negative");
} else {
if (a>0) {
printf("a is positive");
} else {
//only remaining alternative
printf("a is zero");
}
}
If there is only a single statement, the braces {...} are not needed.
Conditional Flow Control The if statement
if (a<0) {
printf("a is negative");
} else if (a>0) {
printf("a is positive");
} else {
//only remaining alternative
printf("a is zero");
}
If there is only a single statement, the braces {...} are not needed.
This leads to a common simplification
Conditional Flow Control The if statement
Start
true
test-1
if (test-1) {
statements-1 false
} else if (test-2) { true
test-2 statements-1
statements-2
} else { false
statements-2 statements-3
statements-3
}
End
Simplified Software Engineering
Outline
1 Problem Solving
Problem
Specification
Design
Implement
Test
Release
Simplified Software Engineering General
Problem
Specification
Problem
Design
Specification
Design
Design Design Design
Implement
Implement Implement Implement
Implement
Release
Test
Release
Simplified Software Engineering General
Useful Questions
Useful Questions
Useful Questions
Cohort codes
There are shorthand codes that represent
Your year of study
The degree programme you are following, e.g.
G161 MEng Engineering Mathematics
H600 BEng Electrical and Electronic Engineering
H606 MEng Electrical and Electronic Engineering
H623 MEng Electronic and Communications Engineering
H640 BEng Electronic and Communications Engineering
J925 BEng Engineering Mathematics
Observation: the integer part of the code uniquely identifies the degree
programme
Simplified Software Engineering Applied to our Problem
int main(void)
{
int year;
int code;
return 0;
}
Simplified Software Engineering Applied to our Problem
int main(void)
{
int year;
/* Test printyear */
printyear(year);
printf("\n");
return 0;
}
int main(void)
{
int code;
/* Test printprogramme */
printprogramme(code);
printf("\n");
return 0;
}
Specification
On input print
1 First
2 Second
3 Third
4 Fourth
Specification
On input print
1 First
2 Second
3 Third
4 Fourth
Option 1:
First determine the exact study, print the full statement
Simplified Software Engineering Applied to our Problem
Option 2:
1 For 161, 606, 623 print MEng, for 600, 640, 925 print BEng
2 For 161, 925 print EngMath, etc.
Simplified Software Engineering Applied to our Problem
Option 3: