0% found this document useful (0 votes)
6 views

Bath-24-Programming-Assignment-C++

Uploaded by

l242550
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Bath-24-Programming-Assignment-C++

Uploaded by

l242550
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

PROGRAMMING FUNDAMENTALS

ASSIGNMENT #3
BDS-1C
Question #1: Tic-Tac-Toe Game
Description:

Create a C++ program that allows two players to play a game of Tic-Tac-Toe. Use a 2D
array to represent the game board. Implement functions to:

1. Initialize the Board: Set up an empty 3x3 grid.


2. Display the Board: Show the current state of the game board.
3. Check for a Win: Determine if a player has won the game.
4. Check for a Draw: Determine if the game is a draw.
5. Handle Player Moves: Allow players to enter their moves alternately.

Requirements:

Use clear prompts for player inputs.


Validate moves to ensure cells are not already occupied.
Announce the winner or declare a draw at the end of the game.
Use functions to organize the code logically.

Sample Input:

Player 1 (X), enter row (1-3): 1


Player 1 (X), enter column (1-3): 1

Player 2 (O), enter row (1-3): 1


Player 2 (O), enter column (2-3): 2

... (continues until game ends)

Sample Output:

Current Board:
X | O |
---------
| |
---------
| |

Player 1 (X) wins!

15 Marks

Question #2: Sudoku Validator


Description:

Develop a C++ program that validates a completed Sudoku puzzle using a 2D array. The
program should check whether the Sudoku solution is valid by ensuring that each row, each
column, and each of the nine 3x3 subgrids contain all digits from 1 to 9 exactly once.

Requirements:

Use a 2D array to store the Sudoku grid.


Implement functions to validate rows, columns, and subgrids.
Provide clear output indicating whether the Sudoku is valid or not.

Sample Input:

Enter the Sudoku grid row by row:


5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 3 4 8
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 5 3 7 9 1
7 1 3 9 2 4 8 5 6
9 6 1 5 3 7 2 8 4
2 8 7 4 1 9 6 3 5
3 4 5 2 8 6 1 7 9

Sample Output:

The Sudoku puzzle is valid.

15 Marks
Question #3: Exam Score Analyzer with File Handling
Description:

Extend Assignment 1, Question 5 by incorporating file handling and functions. The program
should:

1. Input: Read exam scores for three students from a file named scores.txt . Each
student has five exam scores.
2. Calculate Averages: Compute the average score for each student.
3. Determine Highest Average: Identify which student(s) have the highest average.
4. Write Results: Save the analysis to a file named results.txt .
5. Handle Ties: Appropriately handle cases where multiple students share the highest
average.

Requirements:

File Handling: Read from scores.txt and write to results.txt .


Functions: Implement functions for reading data, calculating averages, determining the
highest average, and writing results.

Sample scores.txt Content:

Student1 85 90 78 92 88
Student2 80 85 89 91 87
Student3 90 93 88 87 92

Sample results.txt Output:

Student1: Average = 86.6


Student2: Average = 86.4
Student3: Average = 90.0

Student3 has the highest average score.

20 Marks

OPTIONAL:
Question #4: Dungeon Explorer – Treasure Hunt Game
Description:
Design a C++ program that simulates a treasure hunt in a dungeon using 2D arrays and
functions. The program should:

1. Dungeon Representation: Represent the dungeon as a 5x5 grid using a 2D array,


where each cell can contain a number of treasures (1-10) or be empty.
2. Treasure Distribution: Randomly distribute treasures in the dungeon at the start of the
game.
3. Player Movement: Allow the player to move through the dungeon by entering
directions ( North , South , East , West ).
4. Gameplay Mechanics:
Use functions to:
Initialize and display the dungeon layout.
Handle player movement and treasure collection.
Display the player's current treasure count.
End the game when the player collects at least 50 treasures or chooses to
exit.
Provide feedback after each move, indicating treasures found or if the cell is
empty.

Requirements:

Prevent the player from moving outside the dungeon boundaries.


Keep track of the player's current position.
Ensure that treasures are collected only once per cell.

Sample Interaction:

Welcome to Dungeon Explorer!

Current Position: (0,0)


Treasures Collected: 0

Choose a direction to move (N/S/E/W) or Q to quit: E


Moved East to (0,1)
Found 5 treasures!

Treasures Collected: 5

Choose a direction to move (N/S/E/W) or Q to quit: S


Moved South to (1,1)
No treasures here.

Treasures Collected: 5
...

Congratulations! You collected 52 treasures and completed the dungeon.

25 Marks

OPTIONAL:
Question #5: Library Book Tracker
Description:

Develop a C++ program that manages a library's book inventory using file handling and
functions. The program should allow librarians to:

1. Add New Books: Input book details and save them to a file books.txt .
2. View All Books: Read and display all books from books.txt .
3. Search for a Book: Find books by title or author.
4. Delete a Book: Remove a book from the inventory.
5. Update Book Information: Modify details of an existing book.

Requirements:

Use functions for each operation.


Handle file operations securely, ensuring data integrity.
Validate user inputs.

Sample Input:

Choose an option:
1. Add New Book
2. View All Books
3. Search for a Book
4. Delete a Book
5. Update Book Information
6. Exit
Enter choice: 1

Enter ISBN: 978-3-16-148410-0


Enter Title: C++ Programming
Enter Author: Bjarne Stroustrup
Enter Quantity: 10
Book added successfully.

Sample Output:

Choose an option:
1. Add New Book
2. View All Books
3. Search for a Book
4. Delete a Book
5. Update Book Information
6. Exit
Enter choice: 2

Book Inventory:
ISBN: 978-3-16-148410-0, Title: C++ Programming, Author: Bjarne Stroustrup,
Quantity: 10

25 Marks

You might also like