-1

I am in a intro to Java class. I seem to be having trouble figuring out the algorithm for how i want to go about this.

Currently I need to create and application that generates the number to store in an array element by summing its index and the individual digits of the index. For example, the element with index 17 should be stored as 25... (17+1+7=25) and an element with index 2 would store as 4 (2+0+2=4). I need the program to have 101 elements and then display each value.

I seem to be having trouble figuring out the algorithm for how i want to go about this. Any assistance in the matter would be greatly appreciated. Thanks in advance. I currently am still researching this matter and am still learning to code so please bear with me.

This is the updated code that I have come up with so far

import java.util.Random;
public class Java_Lab_4 {

public static void main(String[] args) {
    int size = 101;
    int max = 101; 
    int[] array = new int[size];
    int loop = 0; 

Random generator = new Random();
//Write a loop that generates 101 integers
//Store them in the array using generator.nestInt(max);

generator.nextInt(max);

for (int i = 0; i<101; i++)
{
    generator.nextInt(max);
}


    }
Ryan Luber
  • 41
  • 5

2 Answers2

1

There are surely many ways to achieve this, but the easiest would be repeated division by 10 (another way would be modular arithmetic, taking the index modulo 10).

This means that you would arrive at something like the following algorithm:

int n = i; // i is the index of the current item
while (n > 0) {
  int x = n;
  if (x > 10) { // we need to deal with the case where i is small
    x = n / 10;
  }
  while (x > 10) { // necessary because we may be dealing with an index > 100
    x = x / 10;
  } // at this point we have the first digit of the index
  a[i] += x; // add this digit to a[i]
  n = n / 10; // get rid of the above digit in the calculation. Note that if n < 10, integer division means that n / 10 == 0
} // at the end of this loop, we have added all digits of i to a[i]
a[i] += i; / now we only need to add the index value itself

There are many ways to solve this, and this is a very straightforward and basic approach. I've added ample comments, but please try to work through the code to understand why this works rather than just copying it.

filpa
  • 3,651
  • 8
  • 52
  • 91
0

Without posting complete code -- because this is an assignment -- here are some things to consider:

array[a] = a + a;

would give you the solution for indices where a < 10. For two/three digit indices, you need to extract the digits with for example

int nthDigit = Integer.parseInt( // Finally, converts the single-char String to an int
    String.valueOf( // converts the char matching the digit to a String
        String.valueOf(a).charAt(n))); // converts 'a' first to a String and 
                                       // then takes its 'n'th character 

where n is 0, 1 or 2. The values of these would then need to be added to the value of the index (a).

Mick Mnemonic
  • 7,808
  • 2
  • 26
  • 30