-4

This is the loop.

    for(m=0; m<10; m++){
        for (i=0; i<=K; i++) {
            A[i] = i;
     }
   }

And this is print code.

System.out.println("A:");
    for (i=0; i<20; i++) {
        System.out.printf("%.1s\t", A[i]);
        if (i==9) System.out.println();
    }

If user enter K as 3 then result is:

A:
0   1   2   3   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0

But it should be like

A:
0   1   2   3   0   1   2   3   0   1   
2   3   0   1   2   3   0   1   2   3   
Brunaldo
  • 324
  • 3
  • 11

5 Answers5

0
System.out.println("A:");
    for (i=0; i<20; i++) {
        System.out.printf("%.1s\t", A[i]%(K+1));
        if (i==9)
          System.out.println();
    }
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
0

Your loops are wrong. You only altered the first K+1 members of the array.

Tung
  • 1
  • 3
0

The index "i" is inside an inner loop so it start from 0 for ech iteration of "m". You are overwriting the first K items of the array A

Maybe you want want write a code like this:

ind = 0;
for(m=0; m<10; m++)
{
        for (i=0; i<=K; i++)
        {
            A[indx] = i;
            indx++;
        }
}
0

The order of your numbers is alright, the problem is the Array A or better, the index when you add your numbers to that array.

for (i=0; i<=K; i++)
     A[i] = i;

here you loop through A starting at A[0] and finishing at A[K]. And you do that 10 times:

  for(m=0; m<10; m++)

So: either you use the % (modulo) operator as QBrute suggested in his comment or you find yourself a more "global" index. For example

 int j = 0;
 for(int m = 0; m < 10; m++){
    for(i = 0; i <= K; i++){
        A[j++] = i;
    }
 }

In this case you have to make sure your array is of the correct size: You want 10 times the numbers from 0 to K, so your A would be int[(K+1)*10] , while the +1 is because you loop from i=0 to i=K (which makes K+1 iterations)

GameDroids
  • 5,584
  • 6
  • 40
  • 59
  • If I may give you some advices: variables and methods should always start with a small letter (`a` not `A` and `k` not `K`). Then give those variables some useful names like `userInput` instead of `k`, so you will know what they are used for. And lastly: always use braces `{ }` in `if` statements or `loops`. If you leave out those braces, it may be convenient and faster to write, but it is an unnecessary source for errors. Believe me :) its easier that way – GameDroids Oct 27 '16 at 10:46
0

Code

    int userEnteredVal = 3;

    Integer[] smallArr = new Integer[userEnteredVal + 1];
    Integer[] outpuptArr = new Integer[20];

    for (int i = 0; i <= userEnteredVal; i++) {
        smallArr[i] = i;
    }

    for (int index = 0; index < 20; index += (userEnteredVal + 1)) {
        System.arraycopy(smallArr, 0, outpuptArr, index, smallArr.length);
    }

    for (int i = 0; i < outpuptArr.length; i++) {
        System.out.printf(outpuptArr[i] + "   ");

        if (i == 9) {
            System.out.printf("\n");
        }
    }

OutPut

0   1   2   3   0   1   2   3   0   1   
2   3   0   1   2   3   0   1   2   3   

hope this will help.

Jobin
  • 5,610
  • 5
  • 38
  • 53