COMP1511 18s1 (webcms)
COMP1511 18s1 (flask)

Objectives

In this Lab, you will:
  • Become familiar with the environment used for the final exam
  • practice coding under exam conditions

Getting Started

The first 10 minutes of the lab is set aside for you to complete the myExperience survey for COMP1511. Your tutors will leave the room to ensure your answers stay confidential.

This will also leave time for everyone to arrive and be ready to start the exam.

Logging Into the Exam environment

Log out of your regular account after completing the myExperience survey.

Your tutors will log your lab machine into the exam environment,

When all machines are logged into the exam environment tutors will give you further instructions.

At this stage turn off your phone, put it and laptops out of reach.

Practice Exam

You will have 5 minutes reading time after that the exam runs 60 minutes.

Part 1 has to be submitted in the first 5 minutes and during that time you are not permitted to run xterms, shells, editors, etc. You can view online documentation.

You run "Enter Part 1 Answers" from the right mouse button menu to enter the answers to part 1.

In the final exam you will have 30 minutes to complete part 1.

You run "View Part 2 Questions" from the right mouse button menu to see part 2 questions.

Part 2 answers are entered into the file specified by each question and submitted using the "give" command. The questions include submission instructions.

The practice exam will be conducted under simulated exam conditions. which means you can't communicate with other people, can not use your phone/tablet/laptop, ...

You tutor can clarify questions and fix any problems with the exam environment.

They can not help you solve the tasks.

Some later lab sessions may have the same questions as yours. Please don't tell people what questions you had. And equally don't try to find out beforehand what questions were used in other labs. You'll get most value, if this practice exam is as real as possible.

Practice Exam Assessment

This work you submit in the exam environment will be automarked and used to calculate your last lab mark.

You can not submit answers outside the exam environment.

You can only submit answers during the hour of the exam.

Assignment 3 Help

There should be time after the exam if you need help with assignment 3.

Practice Exam Part 1 Questions - Version 1

Question 1 (1 mark)

The file question.c contains this C Code:

#include <stdio.h>

int main(void) {
    int x = 11;
    int y = 3;
    printf("%d\n", x / y);
    return 0;
}

question.c is compiled with dcc on a CSE machine like this:

dcc question.c -o question

It compiles successfully. No errors or warnings are produced by dcc. The program is run like this:

./question

What does this program print?

Enter as your answer the output the program produces. Do not enter any extra characters. Do not enter any explanation.

Enter exactly the output the program produces.

If the program prints an error message just write ERROR. Do not enter the exact error message.

Question 2 (1 mark)

The file q2.c contains this C Code:

#include <stdio.h>

int main(int argc, char *argv[]) {
    printf("%d\n", argc);
    return 0;
}

q2.c is compiled with dcc on a CSE machine like this:

dcc q2.c -o q2

It compiles successfully. No errors or warnings are produced by dcc. The program is run like this:

./q2 hello world

What does this program print?

Enter as your answer the output the program produces. Do not enter any extra characters. Do not enter any explanation.

Enter exactly the output the program produces.

If the program prints an error message just write ERROR. Do not enter the exact error message.

Question 3 (1 mark)

The file q3.c contains this C Code:

#include <stdio.h>
#include <string.h>

int main(void) {
    char *s = "hello";
    char i = strlen(s);
    printf("%c%d\n", s[0], i);
    return 0;
}

q3.c is compiled with dcc on a CSE machine like this:

dcc q3.c -o q3

It compiles successfully. No errors or warnings are produced by dcc. The program is run like this:

./q3

What does this program print?

Enter as your answer the output the program produces. Do not enter any extra characters. Do not enter any explanation.

Enter exactly the output the program produces.

If the program prints an error message just write ERROR. Do not enter the exact error message.

Practice Exam Part 1 Questions - Version 2

The file question.c contains this C Code:

#include <stdio.h>

int main(void) {
    int x = 11;
    int y = 3;
    printf("%d\n", x % y);
    return 0;
}

question.c is compiled with dcc on a CSE machine like this:

dcc question.c -o question

It compiles successfully. No errors or warnings are produced by dcc. The program is run like this:

./question

What does this program print?

Enter as your answer the output the program produces. Do not enter any extra characters. Do not enter any explanation.

Enter exactly the output the program produces.

If the program prints an error message just write ERROR. Do not enter the exact error message.

Question 2 (1 mark)

The file q2.c contains this C Code:

#include <stdio.h>

int main(int argc, char *argv[]) {
    printf("%s\n", argv[1]);
    return 0;
}

q2.c is compiled with dcc on a CSE machine like this:

