Saeed Zaghdoudi
Saeed Zaghdoudi

Reputation: 3

Sum of digits in random generated arrays

I know this may stand for a silly question but I have got a lot of problems with this.
I will first explain how it should work :
1)Generate random Array with size in range <4,7>
2)Fill it with random elements in range <100, 999>
3)Print the index of three numbers with biggest digit sum
So the question is-how? I know I should implement this:

             SumOfDigits += ElementFromArray % 10;
              ElementFromArray /= 10;

But i have got no idea where. I tried to add this as a if (i>0) loop inside for loop-but its not working.
Also how at the end im going to print the proper elements? Should I use Arrays.sort(tab) and then System.out.println(tab[tab.length - 1]) (and so on with -2, -3 for 3 last elements)?

import java.util.Arrays;
import java.util.Random;

public class Ex1 {

    public static void main(String[] args) {
        Random rand = new Random();
        int size = rand.nextInt(4) + 4;

        int tab[] = new int[size];

        for (int i = 0; i < tab.length; i++) {
            int elements = rand.nextInt(900) + 100;
            tab[i] = elements;

        }
        System.out.println(Arrays.toString(tab));
    }
}

Upvotes: 0

Views: 520

Answers (2)

Joakim Danielson
Joakim Danielson

Reputation: 52013

If we aim for a solution using only arrays I would use a 2d array to hold the sum of digits and the index of the corresponding number in the tab array

So first create the array based on the size of the original array

int[][] sums = new int[size][2];

then in the for loop, calculate the sum of the random number and store it and the index

sums[i][0] = elements / 100 + (elements / 10) % 10 + elements % 10;
sums[i][1] = i; 

Then sort the sums array using a custom comparator

Arrays.sort(sums, new Comparator<int[]>() {
    @Override
    public int compare(int[] o1, int[] o2) {
        return Integer.compare(o2[0], o1[0]);
    }
});

And finally print the index and number of the numbers of the top 3

for (int i = 0; i < 3; i++) {
    System.out.printf("%d: %d\n", sums[i][1], tab[sums[i][1]]);
}

Upvotes: 1

Just use while loop: here is a quick and dirty solution:

private static void main10(String[] args) {
    Random rand = new Random();
    int size = rand.nextInt(4) + 4;

    int[] tab = new int[size];

    for (int i = 0; i < tab.length; i++) {
        int element = rand.nextInt(900) + 100;
        tab[i] = element;

    }
    System.out.println(Arrays.toString(tab));
    // calculate digits:
    int[] digitsums = new int[size];
    for (int i = 0; i < tab.length; i++) {
        int element = tab[i];
        int sumOfDigits = 0;
        while (element > 0) {
            sumOfDigits += element % 10;
            element /= 10;
        }
        digitsums[i] = sumOfDigits;
    }
    System.out.println(Arrays.toString(digitsums));
    int[] copyOfdigitsums = Arrays.copyOf(digitsums, digitsums.length);
    for (int i = 1; i <= 3; i++) {
        int j = getIndexOfLargest(copyOfdigitsums);
        System.out.println("index of " + i + "largest is " + j + ", with a digitsum of " + copyOfdigitsums[j]);
        copyOfdigitsums[j] = 0;
    }
}

static int getIndexOfLargest(int[] digitsums) {
    int largest = 0;
    int index = 0;
    for (int i = 0; i < digitsums.length; i++) {
        int d = digitsums[i];
        if (largest < d) {
            largest = d;
            index = i;
        }
    }
    return index;

}

Upvotes: 0

Related Questions