COMS11500: Introduction To C: Julian Gough

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 53

COMS11500: Introduction to C

Julian Gough

October 7, 2013
Problem Solving

Outline

1 Problem Solving

2 Conditional Flow Control


What is control flow?
Booleans
The if statement

3 Simplified Software Engineering


General
Applied to our Problem
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

Forum & Office Hour


To avoid duplication (answering the same question over and over), we
will make extensive use of a bulletin board/forum.
There will be a weekly office hours on Wednesdays
Julian from 9.0011.00 Wednesday (MVB 3.20/1.07)
Dimitris from 11:0013:00 Wednesday (MVB 3.20)
Problem Solving

Problem of the Week


Expanding cohort codes

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

Task we will try to solve during this weeks lectures:


Write a program that asks for the code, and prints the fullhand.
Problem Solving

Problem of the Week


Example Input/Output

Please enter your cohort code: 1G161


You are a First Year MEng Engineering Mathematics student

Please enter your cohort code: 3H606


You are a Third Year MEng Electrical and Electronic
Engineering student

Please enter your cohort code: 1G400


You are a First Year mystery student

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

Problem of the Week


Required skills

1 Conditional Control Flow


How to let the program do (print) different things depending on the
input?
2 Types
How to input and process letters (or characters)?
3 Software Engineering
How to turn a problem into a program?
Conditional Flow Control

Outline

1 Problem Solving

2 Conditional Flow Control


What is control flow?
Booleans
The if statement

3 Simplified Software Engineering


General
Applied to our Problem
Conditional Flow Control What is control flow?

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

Dine out Dine out

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

It is useful to test the value of a variable:


Equality: a == b, true iff a equals b.
Note the double equals!
Not-equal: a != b, true iff . . .
Less-than: a < b, true iff a is less than b.
Greater-than: a > b, true iff a is greater than b.
Less-than-or-equals: a <= b, true if . . .
Note that =< doesnt mean anything in C.
Greater-than-or-equals: a >= b, true if . . .
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)
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)

As a result, tests return an int value! For example:

int x=0, y=2, z=2;


x = (x == 0); // now x will be 1
x = (x == 0); // now x will be 0
y = y + (x == 0); // now y will be 3
z = z * (x == y); // now z will be 0
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)

But careful!

int x=2, y=2, z=2, w;


w = (x == y == z);

Since (in math notation) x = y = z, might think w true (1).


But x == y == z evaluates as (x == y) == z.
Plugging in values (2 == 2) == 2.
Now the first equality is true (1), so w is 1 == 2, which is false (0)!
Conditional Flow Control Booleans

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

true false true false


true true false true true true
false false false false true false
Conditional Flow Control Booleans

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

true false true false


true true false true true true
false false false false true false

C uses the symbol && for AND ()


C uses the symbol || for OR ()
Conditional Flow Control Booleans

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.

&& true false || true false


true true false true true true
false false false false true false

C uses the symbol && for AND ()


C uses the symbol || for OR ()
Conditional Flow Control Booleans

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.

&& true false || true false


true true false true true true
false false false false true false

C uses short circuit evaluation:


(x >= 0) && (sqrt(x) < 2.71) and (x == 0) || (10/x > 3)
both work, but dont swap the order!
Conditional Flow Control Booleans

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.

&& true false || true false


true true false true true true
false false false false true false

x = y = z gets rewritten as (x==y) && (y==z)


and x y < z becomes (x<=y) && (y<z)
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
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

int a=3, b=4;


if (a<b) {
printf("a is smaller than b");
}
// "a is smaller than b" is printed
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

int a=7, b=4;


if (a<b) {
printf("a is smaller than b");
}
// nothing is printed
Conditional Flow Control The if statement

Flow Diagram for if


if (test) { statements }

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

Flow Diagram for if


if (test) { statements }

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");
}
}

You want to test whether 0 < a < 5...


Conditional Flow Control The if statement

