0

This was the task: Design, implement and test programs to do each of the following:

a) Find the sum and number of positive integers in a list of 10 integers.

b) Find the smallest number in a list of 10 integers.

c) Determine and output the biggest and smallest numbers in a list of 10 integers. The output should be of the form : “The biggest number 304 was at position 3 in the list” “The smallest number 4 was at position 8 in the list”

So I tried it and the Problem is that everything works except to give me the position of the biggest and smallest number.

    import java.util.Arrays;

public class a5_2 {
    @SuppressWarnings("unlikely-arg-type")
    public static void main (String [] args) {


        int m [] = {-3,23,7,12,4,-44,2,21,3,43} ;

        System.out.println("Array: " + Arrays.toString(m));
        int[] pos = findNumber(m);
            System.out.println("Array without negatives: ");
                for (int i = 0; i < pos.length; i++) 
                    {
                        System.out.println(pos[i]);         
                    }


        System.out.println("Number of pos num: " + pos.length);
            int sum = 0;
                for (int i : pos)
                    sum += i;
        System.out.println("Sum of pos num: " + sum);


        int [] small = findSmallest(pos);
                System.out.println("Smallest Number: ");
                System.out.println(small[0] + " at pos: " + Arrays.asList(pos).indexOf(small[0]));

        int [] big = findBiggest(pos);
                System.out.println("Biggest Number: ");
                System.out.println(big[0] + " at pos: " + Arrays.asList(pos).indexOf(big[0]));
        }

        public static int [] findNumber(int[] sum) {
            int num = 0;
            int n [] = new int [sum.length];
                    for(int i = 0; i < sum.length; i++)
                    {
                        if (sum[i] > 0)
                        {
                            n[num] = sum[i];
                            num++;
                        }
                    }
            int [] pos =  new int [num];
                for (int k = 0 ; k < num ; k++)
                {
                    pos[k] = n[k];
                }

            return pos;

        }
    public static int [] findSmallest(int[] pos) {
        int temp;
        for (int i = 0; i < pos.length; i++)
        {
            for (int j = i + 1; j < pos.length; j++)
            {


                if (pos[i] > pos[j])
                    {
                        temp = pos[i];
                        pos[i] =pos[j];
                        pos[j] = temp;
                    }
            }
        }
        return pos;

    }
    public static int [] findBiggest(int[] pos) {
        int temp;
        for (int i = 0; i < pos.length; i++)
        {
            for (int j = i + 1; j < pos.length; j++)
            {


                if (pos[i] < pos[j])
                    {
                        temp = pos[i];
                        pos[i] =pos[j];
                        pos[j] = temp;
                    }
            }
        }
        return pos;

    }
} 

Output of the position is -1 instead of the output it should give.

Thank you guys in advance :)

2 Answers 2

1

Arrays.asList(pos) is converting into List<int[]> convert it into List<Integer> and then get the index

List<Integer> comArray = Arrays.stream(m).boxed().collect(Collectors.toList());

System.out.println(small[0] + " at pos: " + comArray.indexOf(small[0]));

In java-7, i believe you know already how to convert int[] to List<integer>

 List<Integer> comArray = new ArrayList<>();

        List<Integer> intList = new ArrayList<Integer>();
        for (int i : m)
        {
            intList.add(i);
        }

Verified Code

 @SuppressWarnings("unlikely-arg-type")
    public static void main (String [] args) {


        int m [] = {-3,23,7,12,4,-44,2,21,3,43} ;

        List<Integer> comArray = Arrays.stream(m).boxed().collect(Collectors.toList());

        System.out.println("Array: " + Arrays.toString(m));
        int[] pos = findNumber(m);
            System.out.println("Array without negatives: ");
                for (int i = 0; i < pos.length; i++) 
                    {
                        System.out.println(pos[i]);         
                    }


        System.out.println("Number of pos num: " + pos.length);
            int sum = 0;
                for (int i : pos)
                    sum += i;
        System.out.println("Sum of pos num: " + sum);


        int [] small = findSmallest(pos);
                System.out.println("Smallest Number: ");
                System.out.println(small[0] + " at pos: " + comArray.indexOf(small[0]));

        int [] big = findBiggest(pos);
                System.out.println("Biggest Number: ");
                System.out.println(big[0] + " at pos: " + comArray.indexOf(big[0]));
        }

        public static int [] findNumber(int[] sum) {
            int num = 0;
            int n [] = new int [sum.length];
                    for(int i = 0; i < sum.length; i++)
                    {
                        if (sum[i] > 0)
                        {
                            n[num] = sum[i];
                            num++;
                        }
                    }
            int [] pos =  new int [num];
                for (int k = 0 ; k < num ; k++)
                {
                    pos[k] = n[k];
                }

            return pos;

        }
    public static int [] findSmallest(int[] pos) {
        int temp;
        for (int i = 0; i < pos.length; i++)
        {
            for (int j = i + 1; j < pos.length; j++)
            {


                if (pos[i] > pos[j])
                    {
                        temp = pos[i];
                        pos[i] =pos[j];
                        pos[j] = temp;
                    }
            }
        }
        return pos;

    }
    public static int [] findBiggest(int[] pos) {
        int temp;
        for (int i = 0; i < pos.length; i++)
        {
            for (int j = i + 1; j < pos.length; j++)
            {


                if (pos[i] < pos[j])
                    {
                        temp = pos[i];
                        pos[i] =pos[j];
                        pos[j] = temp;
                    }
            }
        }
        return pos;

    }

 }

Output

Array: [-3, 23, 7, 12, 4, -44, 2, 21, 3, 43]
Array without negatives: 
23
7
12
4
2
21
3
43
Number of pos num: 8
Sum of pos num: 115
Smallest Number: 
2 at pos: 6
Biggest Number: 
43 at pos: 9
Sign up to request clarification or add additional context in comments.

6 Comments

what did you get? it's working for me @preciousbetine
it is throwing a compilation error Cannot find symbol System.out.println(small[0] + " at pos: " + Arrays.stream(pos).boxed().collect(Collectors.toList()).indexOf(small[0])); and it is pointing to the Collectors.toList Function
still showing same error, did you import any additional class
okay which version of java? on 7? @preciousbetine, yes it is from java 8
I am using java 8
|
0

because Arrays.asList(pos) return List<int[]> not List<Integer>.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.