My output is coming wrong and I realised the reason that I am running "for loop" for "k" a value of "for loop" of j..
Running for loops for "j and k" simultaneously would solve my problem. How to do that?
public void yestimatedvalue() {
for (int i = 1; i < bmarray.length; i++) {
double g = 0;
s = i;
int p = missingrowcolindex(s);
System.out.println("p::\t" + p);
for (int j = 0; j < bmarray[i].length; j++) {
if (j != p) {
g = arraybeta[0][p];
for (int k = 1; k < arraybeta.length; k++) {
g = g + ((bmarray[i][j]) * (arraybeta[k][p]));
}
}
}
System.out.println("g::\t" + g);
}
}
output::
g:: 23.99999999999998
g:: 4.9999999999999964
g:: 5.999999999999995
g:: 1.0
Answering this question Running for loops for "j and k" simultaneously is:
for(int j=0, k=1; (j < bmarray[i].length && k < arraybeta.length); j++, k++){
//....
}
Yes, you can. Java (and all c-like languages) support syntax like this:
for(int i=0, j=5, k=2; i < 10 && j < 50; i++, j+=2, k+=3) {
}
Related
I need to print a table that looks like this if the user entered a 5 using nested for loops:
****5
***45
**345
*2345
12345
I've been working on this for hours and the closest I got was:
int size = scan.nextInt();
for (int i = 1; i <= size; i++)
{
for (int star = size-1; star >= i; star--)
System.out.print("*");
for (int k = 1; k <= i; k++)
System.out.print(i);
System.out.println();
}
Which outputs this:
****1
***12
**123
*1234
12345
You have too many loops; I find it easier to reason about zero based looping so I'm going to use that. Iterate i and j from 0 to size. If j + 1 is greater than size - i - 1 then we want to print j + 1. Otherwise, we want a star. Like,
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (j + 1 > size - i - 1) {
System.out.print(j + 1);
} else {
System.out.print('*');
}
}
System.out.println();
}
For size = 5 that outputs (as requested)
****5
***45
**345
*2345
12345
If you simply must have one based indices, that would be
for (int i = 1; i <= size; i++) {
for (int j = 1; j <= size; j++) {
if (j > size - i) {
System.out.print(j);
} else {
System.out.print('*');
}
}
System.out.println();
}
If you want to keep your loops and avoid if statements, you can tweak last loop by changing
for (int k = 1; k <= i; k++)
into
for (int k = 1+size-i; k <= size; k++)
Btw I also find it way easier to start loops from 0, so updated code would look like this:
int size = scan.nextInt();
for (int i = 0; i < size; i++)
{
for (int star = size-1; star > i; star--)
System.out.print("*");
for (int k = size-i; k <= size; k++)
System.out.print(k);
System.out.println();
}
I hope it helps
I'm trying to create a square shape with numbers like this:
1234
2341
3412
4123
How would I go about doing that?
for (int i = 1; i <= 4; i++) {
for (int j = i; j <=4; j++) {
System.out.print(j);
}
System.out.println();
}
This prints:
1234
234
34
4
How do I get it to restart at 1 again?
You can do by using modulo
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.print((j + i) % 4 + 1);
}
System.out.println();
}
output
1234
2341
3412
4123
You need to update to
for (int i = 1; i <= 4; i++) {
for (int j = i; j <=4; j++) {
System.out.print(j);
}
for (int j = 1; j < i; j++) {
System.out.print(j);
}
System.out.println();
}
add one more for loop
for (int i = 1; i <= 4; i++) {
for (int j = i; j <=4; j++) {
System.out.print(j);
}
for (int j = 1; j <i; j++) {
System.out.print(j);
}
System.out.println();
}
There's probably a simpler way, but this should work:
for (int i = 1; i <= 4; i++) {
for (int j = 3; j <= 6; j++) {
System.out.print ((i + j) % 4 + 1);
}
System.out.println();
}
try this:
public class MyClass {
public static void main(String args[]) {
int count = 0;
for (int i = 1; i <= 4; i++) {
for (int j = i; j <=4; j++) {
System.out.print(j);
count += 1;
}
if(count < 4){
int remain = 4 - count; // to identify how many times it goes again
for(int k=0;k<remain;k++){
System.out.print(k+1);
}
}
count = 0;
System.out.println();
}
}
}
Output:
1234
2341
3412
4123
How about this?
for(int i = 1; i <= 4; i++){
for(int j = 1; j <= 4; j++){
System.out.print(((i + j + 2) % 4) + 1);
}
System.out.println();
}
If you look at the expected output, it has a pattern, which is restricted to a finite and known sequence of numbers. Modulus is the ideal option to handle cases like this.
for (int i = 1; i <= 4; i++) {
for (int j = 0; j <= 3; j++) {
System.out.print(((i + j) % 4) + 1);
}
System.out.println();
}
Modulus (%) gives the remainder of one number divided by another, so (i+j) % 4 always gives a result between 0 and 3.
Add one to that to get the value you want (between 1 and 4).
i want to print a triangle/pyramid style like:
1
323
54345
7654567
here is my code:
int lines = 5;
for (int i = 1; i < lines; i++) {
for (int j = 1; j < lines-i; j++) {
System.out.print(" ");
}
for (int j = i; j > 1; j--) { //this for loop is my problem. any solution?
System.out.print(j);
}
for (int j = i; j < i+i; j++) {
System.out.print(j);
}
System.out.println();
}
what i got is
1
223
32345
4324567
i been studying codes while working at office and i think week long i still could not find a solution to this even i use search in Google.
i am only into enhancing my logic through conditionals and no heavy object oriented or recursion yet.
The problem in your first loop is a problem you figured out in your second one! (and it has something to do with the largest number in the loop)
for (int j = i; j > 1; j--) { //this for loop is my problem. any solution?
System.out.print(j);
}
Look at the numbers on the left of the pyramid. They start where the ones on the right end (every line of the pyramid is symmetrical). And the general formula for that number is i + i - 1, where i is the line number from your outer loop.
The second row starts at 2 * i - 1 = 2 * 2 - 1 = 3. The third row starts at 2 * 3 - 1 = 5 etc.
Your second inner loop should therefore look like this:
for (int j = i + i - 1; j > i; j--) {
System.out.print(j);
}
Here is the complete fixed source.
You have to start at the i-th odd number. This is i*2-1. And you stop at i. This also fixes a spacing difference introduced by changing it to lines = 4.
int lines = 4;
for (int i = 1; i <= lines; i++) {
for (int j = 1; j < lines-i+1; j++) {
System.out.print(" ");
}
for (int j = i*2-1; j > i; j--) { //this for loop is my problem. any solution?
System.out.print(j);
}
for (int j = i; j < i+i; j++) {
System.out.print(j);
}
System.out.println();
}
Run it here: http://ideone.com/AKsc1f
int lines = 4;
for (int i = 1; i <= lines; i++) {
for (int j = 1; j < lines-i+1; j++) {
System.out.print(" ");
}
//replace this
for(int j=0; j<i-1; j++) System.out.print(i*2-j-1);
System.out.print(i);
for(int j=; j<i-1;j++) System.out.print(i+j+1);
//==========
System.out.println();
}
I am implementing some algorithm, in which the number of loop nested levels is determined by the input.
For example, if the input is 2-dimensional, then there two nested for-loops, as below:
for(int i=0; i<N; i++) {
for(int j=i+1; j<N; j++) {
if(table[i][j] == -1) {
for(int c=0; c<C; c++) {
int ii = table[i][c];
int jj = table[j][c];
sort(ii, jj);
if((T[ii][jj] != -1 && T[ii][jj] < l)) {
T[i][j] = l;
break;
}
}
}
}
}
If the input is 3-dimensional, then it would be something like below:
for(int i=0; i<N; i++) {
for(int j=i+1; j<N; j++) {
for(int k=j+1; k<N; k++) {
if(table[i][j][k] == -1) {
for(int c=0; c<C; c++) {
int ii = table[i][c];
int jj = table[j][c];
int kk = table[k][c];
sort(ii, jj, kk);
if((T[ii][jj][kk] != -1 && T[ii][jj][kk] < l)) {
T[i][j][k] = l;
break;
}
}
}
}
}
If there are only these two case, then I can write two versions of nested for-loops. But the dimensions of input could be any value between 2 and N. In this case, how to control the nested loop level dynamically, or is there any alternative to go around of this?
The only real way to do this is to use recursion.
You write a method containing a single for loop, each time around the loop if it needs to go deeper then the method calls itself with the right settings for that nested loop to be run.
Recursion is already been explained here. However, there is another solution as well. Using only one big loop containing a tiny inner loop.
int n = ...;
int dim = ...;
// Raise n to the power of dim: powN = n^dim
long powN = 1;
for (int i = 0; i < dim; ++i) powN *= n;
int[] indices = new int[dim];
for (long i = 0; i < powN; ++i)
{
// Calculate the indices
long bigI = i;
for (int k = 0; k < dim; ++k)
{
indices[k] = bigI % n;
bigI /= n;
}
// Now all your indices are stored in indices[]
}
I was suggesting something like this :
public static void recursiveLoop(int N, int level, int a){
if (level<0)
return;
for (int i=a; i<N; i++){
System.out.println("Level is : "+ level+ " i: "+i );
recursiveLoop(N,level-1,i+1);
}
}
You may explain what you really want to do.
If the Outer for loops are doing nothing but controlling a count, then your Nested for loops are simply a more complicated way of iterating by a count that could be handled by a Single for loop.
like:
for (x = 0; x < 8; x++) {
for (y = 0; y < 10; y++) {
for (z = 0; z < 5; z++) {
DoYourStuffs();
}
}
}
Is equivalent to:
for (x = 0; x < 8*10*5; x++) {
DoYourStuffs();
}
for(int j = 0; j < arrayList.size(); j++) {
System.out.print(arrayList.get(j));
}
I would like to print in groups of 5, but how would the code look? Do i have to store the elements into a variable and print them that way?
How about adding a simple line break after each 5 elements?
for (int j = 0; j < arrayList.size(); j++){
System.out.print(arrayList.get(j) + " ");
if (j % 5 == 4) {
System.out.print("\n");
}
}
for(int j = 0; j < arrayList.size(); j+=5){
for (int i = 0; i < 5; i++){
//Inside this loop you can do whatever you want, concatenate...
System.out.print(arrayList.get(i+j));
}
System.out.println("");
}
You can also use the subList(int fromIndex, int toIndex) to break them.
for(int j = 0; j < arrayList.size(); j++){
if ((j != 0) && (j % 4 == 0) { System.out.println(""); }
System.out.print(arrayList.get(j));
}
Oops... Just realized lots of people answered this already...