DPST1091 Revision Arrays Exercises

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

Download filter_list.c here

Or, copy these file(s) to your CSE account using the following command:

1091 fetch-activity filter_list

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 these file(s) to your CSE account using the following command:

1091 fetch-activity array_functions

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 these file(s) to your CSE account using the following command:

1091 fetch-activity split_sum

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

/**
 * The array is split into two parts by the index passed in so 
 * the first part has indexes until the index non-inclusive and 
 * the second part has indexes including the index till the end of the array.
 * This function finds the sum of the first part and puts it into the first sum,
 * and finds the sum of the second part and puts it into the second sum.
 *
 * Takes in 
 * the array "array", 
 * the array's length "length", 
 * the index that splits the array into two parts "index", 
 * a pointer to where the value of the first part of the array's sum should be 
 * placed "first_sum" and 
 * a pointer to where the value of the second part of the array's sum should be 
 * placed "second_sum".
 *
 * Returns nothing.
 */
void split_sum(int array[], int length, int index, int *first_sum, int *second_sum) {

    // TODO complete this function

    return;
}

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 these file(s) to your CSE account using the following command:

1091 fetch-activity dot_product

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:

1091 autotest dot_product

Revision Exercise: Return Common Elements Of Two Arrays (●●●)

Download common_elements.c here

Or, copy these file(s) to your CSE account using the following command:

1091 fetch-activity common_elements

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:

1091 autotest common_elements

Revision Exercise: Count the big numbers in the array (●●◌)

Download count_bigger.c here

Or, copy these file(s) to your CSE account using the following command:

1091 fetch-activity count_bigger

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:

1091 autotest count_bigger