Formal/Semester test – Programming problems
Code comments
Include clear and relevant comments explaining your logic, especially for string manipulation, file
operations, and pointers
Begin each program with a comment stating your name, surname, and student number.
Programming best practices
Descriptive naming – opt for meaningful names for variables and functions.
Use of constants – prefer named constants over magic numbers or hardcoded values.
User input – ensure you validate inputs to prevent errors and handle unexpected values.
Code structure & readability
Indentations – Keep indentation consistent across your code.
Spacing – Use spaces between unrelated section to boost readability.
Scenario
Every year, the Mzansi Music Festival showcases South Africa’s diverse musical tapestry, with
representative beats from every province and culture. The event not only entertains with a rich array
of rhythms, including Gqom, Maskandi, Motswako and Amapiano, but also unites, echoing the
harmonious heritage of the Rainbow Nation.
Problem: Question 1 - Festival Line-Up
Your task is to create a program to manage the list of performing artists. Each artist has several
attributes, and the program should allow a user to calculate the total performance fees for all available
artists, and display artists based on specified filtering criteria.
Q1 - Getting Started
1. Project Setup:
o Create a new project in Code::Blocks and name it as ‘Q1_FormalTest_studentNumber’
where ‘studentNumber’ is your unique student ID.
2. Incorporating the main() function:
o Download the provided text file named ‘[Link]’ and open the file in a text editor.
o Copy the entire content of the file.
o Replace the default content of the main method of your project with the copied
‘mainFunctionQ1’ code.
# 1 of 8
// CMPG 121 – Structured Programming Formal/Semester test SU 1 – SU 5
// Examinator: L Coetzee; Moderator: MJ Zeeman
Q1 - Instructions
NOTE: All the variables must be declared in the main() function. NO global variables are
allowed to be declared and used. Parameters must be used to transfer information to the functions
– no marks for the functions if parameter-transfer is not implemented.
1 Declare a struct named ‘Artist’ with the following data members:
‘name’ – A string to hold the artist’s name.
‘genre’ – A string to represent the music genre (e.g., Hip-hop, House, etc.)
‘performanceFee’ – A double to represent the artist’s performance fee.
‘available’ – A Boolean to indicate whether the artist is available for the festival (true) or
not (false).
2 Write code for the following functions:
2.1 An int function named ‘loadArtistsFromFile’ to receive an array of ‘Artist’ structs.
The function should populate the array with the data from the provided text file ‘[Link]’.
Each line of text in the text file contains the data of an artist. The data is delimited by the
hash (‘#’) character. The structure of each line:
<Name>#<Genre>#<PerformanceFee>#<Availability(1=available,0!=available)>
An example of three lines of text in the text file:
AKA#Hip-hop#10000.50#1
Ladysmith Black Mambazo#Isicathamiya#5500.25#0
Black Coffee#House#15000.00#1
Apply string-handling techniques to extract data for each artist from the file and populate
the array with Artist structs.
Return the number of structs stored in the array.
2.2 A void function named ‘displayArtistInfo’ to receive an array of ‘Artist’ structs, the
number of elements in the array, an optional flag, and an optional string as parameters.
The optional flag indicates if only available artists should be displayed. The default value
for this parameter must be set to false. The string parameter specifies a genre filter. The
# 2 of 8
// CMPG 121 – Structured Programming Formal/Semester test SU 1 – SU 5
// Examinator: L Coetzee; Moderator: MJ Zeeman
default must be an empty string. Display the details of artists based on the provided
filtering criteria in the following format:
Name Genre Performance Fee Availability
--------------------------------------------------------------------
Lira R&B 15000.00 Available
Die Antwoord Alternative 20000.00 Not Available
AKA Hip Hop 18000.00 Available
Mi Casa House 15500.00 Available
Black Coffee Electronic 21000.00 Not Available
Yvonne Chaka Chaka Afro-pop Gauteng 15000.00
2.3 A double function named ‘calcPerformanceTotal’ to receive an array of ‘Artist’ structs
and the number of elements in the array as parameters. Calculate and return the total
performance fees of all the available artists. The output must NOT be displayed from the
function. Example of the output:
********************************************************
Total performance fees for available artists: R 144000.00
********************************************************
3 In the ‘main()’ function:
Write code to declare an array of Artist structs and other required variables. Provide for the
array to hold 50 elements. Call the functions correctly from the ‘main()’ function. Display the total
performance fees of the available artists.
# 3 of 8
// CMPG 121 – Structured Programming Formal/Semester test SU 1 – SU 5
// Examinator: L Coetzee; Moderator: MJ Zeeman
Example program execution
Menu:
1. Display Artist Info
2. Calculate Total Performance Fees for Available Artists
3. Exit
Enter your choice: 1
Filter Options for Displaying Artists:
1. Display All Artists
2. Display Only Available Artists
3. Display Artists by Genre
Enter your filter choice: 2
Name Genre Performance Fee Availability
---------------------------------------------------------------------
Lira R&B 15000.00 Available
AKA Hip Hop 18000.00 Available
Mi Casa House 15500.00 Available
Yvonne Chaka Chaka Afro-pop 15000.00 Available
GoldFish Electronic 17500.00 Available
Freshlyground Afro-fusion 16500.00 Available
. . . .
Menu:
1. Display Artist Info
2. Calculate Total Performance Fees for Available Artists
3. Exit
Enter your choice: 1
Filter Options for Displaying Artists:
1. Display All Artists
2. Display Only Available Artists
3. Display Artists by Genre
Enter your filter choice: 3
Enter genre to filter by (e.g., R&B, Alternative, Hip Hop, Electronic, Folk, etc.): R&B
Name Genre Performance Fee Availability
----------------------------------------------------------------------
Lira R&B 15000.00 Available
Menu:
1. Display Artist Info
2. Calculate Total Performance Fees for Available Artists
3. Exit
Enter your choice: 2
********************************************************
Total performance fees for available artists: R 144000.00
********************************************************
<Menu Display>
Enter your choice: 3
Exiting...
# 4 of 8
// CMPG 121 – Structured Programming Formal/Semester test SU 1 – SU 5
// Examinator: L Coetzee; Moderator: MJ Zeeman
Problem: Question 2 – Ticket Management Program
Your task is to create a program to manage the Mzansi Music Festival’s ticket sales. The program
should allow organisers to sell tickets and record the sales data to a text file.
Q2 - Getting Started
1. Project Setup:
o Create a new project in Code::Blocks and name it as ‘Q2_FormalTest_studentNumber’
where ‘studentNumber’ is your unique student ID.
2. Incorporating the main() function:
o Download the provided text file named ‘[Link]’ and open it in a text editor.
o Copy the entire content of the file.
o Replace the default content of the main function of your project for Question 2 with the
copied ‘mainFunctionQ2’ code.
Q2 - Instructions
NOTE: All operation on the arrays used in this question must be performed using pointers. Usage
of the ‘[ ]’ block bracket notation for array management will result in NO marks for this question,
regardless of functionality.
NOTE: All the variables must be declared in the main() function. NO global variables are allowed
to be declared and used. Parameters must be used to transfer information to the functions – no marks
for the functions if parameter-transfer is not implemented.
1 Declare the necessary parallel arrays to store ticket information, including details such as ticket
ID, category, and price:
1.1 Parallel Arrays – size of each array is 100:
‘arrTicketIDs’ – to store unique Ticket IDs.
‘arrTicketPrices’ – to store the associated ticket prices.
2 Write code for the following functions:
2.1 A void function named ‘captureTicketDetails’ to receive pointers to the arrays
‘arrTicketIDs’ and ‘arrTicketPrices, and a reference to the number of elements in the
arrays as parameters. Capture the ticket category from the user (VIP, Standard,
Economy) and generate a unique ticket ID. Validate the input of the user to be the letter
V, S or E. The ticket ID is composed of:
A prefix based on the category (V for VIP, S for Standard, E for Economy,
# 5 of 8
// CMPG 121 – Structured Programming Formal/Semester test SU 1 – SU 5
// Examinator: L Coetzee; Moderator: MJ Zeeman
A number (incrementing for each ticket category sold),
A hyphen (-),
A 4-digit randomised number.
Example: VIP ticket: V1-1041, Standard ticket: S2-2357, Economy ticket: E3-8874.
Store the ticket ID in the ‘arrTicketIDs’ array.
Compute the ticket price based on the category obtained from the user, where VIP (V) is R
1000.00, Standard (S) is R 500.00, and Economy (E) is R 200.00. Store the computed ticket
prices in the ‘arrTicketPrices’ array at the appropriate index.
Ensure that the number of tickets (elements in the array) is updated accordingly.
Display a confirmation message which includes the ticket ID, for example:
Enter ticket category (V – VIP, S – Standard, E – Economy): VIP
INVALID category. Please enter V, S, or E!
Enter ticket category (V – VIP, S – Standard, E – Economy): B
INVALID category. Please enter V, S, or E!
Enter ticket category (V – VIP, S – Standard, E – Economy): V
Ticket ID: V1-1041 ADDED SUCCESSFULLY!
2.2 A void function named ‘computeTicketPrice’ to receive pointers to the array
‘arrTicketPrices’ and a reference to the number of elements in the array as parameters.
Compute the number of tickets sold in each category as well as the total amount
received per category. Use the information in the ‘arrTicketPrices’ array to compile the
report.
Summary of Ticket Sales
------------------------
2 VIP ticket(s): R 2000.00
1 Standard ticket(s): R 500.00
1 Economy ticket(s): R 200.00
Display the information as shown in the example below:
2.3 A void function named ‘writeToFile’ to receive pointers to the parallel arrays
‘arrTicketIDs’ and ‘arrTicketPrices’, and the number of elements in the arrays as
parameters. Write the ticket IDs and associated prices to a file named ‘[Link]’.
Each line in the file must have a format like: TicketID; TicketPrice.
For example: ‘V1-3291; R 1000.00’
# 6 of 8
// CMPG 121 – Structured Programming Formal/Semester test SU 1 – SU 5
// Examinator: L Coetzee; Moderator: MJ Zeeman
Display a message stating that the data has been written to the file. Example of the
content of the text file (in Notepad):
3 In the ‘main()’ function:
To complete the code in the ‘main()’ function by declaring the arrays and other required
variables in the function. Uncomment the call statements as you complete the code for the
functions. Your functions must be able to communicate with the provided call statements. You
should NOT change the provided call statement.
Example program execution
Menu:
1. Capture Ticket Data
2. Compute Ticket Sales
3. Write to File
4. Exit
Enter your choice: 1
Enter ticket category (V – VIP, S – Standard, E – Economy): V
Ticket ID: V1-1041 ADDED SUCCESSFULLY!
<Menu Display>
Enter your choice: 1
Enter ticket category (V – VIP, S – Standard, E – Economy): E
Ticket ID: E2-1467 ADDED SUCCESSFULLY!
<Menu Display>
Enter your choice: 2
Summary of Ticket Sales
-------------------------------
1 VIP ticket(s): R 1000.00
1 Economy ticket(s): R 500.00
<Menu Display>
Enter your choice: 3
Ticket details WRITTEN to ‘[Link]’
# 7 of 8
// CMPG 121 – Structured Programming Formal/Semester test SU 1 – SU 5
// Examinator: L Coetzee; Moderator: MJ Zeeman
Assessment rubric
Mark
Item
allocation
Q Array and Struct Initialisation -
1 o proper initialisation and use of the ‘Artist’ struct /5
o Initialisation of the array of ‘Artist’ struct and keep track of number of items in the array /3
Function Implementations -
o ‘displayArtistInfo’ – Displaying all artist data /6
o ‘loadArtistsFromFile’ – Loading data from file, handling string manipulation /15
o ‘calculateTotalPerformanceFee’ – Calculate the total performance fee of available artists /6
Integration with Provided Code -
o successful incorporation of provided ‘main()’ function without any conflicts or issues /5
Sub-Total [40]
Q Array Initialisation using Pointers -
2 o proper initialisation of the three parallel arrays using pointers /4
Function Implementations -
o ‘captureTicketData’ – capturing ticket category and validating input, computing ticket price /4
based on category, and generating and storing unique ticket ID, array parameters as
pointers, reference parameters where applicable.
o ‘computeTicketSales’ – using data from the arrTicketPrices array to compile a summary of /4
number of tickets sold per category and income per category.
o ‘writeToFile’ – properly formatting and writing ticket details to ‘[Link]’, array
/4
parameters as pointers, reference parameters where applicable.
Integration with Provided Code -
o successful incorporation of provided ‘main()’ function without any conflicts or issues, /4
proper declarations of arrays, calling functions where appropriate.
Sub-Total [20]
Comments included in program (penalty 2), file name according to the
specification (penalty 2), correct file extension (project zipped) (penalty – not
marked), Logical flow and readability of the code (penalty range from 1 to 5,
depending on severity)
TOTAL 0 60
# 8 of 8
// CMPG 121 – Structured Programming Formal/Semester test SU 1 – SU 5
// Examinator: L Coetzee; Moderator: MJ Zeeman