# Week 03 Laboratory Exercises

### Objectives

• using complex if statements to control program execution
• using while loops for repetition
• building up combinations of loops

### Activities To Be Completed

The following is a list of all the activities available to complete this week...

Worth one mark in total:

• count_up_down
• vector_sum
• x

Worth half a mark in total:

• perfect
• wondrous
• sawtooth

Worth half a mark in total:

• boxes
• spiral

For your interest, but not for marks:

• decimal_spiral

### Preparation

Before the lab you should re-read the relevant lecture slides and their accompanying examples.

### Videos

The following short videos may be of some help to get started on the lab exercises.

If you have watched the videos and are still unsure how to start the exercises, ask your tutor for help :)

• Revision, structs:

### Exercise (●◌◌) : Count Up/Down

Write a C program count_up_down.c that reads one integer n and prints all integers from 0 to n inclusive one per line.

Note that if n is positive, we are counting up and if n is negative, we are counting down. For example:

dcc count_up_down.c -o count_up_down
./count_up_down
Enter number: 5
0
1
2
3
4
5
./count_up_down
Enter number: -3
0
-1
-2
-3


#### Assumptions/Restrictions/Clarifications

You may assume 0 will never be given as input.

You are not permitted to use arrays.

You do not have to do any error checking.

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

You can run an automated code style checker using the following command:
1511 style count_up_down.c


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

1511 autotest count_up_down


When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab03_count_up_down count_up_down.c


Note, even though this is a pair exercise, you both must run give from your own account before Monday 04 October 20:00 to obtain the marks for this lab exercise.

### Exercise (●◌◌) : Vector Sum

cp -n /web/cs1511/21T3/activities/vector_sum/vector_sum.c .


Complete the C program vector_sum.c which reads in two vectors with 3 integer components each and stores the sum of these components in a new vector to be printed.

Your job for this exercise is to complete the middle section (bolded above). There is a given struct vector called sum_vector which needs to store the sum of the two scanned-in vectors, everything else has already been completed.

When the program is completed, it will look like this when run.

./vector_sum
Please enter the values of the first vector (x, y, z): 1 2 3
Please enter the values of the second vector (x, y, z): 4 5 6
The resulting sum vector is:
x: 5
y: 7
z: 9

./vector_sum
Please enter the values of the first vector (x, y, z): 1 2 3
Please enter the values of the second vector (x, y, z): 1 2 3
The resulting sum vector is:
x: 2
y: 4
z: 6

./vector_sum
Please enter the values of the first vector (x, y, z): -5 30 -12
Please enter the values of the second vector (x, y, z): 15 -31 -30
The resulting sum vector is:
x: 10
y: -1
z: -42


### Assumptions/Restrictions/Hints

• You may assume that you will be given only integers as input.
• You may assume that all input is valid.
You can run an automated code style checker using the following command:
1511 style vector_sum.c


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

1511 autotest vector_sum


When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab03_vector_sum vector_sum.c


Note, even though this is a pair exercise, you both must run give from your own account before Monday 04 October 20:00 to obtain the marks for this lab exercise.

### Exercise (●◌◌) : Draw an X

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.

This exercise is designed to give you practice with while loops, if statements and some mathematical operators. Do not use arrays for this exercise!

Note: you are not permitted to use an array in this exercise.

./x
Enter size: 5
*---*
-*-*-
--*--
-*-*-
*---*
./x
Enter size: 9
*-------*
-*-----*-
--*---*--
---*-*---
----*----
---*-*---
--*---*--
-*-----*-
*-------*
./x
Enter size: 15
*-------------*
-*-----------*-
--*---------*--
---*-------*---
----*-----*----
-----*---*-----
------*-*------
-------*-------
------*-*------
-----*---*-----
----*-----*----
---*-------*---
--*---------*--
-*-----------*-
*-------------*

You can run an automated code style checker using the following command:
1511 style x.c


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

1511 autotest x


When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab03_x x.c


Note, even though this is a pair exercise, you both must run give from your own account before Monday 04 October 20:00 to obtain the marks for this lab exercise.

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

You can run an automated code style checker using the following command:
1511 style perfect.c


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

