Daniel Sutantyo, Department of Computing, Macquarie University
2.0 - Algorithm Correctness
2.0 - Algorithm Correctness
// Add the following to your total score:
// - if you are 30 years or younger, give yourself 8 points
// - if you are aged between 30 and 39, give yourself 7 points
// - if you are aged between 40 and 59, give yourself 5 points
// - if you are 60 years or older, give yourself 4 points
if (age <= 30)
points += 8
if (age < 40)
points += 7
if (age < 60)
points += 5
else
points += 4
2.0 - Algorithm Correctness
year = ORIGINYEAR; /* = 1980 */
// Given days = the number of days since 1 January 1980
// work out what year this is
// if number of days > 365
while (days > 365)
{
// if it is a leap year
if (IsLeapYear(year))
{
// and number of days > 366
if (days > 366)
{
days -= 366;
year += 1;
}
}
// it is not a leap year
else
{
days -= 365;
year += 1;
}
}
2.0 - Algorithm Correctness
year = ORIGINYEAR; /* = 1980 */
// Given days = the number of days since 1 January 1980
// work out what year this is
// if days == some big number
while (days > 365)
{
// if it is a leap year
if (IsLeapYear(year))
{
// and number of days > 366
if (days > 366)
{
days -= 366;
year += 1;
}
}
// ok, if it's not a leap year I don't see a problem
else
{
days -= 365;
year += 1;
}
}
2.0 - Algorithm Correctness
year = ORIGINYEAR; /* = 1980 */
// Given days = the number of days since 1 January 1980
// work out what year this is
// if days == 366
while (days > 365)
{
// if it is a leap year and days == 366
if (IsLeapYear(year))
{
// so, we're not going inside this if statement
if (days > 366)
{
days -= 366;
year += 1;
}
// uhh ... what do we do now?
}
// don't look here, bruh, can't help you
else
{
days -= 365;
year += 1;
}
}
2.0 - Algorithm Correctness
2.0 - Algorithm Correctness
2.0 - Algorithm Correctness
2.0 - Algorithm Correctness
2.0 - Algorithm Correctness
2.0 - Algorithm Correctness
2.0 - Algorithm Correctness
2.0 - Algorithm Correctness
for(int i = 2; i < N; i++){
if (N % i == 0)
System.out.println(i);
}
2.0 - Algorithm Correctness
// X and n are positive integers
int add(int X, int n){
if (n <= 0)
return 0;
return X + add(X,n-1);
}
2.0 - Algorithm Correctness
2.0 - Algorithm Correctness
2.0 - Algorithm Correctness
4
7
4
5
1
3
5
2
3
2
Prim's Algorithm
5
2.0 - Algorithm Correctness
4
5
2
???
Prim's Algorithm
2.0 - Algorithm Correctness
2.0 - Algorithm Correctness
2.0 - Algorithm Correctness
2.0 - Algorithm Correctness
3
2
4
1
3
6
3
7
2
2
4
1
6
3
7
2.0 - Algorithm Correctness
2
4
1
6
3
7
3
2
1
3
3
2
2.0 - Algorithm Correctness
2.0 - Algorithm Correctness