lab_week9

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Data Structure & Algorithms 1 (DSA1)

Lab (week 9) - Static Data Structure: 1D-Array in C++


2024 - 2025

24 November 2024

Objectives
Learn to manipulate Arrays :
• Declaration and initialization
• Insertion, deletion, and updating elements
• Searching elements

Part 1: Manipulation of Arrays in C++


Exercise 1
Define a procedure void scanArray(int A[], int n) that takes as arguments an array and its size, and
initializes it with inputs introduced by the user. Define also another procedure printArray(int A[], int
n) to print all the elements in the array with their respective position.

Exercise 2
Define a function int minArray(int A[], int n) which returns the position of the minimum value in the
array A. Write a main program to test your function.

Exercise 3
Define a function bool F(int A[], int n). F returns a Boolean indicating whether there is a value between
0 and 10 in the array. Write a program to test your function.

Exercise 4
Modify the function int scanArray(int A[], int n) of exercise 1 to initialize the array with values be-
tween 0 and 20 only. Define a procedure void printOccurrence(int A[], int n) which computes and
displays the number of occurrences of 0, 1, . . . , 20 in the array.

For example:
- The number 0 appears 3 times,
- The number 1 appears 0 times,

1
- ...
- The number 20 appears 2 times.

Exercise 5
Define a procedure void removeElementArray(int v, int &n, int A[]) which searches the first occur-
rence of v in A[] and removes it from the array by shifting the following elements one position to the left
and inserting 0 at the end of the array.

Exercise 6
Define a procedure void InsertElementArray(int e, int p, int &n, int A[]) to insert an element e
in an array A[] of size n at a specific position p. To insert the element at the position p, all the following
elements should be shifted by one position to the right to release that position.

Exercise 7
Define a function that reverses the elements of an array A of size n without using a second array (in place
reversing).

Exercise 8
Define a procedure void initializeSpace(char A[], int n) that takes as arguments an array of char-
acters and its size, and initializes it with a default empty character (c =’’). Define also another procedure
printArray(char A[], int n) to print all the elements in the one-dimensional array.

Exercise 9
Define a boolean function bool placeMark(char A[], int n, char Mark, int position) that takes as
arguments an array of characters, its size, a character (Mark), and a position. The function is designed to
place a Mark(a character) at a given position. It returns true if the point is successfully placed and false if
the position is occupied (not empty).

Exercise 10
Define a boolean function bool fullArray(char A[], int n) that takes as arguments an array of charac-
ters and its size. The function is designed to check whether the array is full or not.

Exercise 11
Define a procedure printArrayIn2D(char A[], int n, int rows, int columns) to print the elements
of an array in 2D format (rows * columns). For instance, let’s consider the following array:

a b c d e f g h i

The procedure should display this array in the 2D format as follows:

2
a b c
d e f
g h i

Part 2: Tic-Tac-Toe Game in C++


The Tic-Tac-Toe project aims to deliver a classic and entertaining two-player gaming experience. The goal
is to create a console-based Tic-Tac-Toe game in C++, using a 1D array to implement the core game logic
and ensure a smooth and engaging gameplay.

Game Initialization:
The Tic-Tac-Toe game initializes the gameboard as a linear grid (1x9), with cells numbered from 0 to 8. This
configuration provides a straightforward and linear representation of the playing field . Each cell corresponds
to a unique position on the board.

User Interface:
The game leverages a simple console interface to facilitate player interaction. After each player move, the
game dynamically presents the current state of the gameboard. This real-time update ensures that players
have a visual representation of the evolving game, allowing them to strategize and make informed decisions
for their next moves. Your project should ensure the following tasks:
• display an introduction message explaining the game rules and instructions.
• present the current state of the gameboard after each move.

Player Turns:
Players actively participate in the game by inputting their moves during their respective turns. The input
method involves selecting a numbered cell on the gameboard. Each number corresponds to a specific position
on the 1x9 grid, allowing players to specify where they want to place their marker (X or O).

The game incorporates a move validation mechanism to ensure the legality of each player’s input. This
validation process checks whether the selected cell is unoccupied and falls within the valid range of available
positions. If a player attempts to make an invalid move, the game prompts them to input a valid move,
maintaining the integrity of the game rules.

Winning Conditions:
The Tic-Tac-Toe game rigorously evaluates the winning condition following each player’s move, continuously
inspecting the 1x9 gameboard for patterns indicative of victory. The winning criteria are fulfilled when a
player successfully places three markers consecutively in a row, either horizontally, vertically, or diagonally
within the linear structure of the 1x9 gameboard. For instance, a winning scenario could occur in one of the
following cases:
This specific arrangement exemplifies the diverse ways in which a player can achieve victory within the
constraints of the game’s linear layout. Upon detecting a winning sequence, the game decisively declares the
corresponding player as the victor, concluding the round with a definitive outcome. Additionally, the game
exhibits foresight and fairness by adeptly handling ties, a scenario that arises when the entire gameboard is

3
Figure 1: Winning conditions in Tic-Tac-Toe Game (1D)

filled without a conclusive winner. In such instances, the game gracefully acknowledges the draw, providing
closure to the round

To-Do
Implement The Tic-Tac-Toe game for two players, by defining the following modules:
• showBoard: Displays an interface to the users that provides the following options: restore, exit, or
obtain additional information about the game.

• placeMark: update the gameboard by placing a player’s mark (either ’X’ or ’O’) at the specified
position.
• checkWin: is responsible for determining if a player has achieved a winning condition on the game-
board. It checks for three consecutive marks (’X’ or ’O’) in a row, either horizontally, vertically, or
diagonally.

• checkTie: is responsible for determining if the game has ended in a tie, meaning that all cells on the
gameboard are filled, and there is no clear winner.

You might also like