Reputation: 69
I created an array with 50 elements.
The first 25 elements are equal to the square of the index and the last 25 elements are equal to the index times 3.
This is my current output: 0 1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400 441 484 529 576 75 78 81 84 87 90 93 96 99 102 105 108 111 114 117 120 123 126 129 132 135 138 141 144 147
My desired output is this:
0 1 4 9 16 25 36 49 64 81
100 121 144 169 196 225 256 289 324 361
400 441 484 529 576 75 78 81 84 87
90 93 96 99 102 105 108 111 114 117
120 123 126 129 132 135 138 141 144 147
I have tried using the formatting feature (printf), but I can't seem to get it to work. I've also tried using an if statement, but that gave me a "cannot convert int to boolean" error.
I've searched some similar questions but their solutions are either too complex for me to understand or they don't work for my specific code since I am already using for loops.
Does anybody have an easy way to achieve this? This is for a homework assignment. I have the logic part down, which ironically should be the hard part but this formatting part has me stumped lol
I appreciate any help!
This is my code right now:
int [] Number = new int [50];
for(int i = 0; i < Number.length/2; i++) {//first loop
System.out.print(i * i + " ");
}
for(int i = 25; i < Number.length; i++) {//second loop
System.out.print(i * 3 + " ");
}
Upvotes: 2
Views: 5123
Reputation: 1
int[] array = new int[50];
int count = 0; // To print 10 elements per line
for(int I = 0; I < array.length; I++) {
if (count == 10) {
System.out.println();
count = 0;
}
System.out.print(a1[i] + " ");
count++;
}
Upvotes: 0
Reputation: 307
I hope this helps
int[] arrNum = new int[50];
for (int i = 0; i < numbers.length; ++i) { //First loop to enter the data
numbers[i] = ((i < 25) ? i * i : i * 3);
}
for (int i = 0; i < numbers.length; ++i) { //Second loop to output data
if (i % 10 == 0) System.out.println;
System.out.print(numbers[i] + " ");
}
Upvotes: 0
Reputation: 957
Does the array require to store the correct values?
int[] numbers = new int[50];
for (int i=0; i<numbers.length; ++i) { //calculating correct value into the array
numbers[i] = (i < 25) ? i * i : i * 3;
}
for (int i=0; i<numbers.length; ++i) { //displaying data
if (i % 10 == 0 && i > 0) {
System.out.println();
}
System.out.print(numbers[i] + " ");
}
Upvotes: 0
Reputation: 47971
You don't need an array. What you want to do is:
Loop 50 times
i
to 0
i
to 1
i
to 49
Each time:
i < 25
print i * i
i >= 25
print i * 3
Every 10th time, print a new line.
Here is how you can do it without an array:
int size = 50;
for (int i = 0; i < size; i++) {
// Print a newline after every 10 elements
if (i != 0 && i % 10 == 0) {
System.out.println();
}
// Print i * i for 0 up to but excluding 25
// Print i * 3 after that
if (i < size / 2) {
System.out.print(i * i + " ");
} else {
System.out.print(i * 3 + " ");
}
}
If you really wanted to loop over the elements of an array:
int[] myArr = new int[50];
for (int i = 0; i < myArr.length; i++) {
if (i != 0 && i % 10 == 0) {
System.out.println();
}
if (i < size / 2) {
System.out.print(i * i + " ");
} else {
System.out.print(i * 3 + " ");
}
}
Also it is not a good idea to name a variable as Number
, because:
camelCase
. Class names in PascalCase
(see here).Upvotes: 3
Reputation: 127
Try this:
int[] Number = new int[50];
for (int i = 0; i < Number.length / 2; i++) {//first loop
if (i % 10 == 0 && i > 0) {
System.out.println();
}
System.out.print(i * i + " ");
}
for (int i = 25; i < Number.length; i++) {//second loop
if (i % 10 == 0) {
System.out.println();
}
System.out.print(i * 3 + " ");
}
Upvotes: 1