Friday, November 1, 2024

Array Occupancy Manager

#include <iostream>
using namespace std;

// Function to store a number in an unoccupied array element
void storeNumber(int arr[], bool occupied[], int index, int number) {
// Check if the array element at 'index' is unoccupied
if (!occupied[index]) {
arr[index] = number; // Store the number at the specified index
occupied[index] = true; // Mark the index as occupied
} else {
cout << "Error: Array element at index " << index << " is already occupied." << endl;
}
}


// Function to take a number from an occupied array element
int takeNumber(int arr[], bool occupied[], int index) {
// Check if the array element at 'index' is occupied
if (occupied[index]) {
return arr[index]; // Return the number stored at the specified index
} else {
cout << "Error: Array element at index " << index << " is unoccupied." << endl;
return -13; // Return -13 if the element is unoccupied
}
}


// Function to clear the stored number from an occupied array element
void clearNumber(int arr[], bool occupied[], int index) {
// Check if the array element at 'index' is occupied
if (occupied[index]) {
arr[index] = 0; // Clear the number (optional, as occupied[] will indicate emptiness)
occupied[index] = false; // Mark the index as unoccupied
} else {
cout << "Error: Array element at index " << index << " is already unoccupied." << endl;
}
}

// Function to return the largest value stored in the occupied array elements
int getMax(int arr[], bool occupied[], int size) {
int max = arr[0];
bool found = false; // Flag to check if we have at least one occupied element
for (int i = 0; i < size; i++) {
if (occupied[i]) { // Only consider occupied elements
if (!found || arr[i] > max) {
max = arr[i];
found = true;
}
}
}
if (!found) {
cout << "No occupied elements in the array." << endl;
return -13;
}
return max;
}

// Function to return the smallest value stored in the occupied array elements
int getMin(int arr[], bool occupied[], int size) {
int min = arr[0];
bool found = false; // Flag to check if we have at least one occupied element
for (int i = 0; i < size; i++) {
if (occupied[i]) { // Only consider occupied elements
if (!found || arr[i] < min) {
min = arr[i];
found = true;
}
}
}

if (!found) {
cout << "No occupied elements in the array." << endl;
return -13;
}
return min;
}

// Function to calculate and return the average of numbers in occupied array elements
double getAverage(int arr[], bool occupied[], int size) {
int sum = 0;
int count = 0;
for (int i = 0; i < size; i++) {
if (occupied[i]) { // Only sum up occupied elements
sum += arr[i];
count++;
}
}
if (count == 0) {
cout << "No occupied elements to calculate average." << endl;
return -13;
}
return (1.0 * sum) / count; // Return the average as a double
}

// Function to report the total number of occupied array elements
int getOccupiedCount(bool occupied[], int size) {
int count = 0;
for (int i = 0; i < size; i++) {
if (occupied[i]) { // Count occupied elements
count++;
}
}
return count;
}

// Main function to test the functions
int main() {
const int SIZE = 100; // Define the size of the array
int arr[SIZE] = {0}; // Array to store integers, initialized to 0
bool occupied[SIZE] = {false}; // Boolean array to track occupied elements
// Test cases
storeNumber(arr, occupied, 10, 5); // Store 5 in index 10
storeNumber(arr, occupied, 20, 3); // Store 3 in index 20
storeNumber(arr, occupied, 30, 4); // Store 4 in index 30
storeNumber(arr, occupied, 10, 6); // Attempt to store 6 in index 10 (should fail)
cout << "Take number at index 10: " << takeNumber(arr, occupied, 10) << endl;
cout << "Take number at index 40 (unoccupied): " << takeNumber(arr, occupied, 40) << endl;
clearNumber(arr, occupied, 20); // Clear number at index 20
cout << "Take number at index 20 (after clearing): " << takeNumber(arr, occupied, 20) << endl;
cout << "Max value: " << getMax(arr, occupied, SIZE) << endl;
cout << "Min value: " << getMin(arr, occupied, SIZE) << endl;
cout << "Average value: " << getAverage(arr, occupied, SIZE) << endl;
cout << "Total occupied elements: " << getOccupiedCount(occupied, SIZE) << endl;
return 0;
}

Array Occupancy Manager

#include <iostream> using namespace std; // Function to store a number in an unoccupied array element void storeNumber(int arr[], bool...