dcc q2.c -o q2

It compiles successfully. No errors or warnings are produced by dcc. The program is run like this:

./q2 hello world

What does this program print?

Enter as your answer the output the program produces. Do not enter any extra characters. Do not enter any explanation.

Enter exactly the output the program produces.

If the program prints an error message just write ERROR. Do not enter the exact error message.

Question 3 (1 mark)

The file q3.c contains this C Code:

#include <stdio.h>
#include <string.h>

int main(void) {
    char *s = "hello";
    printf("%d\n", strcmp(s, s));
    return 0;
}

q3.c is compiled with dcc on a CSE machine like this:

dcc q3.c -o q3

It compiles successfully. No errors or warnings are produced by dcc. The program is run like this:

./q3

What does this program print?

Enter as your answer the output the program produces. Do not enter any extra characters. Do not enter any explanation.

Enter exactly the output the program produces.

If the program prints an error message just write ERROR. Do not enter the exact error message.

Practice Exam Part 2

Question 1 (version 1)

This is an individual exercise to complete by yourself.

Write a C program product_odd.c that reads 2 positive integers and calculates the product of the odd integers between the first and second integers.

For example, if the two integers entered are 10 and 16 then 2145 should be printed because 11 * 13 * 15 = 2145.

If there are no odd integers between the first and second integers, your program should print 1.

Your program should behave exactly like these examples:

dcc product_odd.c -o product_odd
./product_odd 
Enter lower: 10
Enter upper: 16
2145
./product_odd 
Enter lower: 4
Enter upper: 8
35
./product_odd 
Enter lower: 5
Enter upper: 7
1
./product_odd 
Enter lower: 5
Enter upper: 9
7

You can assume the user supplies 2 positive integers.

You can assume the first integer is smaller than the second.

You can assume that the product will fit in an ''int''. In other words you can assume overflow does not occur.

You do not have do any error checking.

You do not have to check the return value from scanf.

When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest product_odd

Autotest Results

97% of 910 students who have autotested product_odd.c so far, passed all autotest tests.
  • 98% passed test 0
  • 98% passed test 1
  • 98% passed test 2
  • 98% passed test 3
  • 98% passed test 4
  • 98% passed test 5
  • 98% passed test 6
  • 98% passed test 7
When you are finished working on this exercise you must submit your work by running give:
give cs1511 wk13_product_odd product_odd.c
You must run give before Thursday 12 April 23:59:59 to obtain the marks for this lab exercise. Note, this is an individual exercise, the work you submit with give must be entirely your own.

Question 1 (version 2)

This is an individual exercise to complete by yourself.
Download list_increasing.c, the starting code for this exercise or use this cp command:

cp -n /web/cs1511/18s1/activities/array_count_odd/array_count_odd.c .
Your task is to add code to this function:
// return the number of odd values in an array.
int count_odd(int length, int array[]) {
    // PUT YOUR CODE HERE (you must change the next line!)
    return 42;
}

Add code so that count_odd returns the number of odd values in the array.

For example if the array contains these 8 elements:

16, 7, 8, 12, 13, 19, 13, 12

Your function should return 4, because these 4 elements are odd:

7, 13, 19, 13

Testing

array_count_odd.c also contains a simple main function which allows you to test your count_odd function.

Your count_odd function will be called directly in marking. The main function is only to let you test your count_odd function

Assumptions/Restrictions/Clarifications.

An odd number is not divisible by 2.

count_odd should return a single integer.

count_odd should not change the array it is given.

count_odd should not call scanf (or getchar or fgets).

count_odd can assume the array only contains positive integers.

count_odd can assume the array contains at least one integer.

count_odd function should not print anything. It should not call printf.

Your submitted file may contain a main function. It will not be tested or marked.

When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest array_count_odd

Autotest Results

99% of 87 students who have autotested array_count_odd.c so far, passed all autotest tests.
  • 99% passed test 1
  • 99% passed test 2
  • 99% passed test 3
  • 99% passed test 4
  • 99% passed test 5
  • 99% passed test 6
When you are finished working on this exercise you must submit your work by running give:
give cs1511 wk13_array_count_odd array_count_odd.c
You must run give before Friday 1 June 18:00:00 to obtain the marks for this lab exercise. Note, this is an individual exercise, the work you submit with give must be entirely your own.

Question 1 (version 3)

This is an individual exercise to complete by yourself.
Download array_count_small.c, the starting code for this exercise or use this cp command:

cp -n /web/cs1511/18s1/activities/array_count_small/array_count_small.c .
Your task is to add code to this function:
// return the number of small values in an array.
int count_small(int length, int array[]) {
    // PUT YOUR CODE HERE (you must change the next line!)
    return 42;
}

