Programming Fundamentals

Information

  • This page contains additional revision exercises for week 04.
  • 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
(●◌◌)
:

Print the Middle Integer of 3 Integers

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

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

Write a C program middle3.c that reads 3 integers into a struct and prints the middle integer.

You are not permitted to use loops or arrays (use if statements).

Your program should behave exactly like this example:

./middle3
Enter integer: 23
Enter integer: 5
Enter integer: 27
Middle: 23
./middle3
Enter integer: 3
Enter integer: 6
Enter integer: 27
Middle: 6
./middle3
Enter integer: 9
Enter integer: 7
Enter integer: 8
Middle: 8
You can assume the user supplies 3 integers. 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 middle3

Exercise
(●◌◌)
:

Loop Sum

Write a program called loop_sum.c that reads an integer n from standard input, and then scans in n integers from standard input, adds them together, then prints the sum.

You can assume that n is non-negative (≥ 0).

You can assume that you are given exactly n+1 integers.

You can assume that scanf succeeds (you do not need to check for errors).

Make your program match the examples below exactly.

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

dcc loop_sum.c -o loop_sum
./loop_sum 
How many numbers: 2
1
2
The sum is: 3
How many numbers: 3
-3
4
13
The sum is: 14
How many numbers: 5
-2
-1
0
1
2
The sum is: 0

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

1511 autotest loop_sum

Exercise
(●●◌)
:

Print an Hourglass

Write a program called hourglass.c that prompts the user to enter an integer n and prints an nxn pattern containing an hourglass. This hourglass will be made up of *'s', all surrounded by hyphens "-".

Please enter a size: 9
*********
-*******-
--*****--
---***---
----*----
---***---
--*****--
-*******-
*********

You can assume n is >= 3.

Make your program match the examples below exactly.

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

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

./hourglass
Please enter a size: 3
***
-*-
***

./hourglass
Please enter a size: 6
******
-****-
--**--
--**--
-****-
******

./hourglass
Please enter a size: 7
*******
-*****-
--***--
---*---
--***--
-*****-
*******

Please enter a size: 10
**********
-********-
--******--
---****---
----**----
----**----
---****---
--******--
-********-
**********

./hourglass
Please enter a size: 15
***************
-*************-
--***********--
---*********---
----*******----
-----*****-----
------***------
-------*-------
------***------
-----*****-----
----*******----
---*********---
--***********--
-*************-
***************

 ./hourglass
Please enter a size: 25
*************************
-***********************-
--*********************--
---*******************---
----*****************----
-----***************-----
------*************------
-------***********-------
--------*********--------
---------*******---------
----------*****----------
-----------***-----------
------------*------------
-----------***-----------
----------*****----------
---------*******---------
--------*********--------
-------***********-------
------*************------
-----***************-----
----*****************----
---*******************---
--*********************--
-***********************-
*************************
For this exercise, try and break the problem down into small parts and solve them individually.

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

1511 autotest hourglass

Exercise
(●●◌)
:

Count the big numbers in the array

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

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

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

// return the number of "bigger" values in an array (i.e. larger than 99
// or smaller than -99).
int count_bigger(int length, int array[]) {
    // PUT YOUR CODE HERE (you must change the next line!)
    return 42;
}
count_bigger should return a single integer: the number of values in the array which are larger than 99 or smaller than -99.

For example if the array contains these 8 elements:

141, 5, 92, 6, 535, -89, -752, -3

Your function should return 3, because these 3 elements are bigger than 99 or smaller than -99:

141, 535, -752

Assumptions/Restrictions/Clarifications.

count_bigger should return a single integer.

count_bigger should not change the array it is given.

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

You can assume the array contains at least one integer.

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

Exercise
(●●●)
:

Array Functions

In this exercise, you'll be creating a small collection of functions that could help you with working on arrays.

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

cp -n /web/cs1511/23T1/activities/array_functions/array_functions.c .
Your task is to implement the missing functions in array_functions.c

and implement the functions.

Some of these functions read through an array and return some information about the array but do not change the array in any way.

These functions are:

  • array_max; which returns the maximum value in the array,
  • array_min; which returns the minimum value in the array,
  • array_average; which returns the average value in the array, and
  • array_sum; which returns the sum of all values in the array.

Note: array_average should return the greatest int less than or equal to the statistical mean of the array.

Some other functions change the array values or have an effect outside the program. We say that these functions have side effects as they do something other than just return a value.

These functions are:

  • array_add; which adds a value to every element in the array,
  • array_scale; which multiplies every value in array by the same value, and
  • scan_array; which reads in values from input and stores them in the array
  • show_array; which shows an array on the screen in the style [1, 2, 3, 4, 5, 6]
  • copy_array; which copies one array into another
The file array_functions.c contains a main function with some unit tests to help you test your functions.

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

1511 autotest array_functions

Exercise
(●●●)
:

Return Common Elements Of Two Arrays

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

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

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

int common_elements(int length, int source1[length], int source2[length], int destination[length]) {
    // PUT YOUR CODE HERE (you must change the next line!)
    return 42;
}
common_elements should copy the values in the first source array which are also found in the second source array, into the third array.

In other words, all of the elements that appear in both of the source1 and source2 should be copied to the third destination array, in the order that they appear in the first array. common_elements should return a single integer: the number of elements copied to the destination array.

For example, if the source arrays contained the following 6 elements:

source1: 1, 4, 1, 5, 9, 2

source2: 1, 1, 8, 2, 5, 3

common_elements should copy these 4 values to the destination array: 1, 1, 5, 2

The value 4 and 9 do not get copied because they do not occur in the second array.

common_elements should return the integer 4, because there were 4 values copied.

common_elements should copy the elements in the order that they appear in the first array.

If a value in the first array occurs one or more times in the second array, each occurrence of those values in the first array should be copied.

It doesn't matter how many times the values occur in the second array, as long as the values occur at least once. For example. if the two arrays contained the following 5 elements:

source1: 1, 2, 3, 2, 1

source2: 1, 2, 3, 4, 5

Your function should copy all five values (1, 2, 3, 2, 1) from source1 to the destination array {} as all of the values (1, 2, 3) appeared at least once in source2 and it should return 5 because because 5 values were copied.

Assumptions/Restrictions/Clarifications.

You can assume the two source arrays contain only positive integers.

You can assume that all three arrays are the same size (length) and length is > 0.

You cannot assume anything about the number of common elements, i.e. there may not be any common elements between both arrays, or conversely, the entire contents of the first array may also be present in the second array.

common_elements should return a single integer.

common_elements should not change the array it is given.

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

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