Nesting ifs
Using Boolean arithmetic to simplify

if (a>0) {
if (a<5) {
printf("a is in range");
}
}

You want to test whether 0 < a < 5...

if ( (a>0) && (a<5) ) {


printf("a is in range");
}
Conditional Flow Control The if statement

Flow Diagram for if with else


if (test) { statements } else { alt-statements }

Start

true
test

false
alt-
statements
statements

End
Conditional Flow Control The if statement

The else statement

if (test) { statements } else { alt-statements }


Use else to execute something on falsehood as well:
If the test returns true, only the statements are performed
Otherwise, only the alt-statements are performed
Conditional Flow Control The if statement

The else statement

if (test) { statements } else { alt-statements }


Use else to execute something on falsehood as well:
If the test returns true, only the statements are performed
Otherwise, only the alt-statements are performed

int a=3, b=4;


if (a<b) {
printf("a is smaller than b");
} else {
printf("a is larger than or equal to b");
}
// "a is smaller than b" is printed
Conditional Flow Control The if statement

The else statement

if (test) { statements } else { alt-statements }


Use else to execute something on falsehood as well:
If the test returns true, only the statements are performed
Otherwise, only the alt-statements are performed

int a=7, b=4;


if (a<b) {
printf("a is smaller than b");
} else {
printf("a is larger than or equal to b");
}
// "a is larger than or equal to b" is printed
Conditional Flow Control The if statement

Nesting ifs and elses


Dealing with more complex choices

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

Nesting ifs and elses


Dealing with more complex choices

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

Flow Diagram for two nested ifs

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

2 Conditional Flow Control


What is control flow?
Booleans
The if statement

3 Simplified Software Engineering


General
Applied to our Problem
Simplified Software Engineering General

Simplified Software Engineering

Problem

Specification

Design

Implement

Test

Release
Simplified Software Engineering General

Simplified Software Engineering

Problem

Specification
Problem

Design
Specification

Specification Specification Specification

Design
Design Design Design

Implement
Implement Implement Implement

Test Test Test


Test

Implement

Release
Test

Release
Simplified Software Engineering General

On the use of Functions

A function type name(type1 arg1, ...)


Functions take some input, perform some tasks and provide some output.
Proper use of functions is essential for effective C programming.
They allow you to chop up the program in smaller, more manageable
parts
This modular approach better expresses the structure of your solution
Usually functions can be debugged individually
Functions can often be reused in other contexts
Functions will become even more important in the coming weeks!
Even if you do something only once, it sometimes pays to write a
function for it.
Simplified Software Engineering General

Useful Questions

What do I want this line of code to do?


This is a top-down question. Being able to explain to yourself (or someone
else) exactly what it is that you want to achieve is often half of the solution.
Simplified Software Engineering General

Useful Questions

Do I know a command that does that sort of thing?


This is a bottom-up question that allows you to build on your existing
knowledge. Often having a sort-of solution only needs some small changes
to become a full solution.
Simplified Software Engineering General

Useful Questions

Exactly what does this line of code do?


This is a debugging and/or fine-tuning question. Whenever you write a line
(or piece) of code, ask yourself what it does if you would hand-execute it.
This is a good way of preventing bugs from slipping into your program.
Simplified Software Engineering Applied to our Problem

Problem of the Week


Decomposing cohort codes

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

Problem of the Week


Towards a Top-Down Solution

Please enter your cohort code: 1G161


You are a First Year MEng Engineering Mathematics student

Please enter your cohort code: 3H606


You are a Third Year MEng Electrical and Electronic
Engineering student

Please enter your cohort code: 1G400


You are a First Year mystery student

Given a code xUyyy then


x is needed to print First or Third
yyy is needed for MEng Engineering Mathematics or mystery.
Even for the printed statements, these two choices are independent.
Simplified Software Engineering Applied to our Problem

Problem of the Week


Towards a Top-Down Solution

