Sei sulla pagina 1di 5

Ollscoil na hÉireann, Gaillimh

National University of Ireland, Galway

Semester 2 Examinations, 2016/2017

Exam Code(s) 1BE, 1EG, 1BG, 1BM, 1ESE, 1BLE, 1BP

Exam(s) 1st Civil Engineering,


1st Undenominated Engineering,
1st Biomedical Engineering,
1st Mechanical Engineering,
1st Energy Systems Engineering,
1st Electrical and Electronic Engineering,
1st Electronic and Computer Engineering

Module Code(s) CT1111

Module(s) Engineering Computing 11

Repeat Paper No

External Examiner(s) Dr. John Power


Internal Examiner(s)
Dr M. Schukat
*Dr E. Howley
*Dr F. S. Smith
*Dr. Liam Kilmartin
*Dr. Richard Harte

Module Coordinator Dr E. Howley

Instructions: You must answer 3 questions in total.

In Section A: Answer both Q1 AND Q2


In Section B: Answer Q3 OR Q4

Use a separate answer book for each QUESTION.


Duration 2 hours

No. of Pages 5
Department(s) Information Technology

Page 1 of 5
C Programming

Answer Q1 and Q2 – Choices are offered within each question.


Q1: Answer any 2 of the 3 parts to this question. All parts carry equal marks.

A.
i. What output would the following code produce?

#include <stdio.h>
void main()
{
int x = 23;
int y = 6;
int num;
float res, res2;
res = x/y;
num = x%y;
res2 = x/4.0;

printf(“res is %.1f, num is %d and res2 is %.1f”, res2, num, res);

}
ii. Write a function in C that takes one argument, an array of 40
elements. Your function should print out average of the elements
in the array. It should also print out the values in the array that are
less than this average.
[10 Marks]

B. Write a function in C called factors(). It should take a single integer as its


argument and print out all of the numbers that divide into the argument
exactly.
[10 Marks]

C. Write a program in C that reads in 300 integers that represent the exam
results of students. Once the 300 numbers have been read in your code
should do the following:

Print out the highest mark awarded.


Change all marks of 39 to 40.
Change all marks of 49 to 50.
Change all marks of 69 to 70.
Print out the revised marks.
[10 Marks]

PTO

Page 2 of 5
2. Answer any 2 of the 3 parts to this question. All parts carry equal marks.

A.
Figure 1

Figure 1 shown above represents a back yard with a rectangular boundary


wall. The back yard also contains a triangular pond shown as the shaded
area. You are now asked to write a C program which will include the
following:

1. A function to request a user for the dimensions of the yard and the
pond.
2. A function to determine the area of the yard without including the
pond.

**Hint: Area of a triangle = 0.5 * Base * Height

[10 Marks]

B. Write a function in C called getOdds(). This function should take an array


shown below as an argument and determine which values in the array are
odd values. The odd values should be printed to screed while the even
values should be ignored.

5 3 4 2 7 99 100 101

[10 Marks]

A. Write a function in C that reads 20 numbers from the console and stores
these values in an array. The function should return the average of those
number to main where they can be displayed. Show the full program and
how the function would be called from main.
[10 Marks]

PTO

Page 3 of 5
Arduino Section

Answer Question 3 OR 4
Q3
a. Write a program for Arduino that controls a set of two traffic lights at a
crossroads. Assume that when the east-to-west traffic lights are green or amber,
the north-to-south traffic lights are red. Green lights must stay on for 2 minutes
and the amber lights for 6 seconds (remembering that in all other cases the light
must be set to red). You can assume that the Arduino is wired to the traffic lights
in the following pin configuration.

East-west traffic light


 Green light -> Pin 0; Amber light -> Pin 1; Red light -> Pin 2

North-south traffic light


 Green light -> Pin 3; Amber light -> Pin 4; Red light -> Pin 5
[10 Marks]

b. Write a program for Arduino that reads the analog input from a temperature
sensor and uses this value to control a light emitting diode (LED). The purpose of
this system is to warn the user of an increasing temperature by varying the output
of the LED as the temperature passes two separate thresholds. The system is
outlined as follows:
 Temperature sensor attached to analog input 0 of the Arduino; LED
attached from digital pin 9 of the Arduino to ground
 For the sensor, as temperature increases the analogue readings increase
(range 0 to 1023). The max temperature which can be read is 80 degrees;
the min temperature which can be read is 0. Use a function to re-map the
range to represent the real world values.
 The first threshold should be at roughly 30 degrees, the next should be at
roughly 50 degrees. If the first threshold is reached, the LED should turn
ON, if the second threshold is reached, the LED should start BLINKING.
Other than that, the LED should be OFF.

[7 marks]
c. Outline two positives and two negatives of the kind of ‘hard’ thresholding
outlined in part b.

[3 marks]

PTO

Page 4 of 5
Q4
a. To smooth the accelerometer output from a games controller such as the Nintendo
Wii, an analogue smoothing filter is often required. You have a plan to build a
similar game controller and have written all the necessary code except for the
smoothing function. Write the required function which takes in the accelerometer
data as an input and outputs a smoothed version of the signal.

Your function should use a buffer which takes in 4 samples and returns an
average (such as a mean or median) without introducing a significant lag (i.e. a
moving average).

The function should take the form:


int SmoothSensor(int accelerometerData)
[10 marks]

b. A planetary exploration robot has a function built in which allows it to follow


light sources for the purpose of using the light from the sun for navigation. Two
highly sensitive LDRs are present on the front of the robot. You have been tasked
with writing a simple test algorithm using Arduino/Pololu functions which checks
if the LDRs and motors are working.

Your algorithm must:


 Use the Analogue Pins 0 and 1 as your inputs for LDR_left and
LDR_right. Assume that as light shines on the LDR, its analogue values
goes down.
 Use the first 10 seconds to take in the max and min values from the
environment (use millis)
 Map the values to more manageable and intuitive ranges
 Take the difference between the two LDRs and use this value to move the
robot towards the light (motor functions)
 Move the robot forward when the difference resides within a certain range

[10 marks]

END

Page 5 of 5

Potrebbero piacerti anche