COMP1511 17s1 Introduction to Programming

### Objectives

In this Lab, you will practice:
• using complex if statements to control program execution
• using a while loop for repetition
• input & output of numerical values
• implementing simple numerical calculations

### 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

One member of your programming pair should login and run the following commands inside a Linux terminal

Create a new directory for this lab called `lab04` by typing:

```mkdir lab04
```
Change to this directory by typing:
```cd lab04
```

### Exercise: Three or Five

Write a program that three_five.c that reads a positive integer n and print all the positive integers < n divisible by 3 or 5.

For example:

``` ./three_five
Enter number: 10
3
5
6
9
./three_five
Enter number: 30
3
5
6
9
10
12
15
18
20
21
24
25
27
```
``` ~cs1511/bin/autotest lab04 three_five.c
```

### Exercise: Are You Perfect?

Write a program `perfect.c` that reads a positive integer n from standard input and prints all the factors of n, their sum and if indicates if n is a perfect number.
``` ./perfect
Enter number: 6
The factors of 6 are:
1
2
3
6
Sum of factors = 12
6 is a perfect number
./perfect
Enter number: 1001
The factors of 1001 are:
1
7
11
13
77
91
143
1001
Sum of factors = 1344
1001 is not a perfect number
```

``` ~cs1511/bin/autotest lab04 perfect.c
```

### Exercise: X Factor

Write a program called `x.c` that reads an integer n from standard input. and prints an nxn pattern of asterisks and dashes in the shape of an "X".

You can assume n is odd and >= 5.

Make your program match the examples below exactly.

You are not permitted to use an array in this exercise.

```./x
Enter size: 5
*---*
-*-*-
--*--
-*-*-
*---*
./x
Enter size: 9
*-------*
-*-----*-
--*---*--
---*-*---
----*----
---*-*---
--*---*--
-*-----*-
*-------*
./x
Enter size: 15
*-------------*
-*-----------*-
--*---------*--
---*-------*---
----*-----*----
-----*---*-----
------*-*------
-------*-------
------*-*------
-----*---*-----
----*-----*----
---*-------*---
--*---------*--
-*-----------*-
*-------------*
```
``` ~cs1511/bin/autotest lab04 x.c
```

### Exercise: Plus

Write a program called `plus.c` that reads an integer n from standard input. and prints an nxn pattern of asterisks and dashes in the shape of an "+".

You can assume n is odd and >= 5.

Make your program match the examples below exactly.

You are not permitted to use an array in this exercise.

```./plus
Enter size: 7
---*---
---*---
---*---
*******
---*---
---*---
---*---
./plus
Enter size: 13
------*------
------*------
------*------
------*------
------*------
------*------
*************
------*------
------*------
------*------
------*------
------*------
------*------
```
``` ~cs1511/bin/autotest lab04 plus.c
```

### Exercise: Chessboard

Write a program called `chessboard.c` that reads an integer n from standard input. and prints an nxn pattern of asterisks and dashes in the shape of a chessboard.

You can assume n is odd and >= 5.

Make your program match the examples below exactly.

You are not permitted to use an array in this exercise.

```./chessboard
Enter size: 5
-*-*-
*-*-*
-*-*-
*-*-*
-*-*-
./chessboard
Enter size: 7
-*-*-*-
*-*-*-*
-*-*-*-
*-*-*-*
-*-*-*-
*-*-*-*
-*-*-*-
./chessboard
Enter size: 17
-*-*-*-*-*-*-*-*-
*-*-*-*-*-*-*-*-*
-*-*-*-*-*-*-*-*-
*-*-*-*-*-*-*-*-*
-*-*-*-*-*-*-*-*-
*-*-*-*-*-*-*-*-*
-*-*-*-*-*-*-*-*-
*-*-*-*-*-*-*-*-*
-*-*-*-*-*-*-*-*-
*-*-*-*-*-*-*-*-*
-*-*-*-*-*-*-*-*-
*-*-*-*-*-*-*-*-*
-*-*-*-*-*-*-*-*-
*-*-*-*-*-*-*-*-*
-*-*-*-*-*-*-*-*-
*-*-*-*-*-*-*-*-*
-*-*-*-*-*-*-*-*-
```
``` ~cs1511/bin/autotest lab04 chessboard.c
```

### Exercise: It's Prime

