Programming Fundamentals
Part 0: Making Groups & Assignment Checkin
It's Week 10! Well done on making it this far through the course. You've all worked very hard and achieved some incredible results, and you should all feel proud about how far you've come in these ten short weeks. We'll spend a few minutes discussing the term.
This tutorial will involve some group work -- your tutor will split the class into groups. Don't worry! The groups are just for this tutorial.
Part 1: COMP1511 Snakes and Ladders
Welcome to Snakes and Ladders: COMP1511 Edition! Immerse yourself in this classic game with a twist, where every roll of the dice propels you through a thrilling journey of ups and downs. This game combines luck, strategy, COMP1511 knowledge and suspense, ensuring that no two adventures are ever the same. Get ready to climb ladders of success and slide down the snakes of setback in your quest to reach the top!
This tutorial will be run as a giant game! Your tutor may split you up into teams, or compete against everyone in the class at once.
If there are two classes that run at the same time, your tutor may run a combined game with the two classes.
Game instructions
- Roll the Dice: On your turn, roll the dice and move your player forward the number of squares shown.
- Challenge square: If the player lands on the purple square its a race! Pause the round and the first group to answer a 🌟🌟🌟🌟 question gets to move a bonus 2 squares. After this race has happened, continue to play the round as usual. Note, the player that landed on the challenge square should still pick a level as usual.
- Pick a level: If the player lands on another other square choose ⭐️, ⭐️⭐️ or ⭐️⭐️⭐️. Whichever number of stars you choose will dictate the difficulty of your question and if solved will correlate to the same number of bonus moves.
- Climbing Ladders: If you land at the base of a ladder, it's your lucky day! Solve a 🌟🌟🌟🌟 challenge to climb up to the top of the ladder to advance faster towards the treasure.
- Sliding Down Snakes: Beware of snakes! Landing on a snake's head will send you sliding down to its tail if you answer a 🌟🌟🌟🌟 challenge incorrectly, moving you away from the treasure.
- Take Turns: After your move, pass the dice to the group on your left. It’s now their turn to face their fate.
- Once all groups have taken their turn for a round, it is solving time! All groups will solve the tutor selected question for their chosen level. If the question is solved correctly the player will move forward the same number of places as stars for their selected question.
Winning the Game
The first group to reach the 100th square, exact roll not required, finds the treasure and wins the game!
Question bank
⭐️
- What is the name of the compiler used in COMP1511?
- Give an example of a command to compile the file example.c
- Which of the following variable names is aligned with 1511 style?
A) cooked?
B) number_counter
C) while
D) !!yeeet!!
- Name 3 types which variables can have in COMP1511.
- What type does the variable "int *hello" have?
- Which of the following would not be allowed (multiple answers):
A) an if statement followed by an else if
B) int x = 3;
, followed by an else-if followed by an if statement
C) an if statement, followed by an else, followed by an else-if
D) an if statement, followed by three else-if statements
- What is a variable?
- Define an integer data type.
- Give an example of a logical operator in C.
- What is the result of adding two
char
values in C?
- Which operator is used to compare two values for equality?
A) =
B) ==
C) ===
D) !=
- Explain how a memory leak might occur in C programming.
- How does a
while
loop differ from afor
loop?
- What is a pointer, and why is it used in C?
- What is the difference between an array of characters and a string
- Design a struct that stores a students name (max length 100), WAM, id (zID without the Z)
⭐️⭐️
- What line will the first compiler error be on when you compiler this program?
1 #include <stdio.h>
2
3 int main(void) {
4 int x = 0;
5 int y = 1;
6 while (x < 50) {
7 printf("%d bottles of beer on the wall.\n", x);
8 x++;
9 y--;
10 } else {
11 printf("Take one down, pass it around . . .\n", y);
12 }
13 return 0
14 }
- What will this program print?
#include <stdio.h>
#define MAX_NAME_LENGTH 20
int main(void) {
char s[MAX_NAME_LENGTH] = "Hello";
printf("%d", s[5]);
return 0;
}
- What does this program print?
#include <stdio.h>
int main(void) {
int a = 25;
if (a < 0) {
if (a >= 1000) {
a = 0;
} else if (a > 500) {
a = a * 2;
} else {
a = a * 10;
}
} else {
if (a > 20) {
a = a - 42;
} else {
a = a + 42;
}
}
printf("%d\n", a);
return 0;
}
- Suppose you have a function that is supposed to modify the value of a variable passed to it, but changes made within the function do not affect the original variable. What could be going wrong?
- This program takes a number greater than 1 and determines if 2, 3, or 5, is its smallest prime factor. If it has a different smallest prime factor, like 7, then the program lets you know that that factor is higher than 5. Find the errors in the program
#include <stdio.h>
int main(void) {
// Scan the number.
int number;
printf("Enter a number: ");
scanf("%d", &number);
// Find it's lowest factor.
if (number % 2 == 0) {
printf("The lowest prime factor is 2.\n");
}
if (number % 3 == 0) {
printf("The lowest prime factor is 3.\n");
}
if (number % 5 == 0) {
printf("The lowest prime factor is 5.\n");
} else {
printf("The lowest prime factor of %d is higher than 5\n", number);
}
return 0;
}
- This exercise takes in a positive integer 'n' as input, and prints the outline of a square of size 'n' by 'n' in asterisks ('x'). Currently it has some issues - it is your job to figure them out and fix the code.
#include <stdio.h>
int main (void) {
int size;
int rows;
int cols;
printf("Enter the size of the pattern: ");
scanf("%d", size);
while (rows < size) {
while (cols < size) {
printf("*");
cols++;
}
rows++;
}
return 0;
}
- Given two linked lists write a function that returns 0, if neither list contains even numbers, returns 1 if one list contains even numbers, but the other does not or returns -1 if both lists contain even numbers.
int list_evens(struct node *head1, struct node *head2)
- Given a linked list and a value, count the number of times that value appears in the linked list.
int list_count_occurrences(struct node *head, int data)
- Write a function that returns the number of lowercase letters in
char *string
.
- What would the following program output?
#include <stdio.h>
int main(void) {
int i = 0;
int keep_going = 1;
while (keep_going == 1) {
if (i > 3) {
keep_going = 0;
}
i++;
}
printf("%d\n", i);
return 0;
}
- Write a C program that reads 3 integers into a struct that stores three integers and prints the middle integer.
⭐️⭐️⭐️
- Write a program that reverses a string in C. Note the string has a max size of 100.
- Given two linked lists, return a new list that is constructed of nodes containing any values that appear in both lists.
struct node *list_find_intersection(struct node *head1, struct node *head2)
- Given a linked list that is ordered in ascending order and a value to insert, insert the value into the list that will allow the list to remain in ascending order.
struct node *list_ordered_insert(struct node *head, int data)
- Write a function that shortens a string so that it ends after the first word, i.e. “This is a sentence” should turn into “This”
- Write a C program that takes multiple integers as command-line arguments and prints their sum.
- Write a C program that checks if any command-line arguments were provided except for the program name. If none were provided, print a message indicating so; otherwise, print the number of arguments.
- Write a C program that counts the total number of characters in all the command-line arguments passed to it.
- Write the output of the following program.
#include <stdio.h>
int main(void) {
int i = 0;
int keep_going = 0;
while (keep_going == 1) {
if (i > 3) {
keep_going = 0;
}
i++;
}
printf("%d\n", i);
return 0;
}
- Write a program that creates the following
XXXX
XOOX
XOOX
XXXX
-
Write a program that takes in a series of integers until you reach a negative number. Then, stop and calculate the sum.
-
Write a program that scans in integers keeping a cumulative sum, until the sum of entered integers reaches or exceeds the target sum provided by the user.
🌟🌟🌟🌟
- Draw a diagram for the following pseudocode
malloc memory for a new node called node1
node1 data = 3
node1 next = NULL
make the head pointer points to node1
malloc memory for a new node called node2
node2 data = 9
node2 next = NULL
add node2 to the tail of the list, making node1 next point to node2
malloc memory for a new node called node3
node3 data = 5
node3 next = NULL
add node3 to the head of the list, making node3 next point to the current head of the list
make the head point to node3
- Imagine that
int i = 0;
andint *i_ptr = &i
. Which of the following are valid (i.e. compile with no warnings or errors)?
A) i = i_ptr;
B) *i_ptr = i;
C) i_ptr = *i_ptr;
D) if (i == i_ptr) {...}
-
Write a while loop which increments an integer
i
untilstring[i]
is the end of the string -
Find 3 issues in this code
1 #include <stdio.h>
2
3 int main(void) {
4 printf("Counting the number of minutes in a number of hours.");
5 printf("How many hours: ");
6
7 int a;
8 scanf("%d" ,$a);
9
10 int b;
11 int c = 0;
12 int d = 0;
13 char total_minutes = 0;
14
15 while (b < a) {
16 while @c < 60@ {
17 }
18 total_minutes = total_minutes + 1;
19 a = a + 1;
20 }
21
22 printf("There are %c minutes in %d hours.\n" ,total_minutes, a);
23
24 return 1;
- Fill in the [SECRET] section of the code below.
struct node {
int value;
struct node* next;
};
// Creates a node pointing at NULL, with the gien value.
struct node *create_node(int value);
struct node *insert_tail(struct node *head, int value) {
if (head == NULL) {
return create_node(value);
} else {
struct node *current = head;
while ([SECRET]) {
current = current->next;
}
current->next = create_node(value);
return head;
}
}
-
Write a function that given a value and linked lists, determines if that value is at the tail of the linked list.
-
An enum called "indicator" has fields "LEFT", "RIGHT", and "OFF". How would you initialise a variable to have one of those fields?
-
Name three components of style assessed in COMP1511
10.Draw a flowchart for determining if a year is a leap year. For a year to be a leap year the year must be evenly divisible by 4. Then, if the year can be evenly divided by 100, it is not a leap year unless the year is also evenly divisible by 400,
Revision Kahoot
This Kahoot will cover topics from the entirety of COMP1511. If you are working in teams, you are only be allowed one device per team.
Before you Go: Notes For The Lab
This week, the lab will consist of completing myExperience, and attempting the practice exam.
Students attempting the practice exam face-to-face in a CSE lab will do so in the "Exam Environment". This is a locked-down environment which does not allow access to the internet (aside from the COMP1511 course website), and is identical to what will be used in the real final exam. If you are in an online tut-lab and would like to try out the exam environment, there are a number of sessions which you can sign up for here! (the access code is "COMP1511")
During the practice exam, please make sure that you try the following things:
- Open the exam paper, and read the information/conditions within. These will be the same as in your final exam.
- Open the course website, and check out the resources that are available. These will also be available during the real exam.
- Attempt at least one question, including autotesting/submitting the program.
- Check the questions that you have submitted.
Please make sure you complete myExperience before the practice exam.