Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

Making a hollow square in c++

  1. User

    Making a hollow square in c++

  2. Assistant

    Here is an example of how you can create a hollow square using asterisks in C++:

    #include <iostream>
    
    int main() {
        int n;
    
        std::cout << "Enter the size of the square: ";
        std::cin >> n;
    
        // Iterate through each row
        for (int i = 0; i < n; i++) {
            // Print asterisks for the first and last rows
            // Otherwise, print asterisk only at the beginning and end of the row
            if (i == 0 || i == n - 1) {
                for (int j = 0; j < n; j++) {
                    std::cout << "*";
                }
            } else {
                std::cout << "*";
                for (int j = 1; j < n - 1; j++) {
                    std::cout << " ";
                }
                std::cout << "*";
            }
    
            std::cout << std::endl;
        }
    
        return 0;
    }
    

    In this code, the user is first prompted to enter the size of the square. Then, a loop is used to iterate through each row. For the first and last rows, the code prints n asterisks to form a complete row. For the other rows, the code prints an asterisk at the beginning and end of the row, with n-2 spaces in between to create the hollow effect.

  3. User

    Find the extremes of a number of numbers in c++

  4. Assistant

    To find the minimum and maximum values among a set of numbers in C++, you can use a loop to compare each number with the current minimum and maximum values. Here is an example:

    #include <iostream>
    #include <limits>
    
    int main() {
        int n;
        
        std::cout << "Enter the number of values: ";
        std::cin >> n;
    
        int min = std::numeric_limits<int>::max();  // Initialize min with maximum possible value
        int max = std::numeric_limits<int>::min();  // Initialize max with minimum possible value
        
        int num;
    
        for (int i = 0; i < n; i++) {
            std::cout << "Enter value " << (i + 1) << ": ";
            std::cin >> num;
            
            if (num < min) {
                min = num;  // Update the minimum value
            }
            
            if (num > max) {
                max = num;  // Update the maximum value
            }
        }
    
        std::cout << "Minimum value: " << min << std::endl;
        std::cout << "Maximum value: " << max << std::endl;
    
        return 0;
    }
    

    In this code, the user is prompted to enter the number of values. Then, a loop is used to iterate n times to get each value. For each value, it is compared to the current minimum and maximum values. If the value is smaller than the current minimum, it becomes the new minimum. If the value is larger than the current maximum, it becomes the new maximum. Finally, the minimum and maximum values are printed. The std::numeric_limits<int>::max() and std::numeric_limits<int>::min() functions are used to initialize the min and max variables with the maximum and minimum possible integer values, respectively.