Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Java Enter a sequence of real numbers and calculate the number of elements thatis greater than in the previous element of the sequence

Sco0b

Coder
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

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);
    }
}
 
"Something did not work out for you" and someone else is expected to tell you what it is ???
If you want to be helped, explain your problem.
 

Latest posts

Buy us a coffee!

Back
Top Bottom