Reputation: 31
I have arrayA with 20 random elements in the range of 1000.
ArrayA = [133,456,234,512,632,532,234...20];
I need to create new ArrayB and he needs to look like this:
ArrayB = [331(reversed),15(sum of digits),432(reversed),8(sum ofdigits)];
I'm trying to make it without using lists just with methods(for loop and etc.)
My main problem, for now, is that I don't know how to sum digits of the element on even index, and how to create a method for the reversed number on the odd index of the array.
Random random = new Random();
int arrayA[] = new int[20];
for (int i = 0; i < arrayA.length; i++) {
arrayA[i] = random.nextInt(1000);
}
System.out.println("Array A: " + Arrays.toString(arrayA));
int arrayB[] = new int[arrayA.length];
int sum = 0;
for (int i = 0; i < arrayA.length; i++) {
if (i % 2 == 0) {
sum = sum + i % 10;
i = i / 10;
System.out.println(i);
}
}
So again: I need to reverse the number on the odd index and put them in ArrayB in the same index, and I need to sum digits of the element that is on even index and put them in ArrayB. Ofc, an example is on top of my question!
Upvotes: 3
Views: 879
Reputation: 10184
If you modularize your program in functions, you can win half the battle there. Also, in the example you have given, you are actually taking the reverse at the even index and sum at the odd index. Array indices start with 0 in most programming languages.
public static void getNewArray(int[] arrayA) {
int[] arrayB = new int[arrayA.length];
for(int i=0; i < arrayA.length; i++) {
int newNum = -1;
if(i%2 ==0) { //even index
newNum = reverse(arrayA[i]);
}
else { //odd index
newNum = sumUp(arrayA[i]);
}
arrayB[i] = newNum;
}
Arrays.stream(arrayB).forEach(System.out:: println);
}
public static int reverse(int a) {
int reversedNum = 0;
while (a != 0) {
int last_digit = a % 10;
reversedNum = reversedNum * 10 + last_digit;
a = a / 10;
}
return reversedNum;
}
public static int sumUp(int a) {
int sum = 0;
while (a != 0) {
int last_digit = a % 10;
sum = sum + last_digit;
a = a / 10;
}
return sum;
}
Upvotes: 0
Reputation:
This should be almost 100% correct or at least give an idea of how to do those two operations. I typed it directly in this terrible editing window so it's not tested. I think you have a solid base for the %2 = 0 (even) and %2 > 0 (odd)
char[] digits = String.valueOf(intValue).toCharArray();
int total = 0;
for (char c: digits) {
total += Integer.parseInt(String.valueOf(c);
}
char[] reverse = new char[digits.length];
int ptr = 0;
for (int i = digits.length-1; i >=0; i--) {
reverse[ptr] = digits[i];
ptr++
}
Good luck on your homework assignment.. I mean practicing =P
Upvotes: 0
Reputation: 20914
Partial solution.
import java.util.Arrays;
import java.util.Random;
public class ArrayTst {
public static void main(String[] args) {
Random random = new Random();
int arrayA[] = new int[20];
for (int i = 0; i < arrayA.length; i++) {
arrayA[i] = random.nextInt(1000);
}
System.out.println("Array A: " + Arrays.toString(arrayA));
int arrayB[] = new int[arrayA.length];
for (int i = 0; i < arrayA.length; i++) {
if (i % 2 == 0) {
arrayB[i] = sumDigits(arrayA[i]);
}
else {
arrayB[i] = reverseInt(arrayA[i]);
}
}
}
private static int reverseInt(int input) {
// TODO: Complete
}
private static int sumDigits(int num) {
// TODO: Complete
}
}
Basically loop through arrayA
. If index is even, call method sumDigits
and pass the element from arrayA
and store the method return value in the same index element in arrayB
. And if index is odd, call method reverseInt
, again passing element from arrayA
to the method and store the method return value in arrayB
.
Regarding the methods reverseInt
and sumDigits
, as mentioned in the comments, there are already answers to how to do this on Stack Overflow. The former is here and the latter is here
Upvotes: 4