NUIMCrest SE307 - Computation and Complexity Theory
Department of Computer Science
National University of Ireland, Maynooth

T Naughton, NUIM
Back to SE307 home


Lab 1

This lab is designed to refresh your basic programming skills.

Pseudocode algorithms (with pencil and paper) are required. You may use any programming language of your choice (C/C++/Java). However, you are restricted to using while loops for iteration. Ask a demonstrator to check an algorithm as soon as you are finished.


Problem 1.1 Write a function that takes an array A of positive ints, and an arbitrary positive integer X. Array A has an unknown length. You are told, however, that the last element of A will contain the value -1. The function should return 1 if X is in A, and -1 otherwise. (Do not use the array.length functionality provided with Java.)

int searchArray(int A[], int X) {
  /* This function returns 1 if X is in A and -1 otherwise.
  */

  // Your code here
}

Problem 1.2 Write a function that takes an array A of ints and returns the index of the largest integer in A. If two or more indexes have this property, return the largest index. If A is empty, return -1. (The end of array A will be marked by a -1.)

int findHighest(int A[]) {
  /* This function returns the index of the largest int in A.
  ** If there are multiple occurances of this int we return the
  ** last occurance. If the list has no elements, return -1.
  */

  // Your code here
}

Problem 1.3 Write a function that takes a positive integer X and returns the smallest prime number greater than X.

int findNextPrime(int X) {
  /* This function returns the smallest prime number greater than X.
  */

  // Your code here
}

Problem 1.4 Write a function that writes out a list of all positive rational numbers. You do not have to write out their decimal expansions, just all possible strings of the form "X/Y" where X and Y are positive integers. Your program is correct if any given rational will eventually be printed to the screen.

void enumerateRationals() {
  /* This function enumerates the rationals.
  */

  // Your code here
}



End of sheet