Example midterm format

QCM

For each QCM question, choose the best answer. Each question answered correctly is scored 1 point, and each question answered incorrectly is scored -0.5 point.

Question 1

Given the structure Coord defined below, we want to use a function to return a structure with altitude in kilometers.

#include <stdio.h>

struct Coord {
  double lon;
  double lat;
  double alt;
};

struct Coord convert_to_km(struct Coord coord_m) {
  struct Coord coord_km;
  coord_km.lon = coord_m.lon;
  coord_km.lat = coord_m.lat;
  coord_km.alt = coord_m.alt / 1e3;
  return &coord_km;
}

int main() {
  struct Coord coord_m = {7.65833, 45.97639, 4478.};
  struct Coord coord_km = convert_to_km(coord_m);
  printf("The algitude is %f km\n", coord_km->alt); // should be "4.478 km"
  return 0;
}

Which of the following modifications are necessary to make the code work?

  1. The structure coord_m should be declared as a pointer within the function main().
  2. The structure coord_km should be declared as a pointer within the function convert_to_km().
  3. The structure coord_km and not its address should be returned from the function convert_to_km().
  4. The argument to convert_to_km() function should be declared as a pointer type.
  5. The return type of function convert_to_km() should be declared as a pointer.
  6. Both 2 and 5.

Question 2

View the following operation in MATLAB. We would like the result of x + y to be 500000.5.

x = 450000.5;
y = int16(100000) / 2;

Which of the following statements are true?

  1. The output of x + y is an integer
  2. The output of x + y is an a floating point number
  3. y is automatically converted to double before the addition.
  4. Both 2 and 3.
  5. None of the above.

Coding on paper example

A map of altitudes with 3601 x 2501 grid points is stored in a Python list (1-D). The first row of the map is stored in the first 3601 elements of this list; the second row of the map is stored in the next 3601 elements, and so on. You can refer to this list as altitudes. Write a function which returns the maximum altitude and its grid coordinates.

The solution should look something like this - small syntactic errors are permitted:

def calculate_maximum(altitudes):
    ny = 3601
    nx = 2501
    maxvalue = 0
    maxX = 0
    maxY = 0
    for y in range(ny):
        for x in range(nx):
            i = y * ny + x
            if altitudes[i] > maxvalue:
                maxvalue = altitudes[i]
                maxX = x
                maxY = y
    return maxvalue, maxX, maxY

Coding on paper example 2

A given file "LAU_diurnal_PM25.csv" contains daily (24-hour-averaged) airborne particulate matter (PM2.5) mass concentrations in μg/m3 measured in Lausanne for the year 2021. The World Health Organization guidelines recommends a limit value of 15 μg/m3 for 24-hour-averaged concentrations of this pollutant.

The first six lines of the file are shown below (out of 365 lines total):

01.01.2021;4.9
02.01.2021;10.5
03.01.2021;11.7
04.01.2021;15.6
05.01.2021;16.3
06.01.2021;12.9

A MATLAB function that reads this file is as follows. It returns the day, month, and year as integer types and the concentration as double precision.

function [day, month, year, conc] = readfile(filename)

fid = fopen(filename);
data = textscan(fid, '%s %f', 'delimiter', ';');
fclose(fid);

datestr = cell2mat(data{1,1});

day = int16(str2num(datestr(:,1:2)));
month = int16(str2num(datestr(:,4:5)));
year = int16(str2num(datestr(:,7:10)));
conc = data{1,2};

end

Write a MATLAB program to read in the file and calculate the number of exceedances for each month using vectorized operations. Print the output to the screen in the following format.

month = 1, count = 5
month = 2, count = 12
month = 3, count = 8
month = 4, count = 0
month = 5, count = 0
month = 6, count = 0
month = 7, count = 2
month = 8, count = 1
month = 9, count = 0
month = 10, count = 3
month = 11, count = 8
month = 12, count = 8

Solution:

[day, month, year, conc] = readfile('LAU_diurnal_PM25.csv');

LIMIT = 15;

for m=1:12
    n = length(conc(month == m & conc > LIMIT));
    fprintf('month = %d, count = %d\n', m, n);
end