Programming Fundamentals

Information

  • This page contains additional revision exercises for week 05.
  • These exercises are not compulsory, nor do they provide any marks in the course.
  • You cannot submit any of these exercises, however autotests are available for them (Command included at bottom of each exercise).

Exercise
(●●◌)
:

Draw a triangle

Write a program called hollow_triangle.c that reads an integer n from standard input. and prints a pattern of asterisks forming a hollow triangle.

You can assume n is greater than 3.

Make your program match the examples below exactly.

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

dcc hollow_triangle.c -o hollow_triangle
./hollow_triangle 
Enter size: 4
*
**
* *
****
./hollow_triangle 
Enter size: 5
*
**
* *
*  *
*****
./hollow_triangle
Enter size: 8
*
**
* *
*  *
*   *
*    *
*     *
********
./hollow_triangle
Enter size: 11
*
**
* *
*  *
*   *
*    *
*     *
*      *
*       *
*        *
***********

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

1511 autotest hollow_triangle

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: As the course has not taught arrays yet, you are not permitted to use an array in this exercise.

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

Assumptions/Restrictions/Hints

  • You may assume that you will be given only integers as input.
  • You may assume that all input is valid.

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

1511 autotest x

Exercise
(●●◌)
:

Perfect Square Numbers

There is no starter code provided for this exercise. Instead, you have to write 2 functions from scratch.

You must implement the following two functions:

  1. is_perfect_square:

    Which takes in an int parameter, and returns 1 if that integer is a perfect square number, or 0 otherwise.

  2. count_perfect_squares

    Which takes in 2 parameters:

    • The first parameter is an integer, which contains the length of the array.
    • The second parameter is an array of integers.

    The order of these parameters is important.

    Your first parameter must be an int, and your second parameter must be an array.

    This function should return the total number of perfect squares contained in the array.

For more information about perfect_squares see this link.

You may name the function parameters what ever you like.

You may create a main function to test your functions, but this will not be assessed. Your functions will be called directly in automarking.

Assumptions/Restrictions/Clarifications

  • Your program is not required to do any error checking
  • Your functions may use any parameter names you chose, but the name of the functions and order of parameters must match what is described above.
  • You must not modify the array within the count_perfect_squares function.
  • The array will be fully initialised.
  • You can assume length will be at least 0.

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

1511 autotest perfect_square

Exercise
(●●◌)
:

Swap the case of letters in a string.

Download swap_case.c here, or copy it to your CSE account using the following command:

cp -n /web/cs1511/23T1/activities/swap_case/swap_case.c .

Edit the C program swap_case.c (linked above) which reads characters from its input and writes the same characters to its output with lower case letters converted to upper case and upper case letters converted to lower case.

Your program should stop only at the end of input.

To help you format your code nicely, your program must use a function with signature:

int swap_case(int character);
which:
  • returns the character in lower case if it is an upper case letter
  • returns the character in upper case if it is a lower case letter
  • returns the character unchanged otherwise

Note: Your program will not pass autotests if it does not contain this function.

For example:

dcc swap_case.c -o swap_case
./swap_case
Are you saying 'Boo' or 'Boo-Urns'?
aRE YOU SAYING 'bOO' OR 'bOO-uRNS'?
In this house, we obey the laws of thermodynamics!
iN THIS HOUSE, WE OBEY THE LAWS OF THERMODYNAMICS!
UPPER !@#$% lower
upper !@#$% LOWER

Hint: hint use getchar to read characters (don't use scanf or fgets).

Hint: you need only a single int variable. Don't use an array.

Hint: use putchar to output each character.

Hint: make sure you understand this example program which reads characters until end of input.

Hint: make sure you understand this example program which reads characters, printing them with lower case letters converted to upper case.

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

1511 autotest swap_case

Exercise
(●●◌)
:

Calculate the dot product of two arrays.

Download dot_product.c here, or copy it to your CSE account using the following command:

cp -n /web/cs1511/23T1/activities/dot_product/dot_product.c .

Your task is to add code to this function in dot_product.c:

// Calculates the dot product of two arrays of the same length.
//
// Parameters:
//  `length`: the length of both arrays
//  `vector1`: The first array
//  `vector2`: The second array
//
// Returns: the dot product of `vector1` and `vector2`.
int dot_product(int length, int vector1[], int vector2[]) {
    // TODO: Complete this function
}

This function takes in two arrays and returns their dot product.

To calculate the dot product of two arrays (v1 and v2) of length n we use the formula

(v1 . v2) = v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2] + ... + v1[n - 1] * v2[n - 1]

For more information about the dot product see this link.

Once your program is working, the output from the provided main function should be:

dcc -o dot_product dot_product.c 
./dot_product 
Result: 106
Result2: -8

Assumptions/Restrictions/Clarifications

  • Your program is not required to do any error checking
  • You must not modify the arrays within the dot_product function.
  • Both arrays will be fully initialised.
  • You can assume length will be greater than 0.

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

1511 autotest dot_product

Exercise
(●●◌)
:

Find largest value in a 2d array.

Download array_2d_max.c here, or copy it to your CSE account using the following command:

cp -n /web/cs1511/23T1/activities/array_2d_max/array_2d_max.c .

Your task is to add code to this function in array_2d_max.c:

// Return the largest number in a 2D square array
// with sides of length side_length.
int array_max(int array[TEST_ARRAY_SIZE][TEST_ARRAY_SIZE], int side_length) {
    // PUT YOUR CODE HERE (you must change the next line!)
    return 42;
}
You are to implement the array_2d_max function which should return the largest value in the array.

The file array_2d_max.c contains a main function which tests it against an array you have been given.

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

1511 autotest array_2d_max

Exercise
(●●◌)
:

Finding Treasure

Download find_treasure.c here, or copy it to your CSE account using the following command:

cp -n /web/cs1511/23T1/activities/find_treasure/find_treasure.c .

Your task is to add code to this function in find_treasure.c:

// Finds the location of the 'X' in the 2d array.
//
// Parameters:
//  `map`: a 2d array of chars which contains at most one 'X'.
//  `row_pointer`: A pointer to an integer. 
//      If the map contains an 'X', this pointer should be used to set the 
//      integer to the row number of the 'X'.
//  `col_pointer`: A pointer to an integer. 
//      If the map contains an 'X', this pointer should be used to set the 
//      integer to the column number of the 'X'.
//
// Returns: 1 if the treasure was found, 0 otherwise
int find_treasure(char map[SIZE][SIZE], int *row_pointer, int *col_pointer) {
    // TODO: Complete this function
}

This function should try to find the location of the treasure ('X') in the 2D array map.

If map contains the 'X', then the function should use the row_pointer and col_pointer to write the row and column numbers of the treasure into the integers that they point to.

The function should return a 1 if map contained the treasure, or 0 otherwise.

For reference, the location row == 0, col == 0 is the top left corner of the map.

Once your program is working, the output from the provided main function should be:

dcc -o find_treasure find_treasure.c 
./find_treasure 
Treasure was found at coordinates (5, 2)!

Assumptions/Restrictions/Clarifications

  • Your program is not required to do any error checking
  • You must not modify the array within the find_treasure function.
  • You can assume that the map will be fully initialised, and will contain at most 1 'X'

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

1511 autotest find_treasure