1511 autotest perfect


When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab03_perfect perfect.c


Note, even though this is a pair exercise, you both must run give from your own account before Monday 04 October 20:00 to obtain the marks for this lab exercise.

### Exercise (●●◌) : Wondrous

Write a program called wondrous.c that reads an integer n from standard input, and prints the wondrous numbers of it as a graph of asterisks.

The wondrous numbers of a given number n is the sequence generated from the Collatz Conjecture

The Collatz conjecture is a popular conjecture that has 3 steps.

1. Take a positive integer n.
2. If n is even, set n to be n / 2. Otherwise, set n to be 3n + 1.
3. Take the new n and repeat steps 1-3 until n is 1.

The conjecture states that, regardless of the value of n, the sequence created from the above steps will always reach 1.

For example, if the value of n is 3, following the steps in the conjecture gives the following sequence:

3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1

For this exercise, you will create a 2-dimensional representation of this sequence.

Instead of printing out the numbers of the sequence, you will print out n number of '*'s for each n in the sequence

For example:

./wondrous
What number would you like to see: 3
***
**********
*****
****************
********
****
**
./wondrous
What number would you like to see: 4
****
**
./wondrous
What number would you like to see: 5
*****
****************
********
****
**
./wondrous
What number would you like to see: 14
**************
*******
**********************
***********
**********************************
*****************
****************************************************
**************************
*************
****************************************
********************
**********
*****
****************
********
****
**

Note: You should not print out the last '*' that would correspond with the final convergence to '1'.
You can run an automated code style checker using the following command:
1511 style wondrous.c


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

1511 autotest wondrous


When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab03_wondrous wondrous.c


Note, even though this is a pair exercise, you both must run give from your own account before Monday 04 October 20:00 to obtain the marks for this lab exercise.

### Exercise (●●◌) : Sawtooth

This activity was moved later in the exercise after some students found it more difficult than it was intended to be. It's now a 2-circle exericse, rather than a 1-circle exercise.

Write a program sawtooth.c that reads in two non-negative integers, a height and a length. The height represents how tall the sawtooth pattern will be and the length represents how long the sawtooth will be.

A sawtooth pattern looks like the below:

*     *     *     *     *     *
**    **    **    **    **    **
* *   * *   * *   * *   * *   * *
*  *  *  *  *  *  *  *  *  *  *  *
*   * *   * *   * *   * *   * *   *
*    **    **    **    **    **    *

In this example, the height is 6 and the length is 36

Here are some examples of how your program should run:

 ./sawtooth
Please enter the height of the sawtooth: 4
Please enter the length of the sawtooth: 16
*   *   *   *
**  **  **  **
* * * * * * * *
*  **  **  **  *
./sawtooth
Please enter the height of the sawtooth: 6
Please enter the length of the sawtooth: 36
*     *     *     *     *     *
**    **    **    **    **    **
* *   * *   * *   * *   * *   * *
*  *  *  *  *  *  *  *  *  *  *  *
*   * *   * *   * *   * *   * *   *
*    **    **    **    **    **    *

./sawtooth
Please enter the height of the sawtooth: 8
Please enter the length of the sawtooth: 45
*       *       *       *       *       *
**      **      **      **      **      **
* *     * *     * *     * *     * *     * *
*  *    *  *    *  *    *  *    *  *    *  *
*   *   *   *   *   *   *   *   *   *   *   *
*    *  *    *  *    *  *    *  *    *  *
*     * *     * *     * *     * *     * *
*      **      **      **      **      **


Note: When you are testing your program with larger inputs, you may need to change the size of your terminal to prevent output text from wrapping around the screen. If you do not know how to do this, ask your tutor during the lab time

### Hints

• You will need to use 2D while loops for this exercise
• Think about the rows and columns of your loops, what relationship do they have with height/length of the pattern?
• Look at the first triangle in the patterns, what relationship do the columns and the rows have in the diagonal? Try to match the indices below
  012345
0 *     *     *     *     *     *
1 **    **    **    **    **    **
2 * *   * *   * *   * *   * *   * *
3 *  *  *  *  *  *  *  *  *  *  *  *
4 *   * *   * *   * *   * *   * *   *
5 *    **    **    **    **    **    *