Please enter your cohort code: 1G161


You are a First Year MEng Engineering Mathematics student

void printyear(int year);

void printprogramme(int code);

int main(void)
{
int year;
int code;

/* Prompt user for input */


/* TODO */

/* Print the cohort descriptively */


printf("You are a ");
printyear(year);
printf(" Year ");
printprogramme(code);
printf(" student\n");

return 0;
}
Simplified Software Engineering Applied to our Problem

Problem of the Week


Testing the Subroutines

Please enter your year code: 1


First

Please enter your year code: 3


Third

void printyear(int year);

int main(void)
{
int year;

/* Prompt user for input */


printf("Please enter your year code: ");
scanf("%d", &year);

/* Test printyear */
printyear(year);
printf("\n");

return 0;
}

void printyear(int year) { ... }


Simplified Software Engineering Applied to our Problem

Problem of the Week


Testing the Subroutines

Please enter your programme code: 606


MEng Electrical and Electronic Engineering

Please enter your programme code: 400


mystery

void printprogramme(int code);

int main(void)
{
int code;

/* Prompt user for input */


printf("Please enter your programme code: ");
scanf("%d", &code);

/* Test printprogramme */
printprogramme(code);
printf("\n");

return 0;
}

void printprogramme(int code) { ... }


Simplified Software Engineering Applied to our Problem

Problem of the Week


Designing the printyear Subroutine

Specification
On input print
1 First
2 Second
3 Third
4 Fourth

On other inputs we dont


care.
Simplified Software Engineering Applied to our Problem

Problem of the Week


Designing the printyear Subroutine

Nested if statements can do this...

Specification
On input print
1 First
2 Second
3 Third
4 Fourth

On other inputs we dont


care.
Simplified Software Engineering Applied to our Problem

Problem of the Week


Designing the printyear Subroutine

void printyear(int year)


{
Specification if (year==1) {
On input print printf("First");
1 First } else if (year==2) {
2 Second printf("Second");
3 Third } else if (year==3) {
4 Fourth printf("Third");
} else if (year==4) {
On other inputs we dont printf("Fourth");
care. }
}
Simplified Software Engineering Applied to our Problem

Problem of the Week


Writing the printprogramme Subroutine

Degree programme codes


161 MEng Engineering Mathematics
600 BEng Electrical and Electronic Engineering
606 MEng Electrical and Electronic Engineering
623 MEng Electronic and Communications Engineering
640 BEng Electronic and Communications Engineering
925 BEng Engineering Mathematics
rest mystery
Simplified Software Engineering Applied to our Problem

Problem of the Week


Writing the printprogramme Subroutine

Degree programme codes


161 MEng Engineering Mathematics
600 BEng Electrical and Electronic Engineering
606 MEng Electrical and Electronic Engineering
623 MEng Electronic and Communications Engineering
640 BEng Electronic and Communications Engineering
925 BEng Engineering Mathematics
rest mystery

Option 1:
First determine the exact study, print the full statement
Simplified Software Engineering Applied to our Problem

Problem of the Week


Writing the printprogramme Subroutine

Degree programme codes


161 MEng Engineering Mathematics
600 BEng Electrical and Electronic Engineering
606 MEng Electrical and Electronic Engineering
623 MEng Electronic and Communications Engineering
640 BEng Electronic and Communications Engineering
925 BEng Engineering Mathematics
rest mystery

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

Problem of the Week


Writing the printprogramme Subroutine

Degree programme codes


161 MEng Engineering Mathematics
600 BEng Electrical and Electronic Engineering
606 MEng Electrical and Electronic Engineering
623 MEng Electronic and Communications Engineering
640 BEng Electronic and Communications Engineering
925 BEng Engineering Mathematics
rest mystery

Option 3:

1 Determine the specialisation (e.g. 161, 925 for EngMath)


2 Print either BEng or MEng
3 Print the specialisation

You might also like