Add code so that count_small returns the number of small values in the array.

For example if the array contains these 9 elements:

16, 7, 8, 12, 13, -9, -3, 12, -9

Your function should return 5, because these 5 elements are small:

7, 8, -9, -3, -9

Testing

array_count_small.c also contains a simple main function which allows you to test your count_small function.

Your count_small function will be called directly in marking. The main function is only to let you test your count_small function

Assumptions/Restrictions/Clarifications.

An small number is > 1-10 and < 10

count_small should return a single integer.

count_small should not change the array it is given.

count_small should not call scanf (or getchar or fgets).

count_small can assume the array contains at least one integer.

count_small function should not print anything. It should not call printf.

Your submitted file may contain a main function. It will not be tested or marked.

When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest array_count_small

Autotest Results

98% of 85 students who have autotested array_count_small.c so far, passed all autotest tests.
  • 99% passed test 1
  • 99% passed test 2
  • 99% passed test 3
  • 98% passed test 4
  • 98% passed test 5
When you are finished working on this exercise you must submit your work by running give:
give cs1511 wk13_array_count_small array_count_small.c
You must run give before Friday 1 June 18:00:00 to obtain the marks for this lab exercise. Note, this is an individual exercise, the work you submit with give must be entirely your own.

Practice Exam Part 2 - Question 2

Question 2 (version 1)

This is an individual exercise to complete by yourself.
Write a C program odd_even_negative.c which reads integers from standard input until it reads a negative integer. It should then print the odd numbers on one line then print the even numbers on one line.

You may assume that the program's input will contain only integers, in other words you can assume scanf succeeds.

You can assume a negative integer will always be read.

You can assume at most 1000 integers are read before a negative integer is read.

Match the the example below EXACTLY.

./odd_even_negative
1
2
3
2
-42
Odd numbers were: 1 3
Even numbers were: 2 2
./odd_even_negative
1
2
4
16
32
64
128
256
512
1024
2048
4096
-8192
Odd numbers were: 1
Even numbers were: 2 4 16 32 64 128 256 512 1024 2048 4096
Hint: use odd_even_ten.c as a starting point.

When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest odd_even_negative

Autotest Results

96% of 893 students who have autotested odd_even_negative.c so far, passed all autotest tests.
  • 96% passed test 0
  • 96% passed test 1
  • 96% passed test 2
  • 97% passed test 3
  • 96% passed test 4
  • 96% passed test 5
When you are finished working on this exercise you must submit your work by running give:
give cs1511 wk13_odd_even_negative odd_even_negative.c
You must run give before Wednesday 18 April 23:59:59 to obtain the marks for this lab exercise. Note, this is an individual exercise, the work you submit with give must be entirely your own.

Question 2 (version 2)

This is an individual exercise to complete by yourself.
Write a C program eof_even.c which reads integers from standard input until it reaches end-of-input.

It should then print the even integers on a single line, in the order they occurred.

Match the the example below EXACTLY.

dcc eof_even.c -o ./eof_even
./eof_even
1
4
1
5
6
2
6
9

4 6 6

Explanation: given the input 1 4 1 5 6 2 6 9, your program should print 4 6 6, as those are the even values.

Assumptions/Restrictions/Clarifications.

Your program must read until the end of-input. End of input is signalled on a Linux terminal by typing the Ctrl and d keys together. This is what indicates in the above examples.

You can assume the input will only only contain positive integers, one per line.

You can assume each line will contain one and only one integer.

You can assume your input contains at least one integer.

You can assume your input contains no more than 10000 integers.

You can assume no integer will be smaller than 1.

You are free to write this program in any way you wish: there is no specific function that you need to implement. Note that your program will need to have a `main` function.

When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest eof_even

Autotest Results

92% of 91 students who have autotested eof_even.c so far, passed all autotest tests.
  • 93% passed test 1
  • 92% passed test 2
  • 96% passed test 3
  • 96% passed test 4
  • 95% passed test 5
  • 95% passed test 6
  • 96% passed test 7
  • 95% passed test 8
When you are finished working on this exercise you must submit your work by running give:
give cs1511 wk13_eof_even eof_even.c
You must run give before Friday 1 June 18:00:00 to obtain the marks for this lab exercise. Note, this is an individual exercise, the work you submit with give must be entirely your own.

Question 2 (version 3)

This is an individual exercise to complete by yourself.
Write a C program eof_count_last.c which reads integers from standard input until it reaches end-of-input.

It should then print the number of times the last integer read occurred in in its input.

