Практикум по программированию. Основы. Массивы. Нулевые элементы матрицы
From AsIsWiki
(Difference between revisions)
Line 9: | Line 9: | ||
<pre> | <pre> | ||
− | |||
− | |||
− | |||
public class Task16 { | public class Task16 { | ||
Line 18: | Line 15: | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
− | |||
− | |||
System.out.println(); | System.out.println(); | ||
Line 31: | Line 26: | ||
for (int j = 0; j < n; j++) { | for (int j = 0; j < n; j++) { | ||
− | a[i][j] = (int) ( | + | a[i][j] = (int) (3 - 6 * Math.random()); |
if (a[i][j] == 0 ) { | if (a[i][j] == 0 ) { | ||
Line 53: | Line 48: | ||
<pre> | <pre> | ||
+ | // g++ 4.2 | ||
+ | |||
+ | #include <iostream> | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | int main() { | ||
+ | |||
+ | cout << "\n Подсчет нулевых элементов матрицы\n"; | ||
+ | cout << "-----------------------------------\n"; | ||
+ | |||
+ | srand((int)time(0)); | ||
+ | |||
+ | const int n = 3; | ||
+ | const int m = 3; | ||
+ | |||
+ | int a[n][m], k = 0; | ||
+ | |||
+ | for (int i = 0; i < n; i++) { | ||
+ | for (int j = 0; j < n; j++) { | ||
+ | |||
+ | a[i][j] = 2 - (rand() % 5); | ||
+ | |||
+ | if (a[i][j] == 0 ) { | ||
+ | k++; | ||
+ | } | ||
+ | |||
+ | printf("%3d", a[i][j]); | ||
+ | } | ||
+ | |||
+ | cout << "\n"; | ||
+ | } | ||
+ | |||
+ | cout << "-----------------------------------\n"; | ||
+ | cout << " Нулевых элементов: " << k << "\n\n"; | ||
+ | |||
+ | return 0; | ||
+ | } | ||
</pre> | </pre> | ||
Latest revision as of 09:25, 20 April 2015
Contents |
[edit] Java
public class Task16 { private static final int n = 3; private static final int m = 3; public static void main(String[] args) { System.out.println(); System.out.println(" Подсчет нулевых элементов матрицы "); System.out.println("-----------------------------------"); int a[][] = new int[n][m]; int k = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { a[i][j] = (int) (3 - 6 * Math.random()); if (a[i][j] == 0 ) { k++; } System.out.printf("%3d", a[i][j]); } System.out.println(); } System.out.println("-----------------------------------"); System.out.println(" Нулевых элементов: " + k); } }
[edit] C++
// g++ 4.2 #include <iostream> using namespace std; int main() { cout << "\n Подсчет нулевых элементов матрицы\n"; cout << "-----------------------------------\n"; srand((int)time(0)); const int n = 3; const int m = 3; int a[n][m], k = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { a[i][j] = 2 - (rand() % 5); if (a[i][j] == 0 ) { k++; } printf("%3d", a[i][j]); } cout << "\n"; } cout << "-----------------------------------\n"; cout << " Нулевых элементов: " << k << "\n\n"; return 0; }
[edit] Pascal