In: Computer Science
Using c++ to find all the peaks elements in a 2d array, each element has eight neighbours
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 26 27 28 29 30 31 32 |
#include <iostream> int main() { constexpr int numRows{ 10 }; constexpr int numCols{ 10 }; // Declare a 10x10 array int product[numRows][numCols]{}; // Calculate a multiplication table for (int row{ 1 }; row < numRows; ++row) { for (int col{ 1 }; col < numCols; ++col) { product[row][col] = row * col; } } // Print the table for (int row{ 1 }; row < numRows; ++row) { for (int col{ 1 }; col < numCols; ++col) { std::cout << product[row][col] << '\t'; } std::cout << '\n'; } return 0; } |
This program calculates and prints a multiplication table for all values between 1 and 9 (inclusive). Note that when printing the table, the for loops start from 1 instead of 0. This is to omit printing the 0 column and 0 row, which would just be a bunch of 0s! Here is the output:
1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 3 6 9 12 15 18 21 24 27 4 8 12 16 20 24 28 32 36 5 10 15 20 25 30 35 40 45 6 12 18 24 30 36 42 48 54 7 14 21 28 35 42 49 56 63 8 16 24 32 40 48 56 64 72 9 18 27 36 45 54 63 72