COS1512 Assignment
3 (COMPLETE
ANSWERS) 2025 - DUE
4 August 2025
NO PLAGIARISM
[Pick the date]
[Type the company name]
, Exam (elaborations)
COS1512 Assignment 3 (COMPLETE
ANSWERS) 2025 - DUE 4 August 2025
Course
Introduction to Programming II (COS1512)
Institution
University Of South Africa (Unisa)
Book
Introduction to Programming
COS1512 Assignment 3 (COMPLETE ANSWERS) 2025 - DUE 4 August 2025;
100% TRUSTED Complete, trusted solutions and explanations Ensure your
success with us..
Question 1 Write a program that asks a user to enter the size of a dynamic
array that stores exam marks obtained by students. Create the dynamic
array and a loop that allows the user to enter an exam mark into each array
element. Loop through the array, find the average mark for the exam and
output it. Delete the memory allocated to your dynamic array before exiting
your program.
#include <iostream>
#include <new> // Required for std::bad_alloc
#include <iomanip> // Required for formatting the output
int main() {
// --- Step 1: Ask the user for the size of the dynamic array ---
int arraySize;
std::cout << "Enter the number of students (size of the array): ";
std::cin >> arraySize;
// Validate that the size is a positive number
if (arraySize <= 0) {
std::cerr << "Error: The array size must be a positive number."
<< std::endl;
return 1; // Exit with an error code
}
// --- Step 2: Create the dynamic array and handle memory allocation
errors ---
// We use a try-catch block to handle the case where memory cannot be
allocated.
3 (COMPLETE
ANSWERS) 2025 - DUE
4 August 2025
NO PLAGIARISM
[Pick the date]
[Type the company name]
, Exam (elaborations)
COS1512 Assignment 3 (COMPLETE
ANSWERS) 2025 - DUE 4 August 2025
Course
Introduction to Programming II (COS1512)
Institution
University Of South Africa (Unisa)
Book
Introduction to Programming
COS1512 Assignment 3 (COMPLETE ANSWERS) 2025 - DUE 4 August 2025;
100% TRUSTED Complete, trusted solutions and explanations Ensure your
success with us..
Question 1 Write a program that asks a user to enter the size of a dynamic
array that stores exam marks obtained by students. Create the dynamic
array and a loop that allows the user to enter an exam mark into each array
element. Loop through the array, find the average mark for the exam and
output it. Delete the memory allocated to your dynamic array before exiting
your program.
#include <iostream>
#include <new> // Required for std::bad_alloc
#include <iomanip> // Required for formatting the output
int main() {
// --- Step 1: Ask the user for the size of the dynamic array ---
int arraySize;
std::cout << "Enter the number of students (size of the array): ";
std::cin >> arraySize;
// Validate that the size is a positive number
if (arraySize <= 0) {
std::cerr << "Error: The array size must be a positive number."
<< std::endl;
return 1; // Exit with an error code
}
// --- Step 2: Create the dynamic array and handle memory allocation
errors ---
// We use a try-catch block to handle the case where memory cannot be
allocated.