Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
3.7 kB
2
Indexable
Never
#include <iostream>
#include <math.h>
#define MAX 400

int tongcheo(int a[][MAX], int n);
void mangcon(int a[][MAX], int n, int size);

struct sub_array
{
    int subraw[MAX];
    int subend[MAX][MAX];
};

int main()
{
    // UNCOMMENT ALL CIN WHEN DONE
    // TESTING WITH N = 4 AND PREDEFINED INPUT

    int n;
    n = 5; // Remove when done
    // std::cin >> n;

    int a[MAX][MAX] = {{1,  2,  3,  4,  5},
                       {6,  7,  8,  9,  10},
                       {11, 12, 13, 14, 15},
                       {16, 17, 18, 19, 20},
                       {21, 22, 23, 24, 25}};
    if (2 <= n && n <= 400)
    {
        /* Testing with n =4 and predefined input, uncomment when done
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                std::cin >> a[i][j];
            }
        }
        */

        // For testing purpose, uncomment to test
        std::cout << "Predefined array: "
                  << "\n";
        for (int row = 0; row < n; row++)
        {
            for (int col = 0; col < n; col++)
            {
                if (col == n - 1)
                {
                    std::cout << a[row][col] << "\n";
                    continue;
                }
                if (a[row][col] < 10)
                {
                    std::cout << a[row][col] << "  ";
                    continue;
                }
                std::cout << a[row][col] << " ";
            }
        }
        std::cout << "\n\n"
                  << "Subarray: "
                  << "\n";
        for (int i = 2; i < n; i++)
        {
            mangcon(a, n, i);
        }
    }
    return 0;
}

int tongcheo(int a[][MAX], int n)
{
    int sum = 0;
    for (int i = 0; i < n; i++)
    {
        sum += a[i][i];
    }
    for (int i = 0; i < n; i++)
    {
        sum += (-1) * a[i][n - 1 - i];
    }
    return sum;
}

void mangcon(int a[][MAX], int n, int size)
{

    // This var is to count subarray
    int subarr = 0;

    // Count subarray
    for (int i = 0; i < n - size + 1; i++)
    {
        for (int j = 0; j < n - size + 1; j++)
        {
            subarr++;
        }
    }

    // Create array to store subarray
    sub_array subarray[subarr];

    // This var is subarray order
    int suborder = 1;

    // Amount of subarray to be printed
    std::cout << "For " << size << " x " << size << " array: \0";
    std::cout << subarr << " subarray in total. \n\n";

    // Limit starting point
    for (int i = 0; i < n - size + 1; i++)
    {
        for (int j = 0; j < n - size + 1; j++)
        {
            std::cout << "This is the " << suborder << "th subarray.\n";

            // Limit ending point according to 'size'
            for (int row = i; row < i + size; row++)
            {
                // Count subarray member
                int submember = 0;

                for (int col = j; col < j + size; col++)
                {
                    // For testing purpose, uncomment
                    if (a[row][col] < 10)
                    {
                        std::cout << a[row][col] << "  ";
                        continue;
                    }
                    std::cout << a[row][col] << " ";

                    // Put raw data to rawarray
                    // subarray[suborder].subraw[submember] = a[row][col];
                }
            }
            std::cout << "\n\n";
            suborder++;
        }
    }

    // For testing purpose, uncomment to print subarray from struct
    // for (int i = 1; i < suborder; i++)
    // {
    //     for (int j = 1; j < subarr; j++)
    //     {
    //         std::cout << subarray[i].subraw[j] << "\n";
    //     }
    // }
}