Good afternoon, I have a task "Enter a sequence of real numbers and calculate the number of elements that
is greater than in the previous element of the sequence " I took an example of another option and tried to make my own version. But something didn't work out for me. Tell me what
this is task 2
is greater than in the previous element of the sequence " I took an example of another option and tried to make my own version. But something didn't work out for me. Tell me what
this is task 2
Java:
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
import static java.lang.System.out;
public class LR3 {
public static void main(String[] args) {
Random rand = new Random();
double h = 0.3, a = 0.5, b = 11;
int count_of_variables;
double x1, x2;
Scanner sc = new Scanner(System.in);
out.print("Вибиріть завдання (1; 2; 3): ");
switch (sc.nextInt()) {
case 1:
task1(a, b, h);
break;
case 2:
out.println("Введіть кількість змінних (мін. 10) : ");
count_of_variables = sc.nextInt();
double[] variables = new double[count_of_variables];
for (int i = 0; i < variables.length; i++) {
variables[i] = rand.nextInt(30);
}
task2(variables);
break;
case 3:
int max_k = 7;
out.println("Рекомендованно x = 0,6 or 2,8");
out.println("Введіть x1: ");
x1 = sc.nextDouble();
task3(x1,max_k);
out.println("Введіть x2: ");
x2 = sc.nextDouble();
task3(x2,max_k);
break;
}
}
static void task1(double a, double b, double h) {
out.println(" x y z");
out.println("------------------------");
for (double i = a; i <= b; i += h) {
out.printf("%.1f %.4f %.4f\n", i , Math.sin(i)/Math.pow(i,2) , Math.cos(i)/i);
}
}
static void task2(double[] variables) {
out.println("array is: " + Arrays.toString(variables));
double prd = variables[0];
for (int i = 1; i < variables.length; i++) {
if (variables[i] > variables[i+1]) {
prd = prd+1;
}
}
out.printf("Кількість елементів, які більші попереднього елемента послідовності =%i" , prd);
}
static void task3(double x, int max_k) {
int k = 1;
double summ = 0;
for (int i=1;i<=max_k;i++) {
k*=i;
summ += ((Math.pow(x,i)/(k+1)));
}
out.printf("Сумма ряду при x = "+ x + " дорівнює " + "%.4f\n",summ);
}
}