-4

Can someone help me to continue this code using array?this is the sample output Enter number of element(2-10): 5 Element[1]: -3 Element[2]: 0 Element[3]: 10 Element[4]: 2 Element[5]: 7

Max Value is: 10
Min Value is: -3

Ascending order: -3 0 2 7 10
Descending order: 10 7 2 0 -3

I only have this code.

import java.util.Scanner;

public class Array1 {
    public static void main(String[] args) {
        Scanner n = new Scanner(System. in );
        int num[] = new int[];
        int ne = 0;
        int e = 0;
        System.out.print("Enter Number of Elements(2-10): ");
        ne = n.nextInt();
        for (int x = 1; x <= ne; x++) {
            System.out.print("Element[" + x + "]: ");
            e = n.nextInt();
        }
    }
}

The output of this is:

Enter number of element(2-10): 5
Element[1]: -3
Element[2]: 0
Element[3]: 10
Element[4]: 2
Element[5]: 7

Then I don't know how to get the others.

Ivar
  • 6,138
  • 12
  • 49
  • 61
Phriel
  • 5
  • 3
  • There are a few flaws in your code. First. you should not declare a new int array, before you know how much items it will have. (So move `int num[] = new int[];` after `ne = n.nextInt();` and use `new int[ne]` to declare an array of 'ne' items. Then in your for loop you should fill that array. – Ivar Nov 30 '15 at 10:13

2 Answers2

0

I can share the logic but not the code.

The input numbers from user can be added to an array.
Arrays.sort() can be used to sort in ascending or descending order.
Reference: Arrays Sort and Reverse order

Community
  • 1
  • 1
Bharath
  • 1,535
  • 1
  • 12
  • 15
0
  • Change code to below

1.) Take user input first for creating array with size ne.

2.) Initialize your array with size ne.

3.) In loop, keep getting and putting elements in array.

public static void main(String[] args) {
        Scanner n = new Scanner(System.in);

        int ne = 0;
        int e = 0;
        System.out.print("Enter Number of Elements(2-10): ");
        ne = n.nextInt();

        int num[] = new int[ne];

        for (int x = 0; x < ne; x++) {
            System.out.print("Eneter element");
            num[x] = n.nextInt();
        }

        System.out.println("original " + num);
    }

For Sorting, please try youserf first and then post

Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • Thanks. I will try. I have no jcreator at this moment that's why I have trouble in coding a program. I need to execute manually – Phriel Nov 30 '15 at 10:22