habibsafi21 / habibsafi21

0 stars 0 forks source link

main.cpp #1

Open habibsafi21 opened 2 months ago

habibsafi21 commented 2 months ago

include

include

include // For std::out_of_range, std::invalid_argument, and std::bad_alloc

include

using namespace std;

const int INITIAL_SIZE = 100;

// Function to find a value in the array int findValue(int* arr, int size, int value) { for (int i = 0; i < size; ++i) { if (arr[i] == value) { return i; // Return index if found } } return -1; // Return -1 if not found }

// Function to modify a value in the array with exception handling void modifyValue(int* arr, int index, int newValue, int& oldValue) { oldValue = arr[index]; // Store the old value arr[index] = newValue; // Modify with the new value }

// Function to dynamically add a value to the array with exception handling void addValue(int& arr, int& size, int& capacity, int value) { if (size >= capacity) { // Increase the array capacity if the array is full capacity += 1; int newArr = new int[capacity];

        // Copy existing elements to the new array
        for (int i = 0; i < size; ++i) {
            newArr[i] = arr[i];
        }

        delete[] arr;  // Free the old array
        arr = newArr;  // Update pointer to new array
}

arr[size++] = value;  // Add new value and increase the size

}

// Function to remove or replace a value at a given index void removeOrReplace(int* arr, int& size, int index, bool remove) { if (remove) { // Shift all elements after the index to the left for (int i = index; i < size - 1; ++i) { arr[i] = arr[i + 1]; } --size; // Decrease size by 1 } else { arr[index] = 0; // Replace value with 0 } }

int main() { int capacity = INITIAL_SIZE; // Initial capacity int size = 0; // Current number of elements int* arr = new int[capacity]; // Dynamically allocate array

// Reading input from a file
ifstream inputFile("input.txt");
if (inputFile.is_open()) {
    while (inputFile >> arr[size]) {
        ++size;
        if (size >= capacity) {
            // Dynamically resize array if it reaches the capacity
            capacity += 1;
            int* newArr = new int[capacity];
            for (int i = 0; i < size; ++i) {
                newArr[i] = arr[i];
            }
            delete[] arr;
            arr = newArr;
        }
    }
    inputFile.close();
} else {
    cout << "Unable to open file" << endl;
    return 1;
}

int choice, index, value, oldValue;

while (true) {
    // Display menu
    cout << "\nMenu:\n";
    cout << "1. Find a value\n";
    cout << "2. Modify a value\n";
    cout << "3. Add a new value\n";
    cout << "4. Remove or replace a value\n";
    cout << "5. Exit\n";
    cout << "Enter your choice: ";
    cin >> choice;

    // Handle invalid input for menu choice
    if (cin.fail()) {
        cin.clear(); // Clear error flag
        cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Discard invalid input
        cout << "Invalid choice. Please enter a number between 1 and 5.\n";
        continue;
    }

    switch (choice) {
        case 1: // Find a value
            cout << "Enter a number to find: ";
            cin >> value;
            index = findValue(arr, size, value);
            if (index != -1) {
                cout << "Value found at index " << index << endl;
            } else {
                cout << "Value not found!" << endl;
            }
            break;

        case 2: // Modify a value
            try {
                cout << "Enter the index to modify: ";
                cin >> index;
                if (index < 0 || index >= size) {
                    throw out_of_range("Index out of range");
                }
                cout << "Enter the new value: ";
                cin >> value;
                modifyValue(arr, index, value, oldValue);
                cout << "Old value: " << oldValue << ", New value: " << value << endl;
            } catch (const out_of_range& e) {
                cerr << "Error: " << e.what() << endl;
            }
            break;

        case 3: // Add a new value
            try {
                cout << "Enter a value to add: ";
                cin >> value;
                if (cin.fail()) {
                    cin.clear(); // Clear error flag
                    cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Discard invalid input
                    throw invalid_argument("Invalid input. Please enter an integer.");
                }
                addValue(arr, size, capacity, value);
                cout << "Value added successfully. New size: " << size << endl;
            } catch (const invalid_argument& e) {
                cerr << "Error: " << e.what() << endl;
            }
            break;

        case 4: // Remove or replace a value
            cout << "Enter the index to remove/replace: ";
            cin >> index;
            if (index < 0 || index >= size) {
                throw out_of_range("Index out of range");
            }
            cout << "Enter 1 to remove, 0 to replace with 0: ";
            int removeChoice;
            cin >> removeChoice;
            removeOrReplace(arr, size, index, removeChoice == 1);
            cout << "Operation successful. New size: " << size << endl;
            break;

        case 5: // Exit
            cout << "Exiting program.\n";
            delete[] arr; // Clean up dynamically allocated memory
            return 0;

        default:
            cout << "Invalid choice. Please enter a number between 1 and 5.\n";
            break;
    }
}

return 0;

}

