CS211 - Algorithms and Data Structures II
Tutorial 3

T Naughton, CS NUIM
Back to CS211 home


This tutorial reviews some fundamental programming concepts for those students who are, for whatever reason, still struggling with the basics.
Problems 1A.1, 1B.1, 2A.1, 2B.1, 2C.1, T3.1, T3.2

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!

Problem 2A.1 Write a program that takes a number n as input from the user and writes out 2n 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!

Problem 2B.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!

Problem 2C.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!

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

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

This sample output of the program occurs when the user enters the number 3.

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

Problem T3.2 Write a program that takes a number n as input from the user and writes out n2 rows of characters on the screen as shown below. This sample output of the program occurs when the user enters the number 2.

Here we go:
X**X
*XX*
*XX*
X**X
Goodbye!

This sample output of the program occurs when the user enters the number 3.

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