COMP 1045 Programming Concepts Assignment 2 – Programming with C
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
COMP 1045
Programming Concepts
Assignment 2 – Programming with C
INTRODUCTION
This document describes the second assignment for Programming Concepts.
The assignment is intended to provide you with the opportunity to put into practice what you have learnt in the course by applying your knowledge and skills to implement a program that will maintain information on hero and villain characters (using arrays).
This assignment is an individual task that will require an individual submission. You will be required to submit your work via learnonline before Tuesday 14 June (swot vac), 11:30 pm.
This document is a kind of specification of the required end product that will be generated by implementing the assignment. Like many specifications, it is written in English and hence will contain some imperfectly specified parts. Please make sure you seek clarification if you are not clear on any aspect of this assignment.
ASSIGNMENT OVERVIEW
You are required to write a C program that will manage character (heroes and villain) information. Character (hero and villain) information will be stored in a text file that will be read in when the program commences. Once the initial character (hero and villain) information has been read in from the file, the program should allow the user to interactively query and manipulate the character (hero and villain) information. Please ensure that you read sections titled 'Assignment Specification' below for further details.
Please ensure that you read sections titled ‘Assignment Specification’ below for further details.
GRADUATE QUALITIES
By undertaking this assessment, you will progress in developing the qualities of a University of South Australia graduate.
The Graduate qualities being assessed by this assignment are:
• The ability to demonstrate and apply a body of knowledge (GQ1) gained from the lectures, practicals and readings. This is demonstrated in your ability to apply problem solving and programming theory to a practical situation.
• The use of communication skills (GQ6) by producing code that has been properly formatted; and writing adequate, concise and clear comments.
You will also progress in developing the following graduate qualities:
• The development of skills required for lifelong learning (GQ2), by searching for information and learning to use and understand the resources provided (C standard library, lectures, practical exercises, etc); in order to complete a programming exercise.
• The ability to effectively problem solve (GQ3) using C to complete the programming problem. Effective problem solving is demonstrated by the ability to understand what is required, utilise the relevant information from lectures, readings and practical work, write C code, and evaluate the effectiveness of the code by testing it.
• The ability to work autonomously (GQ4) in order to complete the task.
• The application of international standards (GQ7) by making sure your solution conforms to the standards presented in the C Programming Practice slides (available on the course website).
ASSIGNMENT SPECIFICATION – MANAGE CHARACTER INFORMATION
Write a menu driven program called assign2_yourEmailId.c that will allow the user to enter commands and process these commands until the quit command is entered. The program will store and maintain character information (using two arrays – one 2D array that stores the character’s name and one 1D array that stores the character’s health rating). Character information will be stored in a text file that will be read in when the program commences. Once the initial character data has been read in from the file, the program should allow the user to interactively query and manipulate the character information.
Input
When your program begins, it will read in character (hero and villain) information from a file called characters.txt. This is a text file that stores information pertaining to characters (heroes and villains). An example input file called characters.txt can be found on the course website (under the Assessment tab). You may assume that all data is in the correct format. The name of the character (hero or villain) is stored on a separate line. The very next line contains the hero or villain’s health value. This information is seen in Figure 1 below:
After the program has stored the data (using two arrays, one that stores the character’s name (using a 2 dimensional array of characters) and one that stores the character’s health value (using an array of integers)), it will enter interactive mode as described in the following section.
Wonder Woman
90
Batman
80
The Joker
80
Superman
100
Catwoman
50
Aquaman
30
Iron Man
50
Hulk
80
Thanos
90
Figure 1: Character information file format (characters.txt).
Your program will maintain two arrays as follows:
/* 2D array of characters to store character’s name */
char character_array[MAX_CHARACTERS][STRING_LENGTH] = {'\0'};
/* Array of integers to store character’s health value */
int health_array[MAX_CHARACTERS] = {0};
Once the above information is read in from the file, the two arrays will be populated as follows:
character_array
(2D array of
characters)
Wonder Woman |
Batman |
The Joker |
Superman |
Catwoman |
Aquaman |
Iron Man |
Hulk |
Thanos |
health_array
(array of
integers)
90 |
80 |
80 |
100 |
50 |
30 |
50 |
80 |
90 |
Note: That the character and their health value are stored in corresponding positions in each array, i.e. the character stored in character_array[0] has their health value stored in health_array[0], the character in character_array[1] has their health value stored in health_array[1] and so on...
Interactive Mode
Your program should enter an interactive mode after the character (hero and villain) information has been read from the file. The program will allow the user to enter commands and process these commands until the quit command is entered. The following commands should be allowed:
1. list:
Displays for all characters (heroes and villains), the character’s name and their associated health value. Outputs the contents of the character’s name and health arrays as seen below in the section titled Screen Format.
2. search:
Prompts for and reads the character’s name and searches for the character in the character array. If the character is found in the character array, the character’s name and their health value, are displayed to the screen as seen below (in the section titled Screen Format). If the character is not found in the array of characters (2D array of char), an error message stating the character has not been found is displayed to the screen.
3. reset:
Prompts for and reads the character’s name and searcher for the character in the character array. If the character is found in the array of characters, the character’s corresponding health value (stored in the health array) is reset to 100. If the character is not found in the array of characters, an error message stating the character has not been found is displayed to the screen.
4. add:
Prompts for and reads the character’s name. If the character does not already exist (i.e. a match is not found on the character’s name), the character is added to the array of characters and their corresponding health value of 100 is added to the health array. A message is displayed to the screen indicating that this has been successfully added.
The character must be added after the last character entry stored in the array (i.e. at the end of the array). If the character is already stored in the character array, an error message is displayed. No duplicate entries are allowed.
5. remove:
Prompts for the character’s name. If the character is found, he/she is removed from the array of characters (along with their associated health value stored in the health array) and a message is displayed to the screen indicating that this has been done. If the character is not found in the character array, an error message is displayed.
6. high
Displays the characters with the highest health rating to the screen – see section titled ‘Screen Format’ below. If no characters are stored in the array or a character with the highest health rating cannot be found (i.e. all characters have a health or zero), display an error message accordingly.
7. battle:
Prompts for the name of the two opponents to do battle. The program searches for each character in the array of characters and if the character is not found in the array of characters, an error message is displayed to the screen and the user is asked to enter another character.
If the opponents are found in the array of characters, he/she is then able to do battle and the number of battle rounds the heroes/villains will undertake (a number between 1-5 inclusive) is prompted for and read. One battle may have many (1-5 inclusive) rounds. The heroes/villains battle until either an opponent dies (health status is reduced to zero) or the number of battle rounds have been completed. For each individual battle round, the hero/villain will sustain a certain amount of damage to their health rating (make sure you update the health array). The amount of damage sustained from the battle will be a randomly generated value between 0–50 inclusive. After each round, each opponents damage
value (i.e. randomly generated number between 0–50 inclusive) and current health value (i.e. calculated by: health value – damage value) are displayed to the screen.
After every battle (however many rounds), a winner is determined, i.e. the opponent with the higher health value wins the battle.
8. quit:
Causes the program to quit, outputting the contents of the character and health arrays to a file (see section ‘Final Output’ below for format).
Note:
The program should display an appropriate message if a character is not found matching a search criteria. Appropriate messages should also be displayed to indicate whether a command has been successfully completed.
Please refer to the sample output (at the end of this handout) to ensure that your program is behaving correctly and that you have the correct output messages.
Each time your program prompts for a command, it should display the list of available commands. See the sample output (at the end of this handout) to ensure that you have the output format correct.
For example:
Please enter choice
[list, search, reset, add, remove, high, battle, quit]:
Menu input should be validated with an appropriate message being displayed if incorrect input is entered.
Screen Format
The list command (display_characters () function) should display the character (hero and villain) information in the following format:
=======================================
- Character Summary - =======================================
- Name Health - =======================================
- Wonder Woman 90 -
- Batman |
80 - |
- The Joker |
80 - |
- Superman |
100 - |
- Catwoman - |
50 - |
- Aquaman |
30 - |
- Iron Man |
50 - |
- Hulk |
80 - |
- Thanos |
90 - |
=======================================
The search command should display individual character (hero/villain) information to the screen in the following format:
Catwoman's current health: 50%
The high command should display the character(s) with the highest health rating to the screen in the following format:
Characters with the highest health rating of 90% are:
--> Wonder Woman
--> Thanos
Final Output
When your program finishes (because you entered the quit command) your program should output the contents of the array of characters and the array of health values to a file called new_characters.txt.
The format of this file should exactly match that of the input file.
PRACTICAL REQUIREMENTS
It is recommended that you develop this part of the assignment in the suggested stages.
It is expected that your solution WILL include the use of:
• Your solution in a file called assign2_yourEmailId.c. For example: assign2_bonjy007.c
• Appropriate and well constructed while and/or for loops. Marks will be lost if you use break, continue, goto, pass or return (or similar) statements in order to exit from loops.
• Appropriate and well constructed if, else if, else statements (as necessary).
• Use of a 2 dimensional (2D) array to store the character (hero/villain) names, i.e 2D character array – 10 rows x 21 columns (21 accounts for null character).
• Use of an integer array to store character health ratings, i.e. 1D integer array – size 10.
• Eight functions (see below for descriptions):
o int read_character_file(FILE*, char[][STRING_LENGTH], int []);
o void write_to_file(char[][STRING_LENGTH], int [], int);
o void display_characters(char[][STRING_LENGTH], int [], int);
o int find_character(char [], char[][STRING_LENGTH], int);
o int add_character(char[][STRING_LENGTH], int [], char [], int, int*);
o int remove_character(char[][STRING_LENGTH], int [], char [], int*);
o void display_highest_health(char [][STRING_LENGTH], int [], int);
o void do_battle(char[][STRING_LENGTH], int [], int, int);
1. int read_character_file(FILE*, char[][STRING_LENGTH], int []);
This function takes a file name and two arrays (character_array and health_array) as parameters. This function reads the names of the characters stored in that file into a 2 dimensional character array called character_array and reads their associated health value into an array called health_array. The function returns the number of characters (heroes and villains) stored in the file. The file will already have been opened for reading before being passed into this function. Please note: This function will be provided for you and explained in class. You may use your own function or use the function provided … the decision is yours. : )
2. void write_to_file(char[][STRING_LENGTH], int [], int);
This function will output the contents of the character array (array of character names) and the health array (array of health values) to a file in the same format as the input file. The file will need to be opened for writing in this function (and of course closed once all writing has been done). The function accepts the array of characters, the array of health values and the number of characters (heroes and villains) stored in the arrays as parameters.
3. void display_characters(char[][STRING_LENGTH], int [], int);
This function will take the array of characters, the array of health values and the number or characters (heroes and villains) stored in the arrays as parameters and will output the contents of the arrays to the screen. This function displays the information to the screen in the format specified in the assignment specifications under the section - 'Screen Format'.
4. int find_character(char [], char[][STRING_LENGTH], int);
This function will take the character’s name, the array of characters (heroes and villains) and the number of characters (heroes and villains) stored in the array and will return the position (index) of the character found in the array of character (heroes and villains).
If the character is not found, the function returns - 1 (indicating the character could not be found).
5. int add_character(char[][STRING_LENGTH], int [], char [], int, int*);
This function takes the array of characters (heroes and villains), the array of health values, the character's name, health status and the number of characters (heroes and villains) as a call-by- reference parameter. If the character does not already exist (i.e. a match is not found on character name), it adds the character into the array of characters (heroes and villains). Increment the number of characters (heroes and villains) by one if the character is added into the array.
The character must be added after the last character entry stored in the array. You will need to check that you are not exceeding array bounds.
If the array of characters (heroes and villains) is full, the function does not add the new character and returns - 1.
If the character name already exists, the function does not add the new character and returns 0. If the character is successfully added to the array, the function returns 1.
Hint: this function MUST make use of the find_character function.
6. int remove_character(char[][STRING_LENGTH], int [], char [], int*);
This function takes the array of characters (heroes and villains), the array of health values, the character's name and the number of characters (heroes and villains) as a call-by-reference parameter and determines whether the character exists in the array of characters (heroes and villains).
If the character is found, the character is removed from the character (heroes and villains) array (and the corresponding health array) at the position returned by function find_character and the function returns 1 (indicating the character has been successfully removed).
The information must be removed maintaining the order of the character (heroes and villains) array (Hint: shift all elements one position down the array).
If the character is not found in the characters (heroes and villains) array, the function returns 0 (indicating the character has not been removed).
Hint: this function MUST make use of the find_character function.
7. void display_highest_health(char [][STRING_LENGTH], int [], int);
This function will take the array of characters, the array of health values and the number of character (heroes and villains) stored in the arrays as parameters. This function displays the character(s) with the highest health rating to the screen.
This function displays the information to the screen in the format specified in the assignment specifications under the section - 'Screen Format'.
8. void do_battle(char[][STRING_LENGTH], int [], int, int);
This function takes the array of characters, the array of health values and the position of the two characters that are about to do battle (i.e. position is the location/index of the character stored in the array of characters and their associated health value stored in the health array. This is useful so we can update the character's health value after every battle).
This function prompts for and reads the number of battle rounds the heroes/villains will undertake (a number between 1-5 inclusive). The function allows the heroes/villains to battle until either an opponent dies (health status is reduced to zero) or the number of battle rounds have been completed. For each individual battle round, the hero/villain will sustain a certain amount of damage to their health rating. The amount of damage sustained from the battle will be a randomly generated value between (0 - 50 inclusive).
General algorithm is as follows:
while (number of battle rounds have not been completed and both opponents are still alive)
randomly generate a damage value sustained from battle and update opponent 1's health value by health_array[opponent1_pos].
randomly generate a damage value sustained from battle and update opponent 2's health value by health_array[opponent2_pos].
display opponent 1 round results (as seen in the sample output)
display opponent 2 round results (as seen in the sample output)
determine the winner of the battles - the character with the most health left at the end of all the battle rounds is the winner.
display the winner to the screen and also report if a character has died as a result of battle (refer to sample output for layout).
• Output that strictly adheres to the assignment specifications. If you are not sure about these details, you should check with the ‘Sample Output’ provided at the end of this document or post a message to the discussion forum.
• No global variables.
• Appropriate functions (as above) – 1 idea per function.
• Use of #define for symbolic constants (i.e. #define MAX_CHARACTERS 10). i.e. no magic numbers.
• Good programming practice:
1. Consistent commenting and indentation. You are to provide comments to describe: your details, program description, all variable definitions, function prototypes and definitions and every significant section of code.
2. Meaningful variable names (no single letter identifier names).
3. Consistent indentation and code layout.
NOTE: Do not use break, pass, goto, or continue statements in your solution – doing so will result in a significant mark deduction. Do not use the return statement or the like as a way to break out of loops.
PLEASE NOTE: You are reminded that you should ensure that all input and output conform to the assignment specifications. If you are not sure about these details you should check with the sample output provided at the end of this document or post a message to the discussion forum in order to seek clarification.
STAGES
It is recommended that you develop this part of the assignment in the suggested stages. Many problems in later stages are due to errors in early stages. Make sure you have finished and thoroughly tested each stage before continuing.
The following stages of development are recommended:
Stage 0 (preparation)
Start by setting up the project correctly in Microsoft Visual Studio C++ 2022 (refer to instructions on the course website if you are unsure of how to do this).
2022-06-07