Write a program called prime_factors.c that reads a integer n from standard input and prints the decomposition of n into prime factors. If n is prime it instead should print a message indicating this. You may assume n > 2. Make your program match the examples below exactly.
```./prime_factors
Enter number: 6
The prime factorization of 6 is:
2 * 3 = 6
./prime_factors
Enter number: 30
The prime factorization of 30 is:
2 * 3 * 5 = 30
./prime_factors
Enter number: 22501
22501 is prime
./prime_factors
Enter number: 2048
The prime factorization of 2048 is:
2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 = 2048
./prime_factors
Enter number: 22500
The prime factorization of 22500 is:
2 * 2 * 3 * 3 * 5 * 5 * 5 * 5 = 22500
./prime_factors
Enter number: 22501
22501 is prime
```
Hint: prime_factors.c requires a complex flow of control. Sketch out on paper the structure of your program. Your tutor may ask to see this.

Hint: prime_factors.c requires a declaring a number of variables and thinking carefully about how they will be used (choose good names). If you don't have the right variables, you'll get stuck writing the if and while statements.

Hint: prime_factors.c will require difficult debugging - think about where you can printf statements to see the value of variables and understand what your program is doing.

You are not permitted to use an array in this exercise.

``` ~cs1511/bin/autotest lab04 prime_factors.c
```

### Challenge Exercise: Spiral

Write a program called `spiral.c` that reads an integer n from standard input. and prints an nxn pattern of asterisks and dashes in the shape of a spiral.

You can assume n is odd and >= 5.

You are only permitted to use C language features covered in weeks 1-3 lectures. In particular, you are not permitted to use array(s).

Make your program match the examples below exactly.

You are not permitted to use an array in this exercise.

```./spiral
Enter size: 5
*****
----*
***-*
*---*
*****
./spiral
Enter size: 7
*******
------*
*****-*
*---*-*
*-***-*
*-----*
*******
./spiral
Enter size: 9
*********
--------*
*******-*
*-----*-*
*-***-*-*
*-*---*-*
*-*****-*
*-------*
*********
./spiral
Enter size: 17
*****************
----------------*
***************-*
*-------------*-*
*-***********-*-*
*-*---------*-*-*
*-*-*******-*-*-*
*-*-*-----*-*-*-*
*-*-*-***-*-*-*-*
*-*-*-*---*-*-*-*
*-*-*-*****-*-*-*
*-*-*-------*-*-*
*-*-*********-*-*
*-*-----------*-*
*-*************-*
*---------------*
*****************
```
Try to invent your own solution - don't google or ask others how do it. Tutors want to see your original work even if it only partly works

``` ~cs1511/bin/autotest lab04 spiral.c
```

### Challenge Exercise: Decimal Spiral

Write a program called `decimal_spiral.c` that reads an integer n from standard input. and prints an nxn pattern of decimal digits and dashes in the shape of a spiral.

You can assume n is odd and >= 5.

You are only permitted to use C language features covered in weeks 1-3 lectures. In particular, you are not permitted to use array(s).

Make your program match the examples below exactly.

You are not permitted to use an array in this exercise.

```./decimal_spiral
Enter size: 5
65432
----1
210-0
3---9
45678
./decimal_spiral
Enter size: 7
0987654
------3
87654-2
9---3-1
0-012-0
1-----9
2345678
./decimal_spiral
Enter size: 9
876543210
--------9
8765432-8
9-----1-7
0-210-0-6
1-3---9-5
2-45678-4
3-------3
456789012
./decimal_spiral
Enter size: 15
654321098765432
--------------1
2109876543210-0
3-----------9-9
4-210987654-8-8
5-3-------3-7-7
6-4-87654-2-6-6
7-5-9---3-1-5-5
8-6-0-012-0-4-4
9-7-1-----9-3-3
0-8-2345678-2-2
1-9---------1-1
2-01234567890-0
3-------------9
456789012345678
```
Try to invent your own solution - don't google or ask others how do it. Tutors want to see your original work even if it only partly works - this is a really hard problem.

``` ~cs1511/bin/autotest lab04 decimal_spiral.c
```

### Submission/Assessment

When you are satisfied with your work, ask your tutor to assess it. You also need to submit your work electronically by typing (run this command in the `lab04` directory):
```give cs1511 lab04 three_five.c perfect.c x.c plus.c chessboard.c prime_factors.c spiral.c decimal_spiral.c
```
Submit the challenge exercises only if you attempt them.

If you are working at home, you may find it more convenient to upload your work via give's web interface.

Remember the lab assessment guidelines - if you don't finish the exercises you can finish them in your own time, submit them by Monday 11:00am using `give` and ask your tutor to assess them at the start of the following lab.

Either or both members of a programming pair can submit the work (make sure each program lists both of you as authors in the header comment).