# Exercising Loop: Pattern

1. Feb 20, 2008

### Eus

Hi Ho!

Given an input of 5, it is easy to produce the following pattern:
Code (Text):

xxxxx
xx  x
x x x
x  xx
xxxxx

But, given an input of 5, how would you produce the following one?
Code (Text):

1  7 14 21
2  8 15 22
3  9 16 23
4 10 17 24
5 11 18 25

I have implemented the solution below, but I wonder if there is anyone here that can suggest a better algorithm.

Code (Text):

if (input > 2)
{
/*
> input  = 5
> output =

1 7
2 8
3 9

3 * (3 - 1) / (3 - 2)

1  7 13
2  8 14
3  9 15
4 10 16

4 * (4 - 1) / (4 - 2) = 6
floor (6) = 6
ceil (6) = 6
.0 -> 6

> 1 7  14 21
> 2 8  15 22
> 3 9  16 23
> 4 10 17 24
> 5 11 18 25

5 * (5 - 1) / (5 - 2) = 6.666...
floor (6.66...) = 6
ceil (6.66...) = 7
.666... > .5 -> 6 7 7

1  8 15 23 31
2  9 16 24 32
3 10 17 25 33
4 11 18 26 34
5 12 19 27 35
6 13 20 28 36

6 * (6 - 1) / (6 - 2) = 7.5
floor (7.5) = 7
ceil (7.5) = 8
.5 == .5 -> 7 7 8 8

1  9 17 25 34 43
2 10 18 26 35 44
3 11 19 27 36 45
4 12 20 28 37 46
5 13 21 29 38 47
6 14 22 30 39 48
7 15 23 31 40 49

7 * (7 - 1) / (7 - 2) = 8.4
floor (8.4) = 8
ceil (8.4) = 9
.4 < .5 -> 8 8 8 9 9

n * (n - 1) / (n - 2) = ?
*/

int i = 0;
int j = 0;
int k = input - 2;

int tmp = 0;
int num_of_digits = log10 (input * input) + 2;

int distance = input * input - input;
double step_size = (double) input * (input - 1) / (input - 2);
int floor_val = floor (step_size);
int ceil_val = ceil (step_size);
int mid_point = 0;

while ((distance - mid_point * floor_val) % ceil_val != 0)
{
++mid_point;
}

for (i = 1; i <= input; i++)
{
printf ("%*d", num_of_digits, i);
for (j = 1, tmp = i; j <= k; j++)
{
if (j <= mid_point)
{
tmp += floor_val;
}
else
{
tmp += ceil_val;
}
printf ("%*d", num_of_digits, tmp);
}
printf ("\n");
}
}

Best regards,
Eus

2. Feb 20, 2008

### jim mcnamara

I do not get what you are doing at all. I fail to see why "5" produces any of the above outputs - because there is no defined set of rules.

In the first block, row 2 is inverted to make row 4. row 3 is missing even numbered 'X' positions. How is that related to 5?

In the second block, you simply have a folded number line, a line that is missing
6, 12, 13, 19, 20 that is printed in columns:
12345 78911 11111 22222
01 45678 12345

What the heck does five (other than columns) have to do with this? Is there some hidden function that determines these outputs?

When you write programs, you shold try to define a process or algorithm, rather than simply trying output a stream with apparently random rules for formatting and omitting elements.

For example - I give you "6" - what does block 1 look like now? what does block 2 look like?

3. Feb 21, 2008

### Eus

Hi Ho!

Well, we are to define the rules based on the patterns we see.

It is not said in the problem. But, based on my observation, "5" means that the pattern will be printed on a 5x5 square block of characters.

We are to determine what number five has to do with the patterns produced.

Yes, the functions that produces the patterns are to be determined.

I have defined and given the algorithm for the second pattern in the first post.
If you observe the patterns closely, the rules for the first one is very obvious, whereas the second one is not so obvious.
I just wonder if someone can recognize a better pattern for the second one.

The first one will look like as:
Code (Text):

xxxxxx
xx   x
x x  x
x  x x
x   xx
xxxxxx

The second one should look like as:
Code (Text):

1  8 15 23 31
2  9 16 24 32
3 10 17 25 33
4 11 18 26 34
5 12 19 27 35
6 13 20 28 36

Best regards,
Eus

4. Feb 21, 2008

### ron_jay

The first one is quite simple:

Given the size of the array as 5, You have to put the "x" in the boundary of the square and on the right diagonal.

condition for printing right diagonal:

if(r==c){print "x"}

condition for printing boundary:

if(r-1==-1||r==n-1||c==0||c-1==-1){print "x"}

that's it.

5. Feb 21, 2008

### Flamingo

The first column behaves slightly differently from the other columns.

With a nested loop, it should be i*7+j UNLESS i==0, then it's i*7+j+1

i is the column number starting at 0
j is the row number starting at 0

6. Feb 21, 2008

### Flamingo

the difference between the numbers in the columns are seven, except for the difference between the 0th, and the 1st column

you simply input how many columns there are going to be.

7. Feb 21, 2008

### Flamingo

if you wish to simplify from the nested loop, you can calculate j with modulus

j = i % 5;

and keep incramenting i to the end.

8. Feb 21, 2008

### Flamingo

or,

j = i % num_cols;

good luck