Практикум по программированию. Основы. Массивы. Среднее арифметическое 2
From AsIsWiki
(Difference between revisions)
Line 9: | Line 9: | ||
<pre> | <pre> | ||
− | |||
− | |||
− | |||
public class Task06 { | public class Task06 { | ||
Line 17: | Line 14: | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
− | |||
− | |||
System.out.println(); | System.out.println(); | ||
Line 24: | Line 19: | ||
System.out.println(" заполняется так: B[I] = (A[1] + ... + A[I]) / I "); | System.out.println(" заполняется так: B[I] = (A[1] + ... + A[I]) / I "); | ||
System.out.println("-------------------------------------------------"); | System.out.println("-------------------------------------------------"); | ||
− | |||
System.out.print(" Массив A:"); | System.out.print(" Массив A:"); | ||
Line 54: | Line 48: | ||
<pre> | <pre> | ||
+ | // g++ 4.2 | ||
+ | |||
+ | #include <iostream> | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | int main() { | ||
+ | |||
+ | cout << "\n Генератор массива, каждый элемент которого\n"; | ||
+ | cout << " заполняется так: B[I] = (A[1] + ... + A[I]) / I\n"; | ||
+ | cout << "-------------------------------------------------\n"; | ||
+ | cout << " Массив A:"; | ||
+ | |||
+ | srand((int)time(0)); | ||
+ | |||
+ | const int n = 10; | ||
+ | int a[n]; | ||
+ | |||
+ | for (int i = 0; i < n; i++) { | ||
+ | a[i] = rand() % 10; | ||
+ | printf("%5d", a[i]); | ||
+ | } | ||
+ | |||
+ | cout << "\n Массив B: "; | ||
+ | |||
+ | int s = 0; | ||
+ | double b[n]; | ||
+ | |||
+ | for (int i = 0; i < n; i++) { | ||
+ | s += a[i]; | ||
+ | b[i] = s / (i + 1.0); | ||
+ | printf("%.2f ", b[i]); | ||
+ | } | ||
+ | |||
+ | cout << "\n\n"; | ||
+ | return 0; | ||
+ | } | ||
</pre> | </pre> | ||
Latest revision as of 11:03, 19 April 2015
Contents |
[edit] Java
public class Task06 { private static final int n = 10; public static void main(String[] args) { System.out.println(); System.out.println(" Генератор массива, каждый элемент которого "); System.out.println(" заполняется так: B[I] = (A[1] + ... + A[I]) / I "); System.out.println("-------------------------------------------------"); System.out.print(" Массив A:"); int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = (int) (10 * Math.random()); System.out.printf("%5d", a[i]); } System.out.print("\n Массив B: "); int s = 0; double b[] = new double[n]; for (int i = 0; i < n; i++) { s += a[i]; b[i] = s / (i + 1.0); System.out.printf("%.2f ", b[i]); } System.out.println(); } }
[edit] C++
// g++ 4.2 #include <iostream> using namespace std; int main() { cout << "\n Генератор массива, каждый элемент которого\n"; cout << " заполняется так: B[I] = (A[1] + ... + A[I]) / I\n"; cout << "-------------------------------------------------\n"; cout << " Массив A:"; srand((int)time(0)); const int n = 10; int a[n]; for (int i = 0; i < n; i++) { a[i] = rand() % 10; printf("%5d", a[i]); } cout << "\n Массив B: "; int s = 0; double b[n]; for (int i = 0; i < n; i++) { s += a[i]; b[i] = s / (i + 1.0); printf("%.2f ", b[i]); } cout << "\n\n"; return 0; }
[edit] Pascal