NUIMCrest CS210 - Algorithms and Data Structures I
Department of Computer Science
National University of Ireland, Maynooth

T Naughton, NUIM
Back to CS210 home


Laboratory Sheet 1

Iteration and arrays. This lab exam will allow you to test your basic Java programming skills. It is a 40-minute closed-book exam (this means you may not use books, notes, or a compiler).

Use your laboratory notebook for all writing. Use a page title of "Lab Exam 1" and today's date, and number your answer to each problem clearly.

Use the remainder of the lab (and the remainder of the week) to review your Java notes and write corrected versions of these programs into your laboratory notebook. Entitle these pages "Lab Exam 1 - Corrections". Before the next lab you should also grade today's attempt at this exam. The marking scheme for each problem is as follows: 0.1 marks for an attempt; 0.6 marks for a program with one small error; 1.0 for a perfect pseudocode program.


Problem 1.1 Write a function that takes an array of ints and returns the index of the largest integer. If two or more indexes have this property, return the largest index. If the list is empty, return -1.

int findHighest(int list[]) {
  /* This function returns the index of the largest int in list.
  ** 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.2 Write a function that takes an array of ints and prints out the indexes of the first two duplicate integer values found in the list. If no duplicates are found then print an appropriate message.

void findDuplicates(int list[]) {
  /* This function prints out the indexes of the first two duplicates
  ** found in list. The user is notified if no duplicates are found.
  */

  // Your code here
}

Problem 1.3 Write a function that takes an array of ints and prints out the index of the element with a value closest to 10. If multiple integers are equally close to 10 then print out the first such index found.

void findClosestTo10(int list[]) {
  /* This function prints out the index of the element in list
  ** that has a value closest to 10. If multiple such values exist,
  ** the first one found is printed out.
  */

  // your code here
}



End of sheet