DPST1091 Revision Arrays Exercises

Revision Exercise: Filter a 1D array(●◌◌)

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

cp -n /import/reed/A/dp1091/public_html/24T3/activities/filter_list/filter_list.c .

Your task is to write a program to find the number of bags from people over a specified height.

More specifically, your program should do the following.

  1. Scan in 5 pairs of height and number of bags, and store these pairs in an array of structs
  2. Ask the user for a minimum height to filter by
  3. Find the number of bags, from people who were greater than or equal to that height

This program has some starter code which includes the following struct.

struct passenger {
    double height;
    int num_bags;
};

The starter code also creates an array for you to store data in.

struct passenger my_array[SIZE];

Examples

dcc filter_list.c -o filter_list
./filter_list
Enter height & number of bags: 150.0 1
Enter height & number of bags: 160.0 2
Enter height & number of bags: 170.0 3
Enter height & number of bags: 180.0 1
Enter height & number of bags: 190.0 2
Select height: 170.0
Total of 6 bags from people over 170.000000
./filter_list
Enter height & number of bags: 150.0 1
Enter height & number of bags: 160.0 1
Enter height & number of bags: 170.0 1
Enter height & number of bags: 180.0 1
Enter height & number of bags: 190.0 1
Select height: 200.0
Total of 0 bags from people over 200.000000

Assumptions/Restrictions/Clarifications

  • Your program should match the output shown above exactly.
  • You can assume you will always be given the correct data type during input.
  • You can assume a height is always a positive and non-zero number.
  • You can assume the number of bags is non-negative.
  • Your program should still work when a person has no baggage.

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

1091 autotest filter_list

Revision Exercise: Array Functions (●●●)

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

cp -n /import/reed/A/dp1091/public_html/24T3/activities/array_functions/array_functions.c .

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

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 returns the maximum value in the array,
  • array_min returns the minimum value in the array,
  • array_average returns the average value in the array, and
  • array_sum 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:

1091 autotest array_functions

Revision Exercise: Split sum (●●◌)

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

cp -n /import/reed/A/dp1091/public_html/24T3/activities/split_sum/split_sum.c .

In this exercise you will be asked to sum two parts of an array and store the results at two given addresses.

You will implement the split_sum function, which takes five arguments: array, length, index, first_sum and second_sum. You should sum the array's values from the start of the array up to (but not including) the index given and put the result into first_sum, and then sum the array's values from the index till the end of the array and put the result into second_sum.

Examples

dcc split_sum.c -o split_sum
./split_sum
Enter length: 3
Enter array values: 11 2 3
Enter index: 1
First sum: 11
Second sum: 5
./split_sum
Enter length: 6
Enter array values: 1 2 3 4 5 6
Enter index: 4
First sum: 10
Second sum: 11
./split_sum
Enter length: 7
Enter array values: 12 45 24 57 10 8 12
Enter index: 2
First sum: 57
Second sum: 111

Assumptions/Restrictions/Clarifications

  • You may assume the index given is between 0 and the length of the array inclusive.
  • You may assume you always receive valid inputs.
  • Hint: The sum of an empty array is 0.

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

1091 autotest split_sum

Revision Exercise: Reverse an array (●◌◌)

Write a C program, reverse_array.c, which reads integers line by line, and when it reaches the end of input, prints those integers in reverse order, line by line.

You will never be given more than 100 integers to print out.

Examples

dcc reverse_array.c -o reverse_array
./reverse_array
Enter numbers forwards:
10
50
20
40 

Reversed:
40
20
50
10
./reverse_array
Enter numbers forwards:
-5
-4
-3
-2
-1 

Reversed:
-1
-2
-3
-4
-5

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

1091 autotest reverse_array

Revision 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 /import/reed/A/dp1091/public_html/24T3/activities/dot_product/dot_product.c .

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:

1091 autotest dot_product

Revision 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 /import/reed/A/dp1091/public_html/24T3/activities/common_elements/common_elements.c .

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:

1091 autotest common_elements

Revision 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 /import/reed/A/dp1091/public_html/24T3/activities/count_bigger/count_bigger.c .

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:

1091 autotest count_bigger

Revision 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 /import/reed/A/dp1091/public_html/24T3/activities/array_2d_max/array_2d_max.c .

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:

1091 autotest array_2d_max

Revision Exercise: Finding Treasure (●●◌)

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

cp -n /import/reed/A/dp1091/public_html/24T3/activities/find_treasure/find_treasure.c .

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 find_treasure.c -o find_treasure
./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:

1091 autotest find_treasure

Revision Exercise: Find totals of rows

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

cp -n /import/reed/A/dp1091/public_html/24T3/activities/find_totals/find_totals.c .

Given a 2d arrays of integers, your task is to find the number of rows where the sum of the integers equates to exactly 10.

You can assume the given array is always of size 5

You can assume the array always has the same number of rows and columns (The array is always square)

Only this specific function will be called in marking, the main function is only provided for your testing, however you can create more functions it is helpful.

For example, if the following 2D array was given

The output should be exactly

dcc find_totals.c -o find_totals
./find_totals
2 rows had a sum of 10

This output is becasue rows 2 and 3 each have a sum of exactly 10.

Your function should work when there are arrays with no rows equal to 10.

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

1091 autotest find_totals