• How could you apply the above rule to later triangles? You may find the % operator helpful.
• You will likely need to check two things to print out every *, one check for the diagonals and one for the straight lines. In all other cases, you want to print spaces.

### Assumptions

• All heights will be non-negative integers
• All lengths will be positive integers. Although a length of 0 is possible, it can be interpreted differently and will not be tested.
You can run an automated code style checker using the following command:
1511 style sawtooth.c


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

1511 autotest sawtooth


When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab03_sawtooth sawtooth.c


Note, even though this is a pair exercise, you both must run give from your own account before Monday 04 October 20:00 to obtain the marks for this lab exercise.

### Exercise (●●●) : Boxes

For this exercise, make a program called boxes.c which reads in a number and then draws that many square boxes inside each other using asterisks.

You can assume that you will not be tested on any values of n above 50.

For example:

./boxes
How many boxes: 1
***
*-*
***

./boxes
How many boxes: 2
*******
*-----*
*-***-*
*-*-*-*
*-***-*
*-----*
*******

./boxes
How many boxes: 5
*******************
*-----------------*
*-***************-*
*-*-------------*-*
*-*-***********-*-*
*-*-*---------*-*-*
*-*-*-*******-*-*-*
*-*-*-*-----*-*-*-*
*-*-*-*-***-*-*-*-*
*-*-*-*-*-*-*-*-*-*
*-*-*-*-***-*-*-*-*
*-*-*-*-----*-*-*-*
*-*-*-*******-*-*-*
*-*-*---------*-*-*
*-*-***********-*-*
*-*-------------*-*
*-***************-*
*-----------------*
*******************

You can run an automated code style checker using the following command:
1511 style boxes.c


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

1511 autotest boxes


When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab03_boxes boxes.c


Note, even though this is a pair exercise, you both must run give from your own account before Monday 04 October 20:00 to obtain the marks for this lab exercise.

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

This exercise must be completed without arrays.

Make your program match the examples below exactly.

./spiral
Enter size: 5
*****
----*
***-*
*---*
*****
./spiral
Enter size: 7
*******
------*
*****-*
*---*-*
*-***-*
*-----*
*******
./spiral
Enter size: 9
*********
--------*
*******-*
*-----*-*
*-***-*-*
*-*---*-*
*-*****-*
*-------*
*********
./spiral
Enter size: 17
*****************
----------------*
***************-*
*-------------*-*
*-***********-*-*
*-*---------*-*-*
*-*-*******-*-*-*
*-*-*-----*-*-*-*
*-*-*-***-*-*-*-*
*-*-*-*---*-*-*-*
*-*-*-*****-*-*-*
*-*-*-------*-*-*
*-*-*********-*-*
*-*-----------*-*
*-*************-*
*---------------*
*****************

You can run an automated code style checker using the following command:
1511 style spiral.c


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

1511 autotest spiral


When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab03_spiral spiral.c


Note, even though this is a pair exercise, you both must run give from your own account before Monday 04 October 20:00 to obtain the marks for this lab exercise.

### Exercise (☠) : Decimal Spiral (extra-hard)

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.

This exercise must be done without arrays.

Make your program match the examples below exactly.

./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

You can run an automated code style checker using the following command:
1511 style decimal_spiral.c


### Submission

When you are finished each exercises make sure you submit your work by running give.

You can run give multiple times. Only your last submission will be marked.

Don't submit any exercises you haven't attempted.

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

Remember you have until Week 4 Monday 20:00 to submit your work.

You cannot obtain marks by e-mailing your code to tutors or lecturers.

You check the files you have submitted here.

Automarking will be run by the lecturer several days after the submission deadline, using test cases different to those autotest runs for you. (Hint: do your own testing as well as running autotest.)

After automarking is run by the lecturer you can view your results here. The resulting mark will also be available via give's web interface.

#### Lab Marks

When all components of a lab are automarked you should be able to view the the marks via give's web interface or by running this command on a CSE machine:

1511 classrun -sturec