COMP1511 17s1 Introduction to Programming

Objectives

In these revision lab exercises you will revise topics relevant to the week 8/9 exam.

Preparation

Before the lab you should re-read the relevant lecture slides and their accompanying examples. You should also have read the lab assessment guidelines.

Getting Started

As usual create a new directory for this lab called lab08 by typing:
mkdir lab08
Change to this directory by typing:
cd lab08

Introduction

These revision exercises are designed to prepare you for the mid-session exam.

These revision exercises are not assessable.

They do not get marked.

There is no need to submit them with give.

Revision lab times will be announced which you can attend to get help with these revision exercises.

Revision Exercise: Read Ten

Write a C program read_ten.c which reads 10 integers from standard input, stores them in an array, and prints them on one line.

You may assume that the program's input will contain only integers and will contain at least 10 integers, in other words you can assume scanf succeeeds.

Match the the example below EXACTLY.

./read_ten
1
2
4
16
32
64
128
256
512
1024
Numbers were: 1 2 4 16 32 64 128 256 512 1024
As usual autotest is available to help you test your program.
 ~cs1511/bin/autotest lab08 read_ten.c

Revision Exercise: Reverse Ten

Write a C program reverse_ten.c which reads 10 integers from standard input, and prints them on them one line in reverse order.

Hint: use read_ten.c as a starting point.

You may assume that the program's input will contain only integers and will contain at least 10 integers, in other words you can assume scanf succeeeds.

Match the the example below EXACTLY.

./reverse_ten
1
2
4
16
32
64
128
256
512
1024
Numbers were: 1024 512 256 128 64 32 16 4 2 1
As usual autotest is available to help you test your program.
 ~cs1511/bin/autotest lab08 reverse_ten.c

Revision Exercise: Odd-Even Ten

Write a C program odd_even_ten.c which reads 10 integers from standard input, prints the odd numbers on one line, then prints the even numbers on one line.

Hint: use read_ten.c as a starting point.

You may assume that the program's input will contain only positive integers and will contain at least 10 integers, in other words you can assume scanf succeeeds.

Match the the example below EXACTLY.

./odd_even_ten
1
2
4
16
32
64
128
256
512
1024
Odd numbers were: 1
Even numbers were: 2 4 16 32 64 128 256 512 1024
./odd_even_ten
3
5
7
11
13
11
9
7
5
3
Odd numbers were: 3 5 7 11 13 11 9 7 5 3
Even numbers were:
As usual autotest is available to help you test your program.
 ~cs1511/bin/autotest lab08 odd_even_ten.c

Revision Exercise: Odd-Even Negative

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.

Hint: use odd_even_ten.c as a starting point.

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

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
As usual autotest is available to help you test your program.
 ~cs1511/bin/autotest lab08 odd_even_negative.c

Revision Exercise: Odd-Even EOF

Write a C program odd_even_eof.c which reads integers from standard input until it reaches end-of-input.

It should then print the odd numbers on one line then print the even numbers on one line.

Hint: use odd_even_negative.c as a starting point.

You may assume that the program's input will contain only integers.

You can assume at most 1000 integers will be read before end-of-input is reached.

Match the the example below EXACTLY.

./odd_even_eof
1
2
4
16
32
64
128
256
512
1024
2048
4096

Odd numbers were: 1
Even numbers were: 2 4 16 32 64 128 256 512 1024 2048 4096
./odd_even_eof
3
-3
4
-4
4

Odd numbers were: 3 -3
Even numbers were: 4 -4 4
As usual autotest is available to help you test your program.
 ~cs1511/bin/autotest lab08 odd_even_eof.c

Revision Exercise: Odd-Even Argument

Write a C program odd_even_argument.c which is given integers as command-line arguments, and prints the odd numbers on one line then prints the even numbers on one line.

Hint: use odd_even_eof.c as a starting point.

You may assume that the program's command line arguments will only be integers.

You can assume at most 1000 command line arguments will be supplied.

Match the the example below EXACTLY.

./odd_even_argument 1 2 4 16 32 64 128 256 512 1024 2048 4096 
Odd numbers were: 1
Even numbers were: 2 4 16 32 64 128 256 512 1024 2048 4096
./odd_even_argument 3 -3 4 -4 4 
Odd numbers were: 3 -3
Even numbers were: 4 -4 4
As usual autotest is available to help you test your program.
 ~cs1511/bin/autotest lab08 odd_even_argument.c

Revision Exercise: Indivisible

Write a C program indivisible.c which reads integers > 1 from standard input until it reaches end-of-input.

It should then print the integers read which are not exactly divisible by any other of the integers read.

In other words it should not print an integer if another integer that has been read is a factor of that number.

Hint: use odd_even_eof.c as a starting point.

You may assume that the program's input will contain only integers.

You may assume that all integers are > 1.

You can assume at most 1000 integers will be read before end-of-input is reached.

Match the the example below EXACTLY.

./indivisible
42
7
6
12

Indivisible numbers: 7 6
./indivisible
2
3
4
5
6
7
8
9
10

Indivisible numbers: 2 3 5 7
./indivisible
5
6
5

Indivisible numbers: 6
As usual autotest is available to help you test your program.
 ~cs1511/bin/autotest lab08 indivisible.c

Submission/Assessment

These revision exercises are not assessable.

They do not get marked.

There is no need to submit them with give.