#include #include #include // For rand() and srand() #include // For time() int main() { // Seed for random number generation std::srand(static_cast(std::time(0))); // Define 2D array size const int rows = 1000; const int cols = 1000; int array[rows][cols]; // Initialize the 2D array with random values for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { array[i][j] = std::rand() % 100; // Random values between 0 and 99 } } // Measure time for row-wise access auto start_row = std::chrono::high_resolution_clock::now(); long long sum_row = 0; for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { sum_row += array[i][j]; // Accessing row-wise } } auto end_row = std::chrono::high_resolution_clock::now(); auto duration_row = std::chrono::duration_cast(end_row - start_row).count(); // Measure time for column-wise access auto start_col = std::chrono::high_resolution_clock::now(); long long sum_col = 0; for (int j = 0; j < cols; ++j) { for (int i = 0; i < rows; ++i) { sum_col += array[i][j]; // Accessing column-wise } } auto end_col = std::chrono::high_resolution_clock::now(); auto duration_col = std::chrono::duration_cast(end_col - start_col).count(); std::cout << "Row sum: " << sum_row << "\n"; std::cout << "Column sum: " << sum_col << "\n"; // Output the results std::cout << "Time taken for row-wise access: " << duration_row << " microseconds\n"; std::cout << "Time taken for column-wise access: " << duration_col << " microseconds\n"; return 0; }