Match the the examples below EXACTLY.

dcc eof_count_last.c -o ./eof_count_last
./eof_count_last
1
4
1
5
6
2
6
1

3

Explanation: given the input 1 4 1 5 6 2 6 1, your program should print 3, as the last number read was 1 and it occurred 3 times in its input.
./eof_count_last
10
20
30
40


1
./eof_count_last
10
20
10
20
10

3
./eof_count_last
42
42
42
42
42
42
42

7

Assumptions/Restrictions/Clarifications.

Your program must read until the end of-input. End of input is signalled on a Linux terminal by typing the Ctrl and d keys together. This is what indicates in the above examples.

You can assume the input will only only contain positive integers, one per line.

You can assume each line will contain one and only one integer.

You can assume your input contains at least one integer.

You can assume your input contains no more than 10000 integers.

You can assume no integer will be smaller than 1.

You are free to write this program in any way you wish: there is no specific function that you need to implement. Note that your program will need to have a `main` function.

When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest eof_count_last

Autotest Results

95% of 76 students who have autotested eof_count_last.c so far, passed all autotest tests.
  • 96% passed test 1
  • 95% passed test 2
  • 95% passed test 3
  • 95% passed test 4
  • 95% passed test 5
When you are finished working on this exercise you must submit your work by running give:
give cs1511 wk13_eof_count_last eof_count_last.c
You must run give before Friday 1 June 18:00:00 to obtain the marks for this lab exercise. Note, this is an individual exercise, the work you submit with give must be entirely your own.

Practice Exam Part 2 - Question 3

Question 3 (version 1)

This is an individual exercise to complete by yourself.
Download count_even_list.c, the starting code for this exercise or use this cp command:

cp -n /web/cs1511/18s1/activities/count_even_list/count_even_list.c .
Note count_even_list.c uses the following familiar data type:
struct node {
    struct node *next;
    int          data;
};
Your task is to add code to this function:
// return the number of even values in a linked list
int count_even(struct node *head) {

    // PUT YOUR CODE HERE (change the next line!)
    return 42;

}

count_even is given one argument, head, which is the pointer to the first node in a linked list.

Add code to count_even so that its returns the number of even values in the linked list.

For example if the linked list contains these 8 elements:

16, 7, 8, 12, 13, 19, 21, 12

count_even should return 4, because these 4 elements are even:

16, 8, 12, 12

Testing

count_even_list.c also contains a main function which allows you to test your count_even function.

This main function:

  • converts the command-line arguments to a linked list
  • assigns a pointer to the first node in the linked list to head
  • calls count_even(head)
  • prints the result.

Do not change this main function. If you want to change it, you have misread the question.

Your count_even function will be called directly in marking. The main function is only to let you test your count_even function

Here is how you use main function allows you to test count_even:

cp -n /web/cs1511/18s1/activities/count_even_list/count_even_list.c .
dcc count_even_list.c -o count_even_list
./count_even_list 16 7 8 12 13 19 21 12
4
./count_even_list 2 4 6 2 4 6
6
./count_even_list 3 5 7 11 13 15 17 19 23 29
0
./count_even_list 2 4 8 16 32 64 128 256
8
./count_even_list
0

Assumptions/Restrictions/Clarifications.

An even number is divisible by 2.

count_even should return a single integer.

count_even should not change the linked list it is given. Your function should not change the next or data fields of list nodes.

count_even should not use arrays.

count_even should not call malloc.

count_even should not call scanf (or getchar or fgets).

You can assume the linked list only contains positive integers.

count_even should not print anything. It should not call printf.

Do not change the supplied main function. It will not be tested or marked.

When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest count_even_list

Autotest Results

99% of 783 students who have autotested count_even_list.c so far, passed all autotest tests.
  • 99% passed test 1
  • 99% passed test 10
  • 99% passed test 2
  • 99% passed test 3
  • 99% passed test 4
  • 99% passed test 5
  • 99% passed test 6
  • 99% passed test 7
  • 99% passed test 8
  • 99% passed test 9
When you are finished working on this exercise you must submit your work by running give:
give cs1511 wk13_count_even_list count_even_list.c
You must run give before Friday 25 May 23:59:59 to obtain the marks for this lab exercise. Note, this is an individual exercise, the work you submit with give must be entirely your own.

Question 3 (version 2)

This is an individual exercise to complete by yourself.
Download list_increasing.c here, or copy it to your CSE account using the following command:
cp -n /web/cs1511/18s1/activities/list_increasing/list_increasing.c .
Your task is to add code to this function in list_increasing.c:
int increasing(struct node *head) {

    // PUT YOUR CODE HERE (change the next line!)
    return 42;

}