PrasanBora commented 2 months ago

Corrected and Improved Code

include

include

include

include

using namespace std;

const int INITIAL_SIZE = 100;

// Function to find a value in the array int findValue(int* arr, int size, int value) { for (int i = 0; i < size; ++i) { if (arr[i] == value) { return i; // Return index if found } } return -1; // Return -1 if not found }

// Function to modify a value in the array with exception handling void modifyValue(int* arr, int index, int newValue, int& oldValue) { oldValue = arr[index]; // Store the old value arr[index] = newValue; // Modify with the new value }

// Function to dynamically add a value to the array with exception handling void addValue(int& arr, int& size, int& capacity, int value) { if (size >= capacity) { // Increase the array capacity if the array is full capacity = 2; // Doubling the capacity is more efficient int* newArr = new int[capacity];

    // Copy existing elements to the new array
    for (int i = 0; i < size; ++i) {
        newArr[i] = arr[i];
    }

    delete[] arr; // Free the old array
    arr = newArr; // Update pointer to new array
}

arr[size++] = value; // Add new value and increase the size

}

// Function to remove or replace a value at a given index void removeOrReplace(int* arr, int& size, int index, bool remove) { if (remove) { // Shift all elements after the index to the left for (int i = index; i < size - 1; ++i) { arr[i] = arr[i + 1]; } --size; // Decrease size by 1 } else { arr[index] = 0; // Replace value with 0 } }

int main() { int capacity = INITIAL_SIZE; // Initial capacity int size = 0; // Current number of elements int* arr = new int[capacity]; // Dynamically allocate array

// Reading input from a file
ifstream inputFile("input.txt");
if (inputFile.is_open()) {
    while (inputFile >> arr[size]) {
        ++size;
        if (size >= capacity) {
            // Dynamically resize array if it reaches the capacity
            addValue(arr, size, capacity, 0); // Temporary value
        }
    }
    inputFile.close();
} else {
    cout << "Unable to open file" << endl;
    delete[] arr; // Clean up dynamically allocated memory
    return 1;
}

int choice, index, value, oldValue;

while (true) {
    // Display menu
    cout << "\nMenu:\n";
    cout << "1. Find a value\n";
    cout << "2. Modify a value\n";
    cout << "3. Add a new value\n";
    cout << "4. Remove or replace a value\n";
    cout << "5. Exit\n";
    cout << "Enter your choice: ";
    cin >> choice;

    // Handle invalid input for menu choice
    if (cin.fail()) {
        cin.clear(); // Clear error flag
        cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Discard invalid input
        cout << "Invalid choice. Please enter a number between 1 and 5.\n";
        continue;
    }

    switch (choice) {
        case 1: // Find a value
            cout << "Enter a number to find: ";
            cin >> value;
            index = findValue(arr, size, value);
            if (index != -1) {
                cout << "Value found at index " << index << endl;
            } else {
                cout << "Value not found!" << endl;
            }
            break;

        case 2: // Modify a value
            try {
                cout << "Enter the index to modify: ";
                cin >> index;
                if (index < 0 || index >= size) {
                    throw out_of_range("Index out of range");
                }
                cout << "Enter the new value: ";
                cin >> value;
                modifyValue(arr, index, value, oldValue);
                cout << "Old value: " << oldValue << ", New value: " << value << endl;
            } catch (const out_of_range& e) {
                cerr << "Error: " << e.what() << endl;
            }
            break;

        case 3: // Add a new value
            try {
                cout << "Enter a value to add: ";
                cin >> value;
                if (cin.fail()) {
                    cin.clear(); // Clear error flag
                    cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Discard invalid input
                    throw invalid_argument("Invalid input. Please enter an integer.");
                }
                addValue(arr, size, capacity, value);
                cout << "Value added successfully. New size: " << size << endl;
            } catch (const invalid_argument& e) {
                cerr << "Error: " << e.what() << endl;
            }
            break;

        case 4: // Remove or replace a value
            try {
                cout << "Enter the index to remove/replace: ";
                cin >> index;
                if (index < 0 || index >= size) {
                    throw out_of_range("Index out of range");
                }
                cout << "Enter 1 to remove, 0 to replace with 0: ";
                int removeChoice;
                cin >> removeChoice;
                removeOrReplace(arr, size, index, removeChoice == 1);
                cout << "Operation successful. New size: " << size << endl;
            } catch (const out_of_range& e) {
                cerr << "Error: " << e.what() << endl;
            }
            break;

        case 5: // Exit
            cout << "Exiting program.\n";
            delete[] arr; // Clean up dynamically allocated memory
            return 0;

        default:
            cout << "Invalid choice. Please enter a number between 1 and 5.\n";
            break;
    }
}

return 0;

}