Практикум по программированию. Основы. Массивы. Сепаратор
From AsIsWiki
(Difference between revisions)
Line 9: | Line 9: | ||
<pre> | <pre> | ||
− | |||
− | |||
− | |||
public class Task14 { | public class Task14 { | ||
Line 17: | Line 14: | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
− | |||
− | |||
System.out.println(); | System.out.println(); | ||
Line 60: | Line 55: | ||
<pre> | <pre> | ||
+ | // g++ 4.2 | ||
+ | |||
+ | #include <iostream> | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | int main() { | ||
+ | |||
+ | cout << "\n Разделение массива на положительные и отрицательные числа\n"; | ||
+ | cout << "-----------------------------------------------------------\n"; | ||
+ | cout << " Массив A:"; | ||
+ | |||
+ | srand((int)time(0)); | ||
+ | |||
+ | const int n = 16; | ||
+ | int a[n], b[n], k = 0; | ||
+ | |||
+ | for (int i = 0; i < n; i++) { | ||
+ | a[i] = 9 - (rand() % 19); | ||
+ | |||
+ | if (a[i] < 0) { | ||
+ | b[k++] = a[i]; | ||
+ | } | ||
+ | |||
+ | printf("%3d", a[i]); | ||
+ | } | ||
+ | |||
+ | for (int i = 0; i < n; i++) { | ||
+ | if (a[i] >= 0) { | ||
+ | b[k++] = a[i]; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | cout << "\n Массив B:"; | ||
+ | |||
+ | for (int i = 0; i < n; i++) { | ||
+ | printf("%3d", b[i]); | ||
+ | } | ||
+ | |||
+ | cout << "\n\n"; | ||
+ | |||
+ | return 0; | ||
+ | } | ||
</pre> | </pre> | ||
Latest revision as of 20:43, 19 April 2015
Contents |
[edit] Java
public class Task14 { private static final int n = 16; public static void main(String[] args) { System.out.println(); System.out.println(" Разделение массива на положительные и отрицательные числа "); System.out.println("-----------------------------------------------------------"); System.out.print(" Массив A:"); int a[] = new int[n]; int b[] = new int[n]; int k = 0; for (int i = 0; i < n; i++) { a[i] = (int) (9 - 19 * Math.random()); if (a[i] < 0) { b[k++] = a[i]; } System.out.printf("%3d", a[i]); } for (int i = 0; i < n; i++) { if (a[i] >= 0) { b[k++] = a[i]; } } System.out.print("\n Массив B:"); for (int i = 0; i < n; i++) { System.out.printf("%3d", b[i]); } System.out.println(); } }
[edit] C++
// g++ 4.2 #include <iostream> using namespace std; int main() { cout << "\n Разделение массива на положительные и отрицательные числа\n"; cout << "-----------------------------------------------------------\n"; cout << " Массив A:"; srand((int)time(0)); const int n = 16; int a[n], b[n], k = 0; for (int i = 0; i < n; i++) { a[i] = 9 - (rand() % 19); if (a[i] < 0) { b[k++] = a[i]; } printf("%3d", a[i]); } for (int i = 0; i < n; i++) { if (a[i] >= 0) { b[k++] = a[i]; } } cout << "\n Массив B:"; for (int i = 0; i < n; i++) { printf("%3d", b[i]); } cout << "\n\n"; return 0; }
[edit] Pascal