increasing is given one argument, head, which is the pointer to the first node in a linked list.

Add code to increasing so that its returns 1 if the list is in increasing order - the value of each list element is larger than the element before.

For example if the linked list contains these 8 elements:

1, 7, 8, 9, 13, 19, 21, 42

increasing should return 1 because is is increasing order

Testing

list_increasing.c also contains a main function which allows you to test your increasing function.

This main function:

  • converts the command-line arguments to a linked list
  • assigns a pointer to the first node in the linked list to head
  • calls list_increasing(head)
  • prints the result.

Do not change this main function. If you want to change it, you have misread the question.

Your list_increasing function will be called directly in marking. The main function is only to let you test your list_increasing function

Here is how you use main function allows you to test list_increasing:

dcc list_increasing.c -o list_increasing
./list_increasing 1 2 4 8 16 32 64 128 256
1
./list_increasing 2 4 6 5 8 9
0
./list_increasing 13 15 17 17 18 19
0
./list_increasing 2 4
1
./list_increasing 42
1
./list_increasing
1

Assumptions/Restrictions/Clarifications.

increasing should return a single integer.

increasing should not change the linked list it is given. Your function should not change the next or data fields of list nodes.

increasing should not use arrays.

increasing should not call malloc.

increasing should not call scanf (or getchar or fgets).

You can assume the linked list only contains positive integers.

increasing should not print anything. It should not call printf.

Do not change the supplied main function. It will not be tested or marked.

When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest list_increasing

Autotest Results

96% of 490 students who have autotested list_increasing.c so far, passed all autotest tests.
  • 99% passed test 1
  • 97% passed test 10
  • 98% passed test 2
  • 98% passed test 3
  • 99% passed test 4
  • 98% passed test 5
  • 98% passed test 6
  • 98% passed test 7
  • 97% passed test 8
  • 98% passed test 9
When you are finished working on this exercise you must submit your work by running give:
give cs1511 wk13_list_increasing list_increasing.c
You must run give before Friday 1 June 18:00:00 to obtain the marks for this lab exercise. Note, this is an individual exercise, the work you submit with give must be entirely your own.

Question 3 (version 3)

This is an individual exercise to complete by yourself.
Download list_second_last.c, the starting code for this exercise or use this cp command:

cp -n /web/cs1511/18s1/activities/list_second_last/list_second_last.c .
Note list_second_last.c uses the following familiar data type:
struct node {
    struct node *next;
    int          data;
};
Your task is to add code to this function:
int second_last(struct node *head) {

    // PUT YOUR CODE HERE (change the next line!)
    return 42;

}

second_last is given one argument, head, which is the pointer to the first node in a linked list.

Add code to second_last so that its returns the second last element of the list.

For example if the linked list contains these 8 elements:

1, 7, 8, 9, 13, 19, 21, 42

second_last should return 21 because this is the second last element.

second_last can assume the list has at least two elements.

Testing

list_second_last.c also contains a main function which allows you to test your second_last function.

This main function:

  • converts the command-line arguments to a linked list
  • assigns a pointer to the first node in the linked list to head
  • calls list_second_last(head)
  • prints the result.

Do not change this main function. If you want to change it, you have misread the question.

Your list_second_last function will be called directly in marking. The main function is only to let you test your list_second_last function

Here is how you use main function allows you to test list_second_last:

cp -n /web/cs1511/18s1/activities/list_second_last/list_second_last.c .
dcc list_second_last.c -o list_second_last
./list_second_last 1 2 4 8 16 32 64 128 256
128
./list_second_last 2 4 6 5 8 9
8
./list_second_last 13 15 17 17 18 19
18
./list_second_last 2 4
2

Assumptions/Restrictions/Clarifications.

second_last will be given a list containing at least two elements.

second_last should return a single integer.

second_last should not change the linked list it is given. Your function should not change the next or data fields of list nodes.

second_last should not use arrays.

second_last should not call malloc.

second_last should not call scanf (or getchar or fgets).

You can assume the linked list only contains positive integers.

second_last should not print anything. It should not call printf.

Do not change the supplied main function. It will not be tested or marked.

When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest list_second_last

Autotest Results

100% of 84 students who have autotested list_second_last.c so far, passed all autotest tests.
When you are finished working on this exercise you must submit your work by running give:
give cs1511 wk13_list_second_last list_second_last.c
You must run give before Friday 1 June 18:00:00 to obtain the marks for this lab exercise. Note, this is an individual exercise, the work you submit with give must be entirely your own.