CS211 - Algorithms and Data Structures II
Lab Exams and Lab Assignments

T Naughton, CS NUIM
Back to CS211 home


Basic C++ programming:

[Lab exam 1] [Sample exam solutions 1] [Lab sheet 1] [Sample lab solutions 1.4][1.5] (week of 24 Jan 2000)
[Tutorial 1] [Sample tutorial solutions 1]
[Lab exam 2] [Sample exam solutions 2] [Lab sheet 2] [Sample lab solutions 2] (week of 31 Jan 2000)
[Tutorial 2] [Sample tutorial solutions 2]

Pointers and memory allocation (more revision):

[Lab exam 3] [Sample exam solutions 3] [Lab sheet 3] [Sample lab solutions 3] (week of 07 Feb 2000)
No exam this week [Lab sheet 4] [Sample lab solutions 4] [Class-based version] (week of 14 Feb 2000)
No exam this week [Lab sheet 5] [Sample lab solutions 5.1][5.2] (week of 21 Feb 2000)
No exam this week [Lab sheet 6] [Sample lab solutions 6.1][6.2] (week of 28 Feb 2000)

Binary trees (beginning of advanced topics):

[Lab exam 7] [Sample exam solutions 7] [Lab sheet 7] [Sample lab solutions 7] (week of 13 Mar 2000)
No exam this week [Lab sheet 8] [Sample lab solutions 8] (week of 20 Mar 2000)

Hash tables and computational complexity:

No exam this week [Lab sheet 9] [Sample lab solutions 9] (week of 27 Mar 2000)
No exam this week [Lab sheet 10] [Sample lab solutions 10] (week of 03 Apr 2000)

[Lab exam 11] [Sample exam solutions 11.1] [11.2] [Lab sheet 11] (week of 24 Apr 2000)

Sample solutions will be posted as soon as submission deadline has passed.


Lab Exam 1

This lab exam is designed to test your basic C++ programming skills.

Problem 1A.1 Write a program that takes a number n as input from the user and writes out n rows of text on the screen. Each row contains (nth-1) blanks followed by nth 'a' characters.

Sample output of the program is shown below, when the user enters the number 4.

Here we go:
a
 aa
  aaa
   aaaa
Goodbye!

Your program should have the following structure.

#include<iostream>

void main(void){
  int n;
  cout << "Please enter a nonnegative integer:" << endl;
  cin >> n;

  cout << endl << "Here we go:" << endl;
  /*
  ** Your code here
  */
  cout << "Goodbye!" << endl;
}

Problem 1B.1 Write a program that takes a number n as input from the user and writes out n rows of asterisks on the screen as shown below. This sample output of the program occurs when the user enters the number 4.

Here we go:
****
 ***
  **
   *
Goodbye!

Your program should have the following structure.

#include<iostream>

void main(void){
  int n;
  cout << "Please enter a nonnegative integer:" << endl;
  cin >> n;

  cout << endl << "Here we go:" << endl;
  /*
  ** Your code here
  */
  cout << "Goodbye!" << endl;
}

Problem 1.2 Provide a snippet of code (between 3 and 5 lines) that illustrates the operation of the '*' and '&' symbols when manipulating pointers. Demonstrate your understanding by explaining each line of code.

Problem 1.3 Write a program that reads 10 integers from the user and stores them in an array of type int. Using a second array of type double, also of length 10, the program stores each of the first array's integers divided by 2. Finally, the program outputs each integer-double pair, one per line.


Lab Sheet 1

This lab sheet is designed to test your basic C++ programming skills.

Problem 1.4 Write a program that asks the user for a letter from the alphabet and then prints out the next character in the alphabet. Take account of both upper and lower case characters, and if the user enters 'z' or 'Z' then return 'a' or 'A', respectively. The program will loop continuously, until the user indicates the program should terminate. A sample run of the program follows.

Hi! I'm a clever computer program that knows the alphabet.
Please enter a letter:
c
The next letter is d.
Do you want to enter another letter (y = yes)?
y
Please enter a letter:
D
The next letter is E.
Do you want to enter another letter (y = yes)?
y
Please enter a letter:
z
The next letter is a.
Do you want to enter another letter (y = yes)?
n
Goodbye!

Problem 1.5 Similar to problem 1.3. However, this time, allow the user to enter any number of integers, specified at run-time (you will have to use new and delete). Furthermore, sort both arrays in ascending order before writing them out. Use any sorting algorithm you like (except the ones built into C++).

Problem 1.6 Design a strategy for a computer program to play the well-known childrens' game of Hangman. C++ code is not required, just sequences of steps written in English.