How to Generate a Specific Matrix Pattern in C Programming?

  • Thread starter Thread starter Eus
  • Start date Start date
  • Tags Tags
    Loop
AI Thread Summary
The discussion revolves around generating two specific patterns based on an input number, with a focus on the second pattern. The first pattern is a 5x5 grid of 'x' characters, where 'x' is placed on the boundaries and the right diagonal. The rules for this pattern are clearly defined, involving checks for the boundaries and diagonal conditions.The second pattern, which is a matrix of numbers, is less straightforward. An initial implementation is provided, but there is confusion regarding the logic behind the output and its relationship to the input number. Participants express skepticism about the lack of defined rules for generating the second pattern and question the underlying algorithm. The need for a clearer algorithm is emphasized, with suggestions to define processes rather than relying on seemingly random formatting.The conversation highlights the challenge of deriving a consistent method for producing the second pattern and invites further exploration of potential algorithms. A proposed solution for the second pattern when the input is 6 is also shared, demonstrating an attempt to clarify the output structure.
Eus
Messages
93
Reaction score
0
Hi Ho!

Given an input of 5, it is easy to produce the following pattern:
Code:
xxxxx
xx  x
x x x
x  xx
xxxxx

But, given an input of 5, how would you produce the following one?
Code:
 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:
	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");
		}
	}


Eus
 
Technology news on Phys.org
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?
 
Hi Ho!

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.

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

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?

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.

What the heck does five (other than columns) have to do with this?

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

Is there some hidden function that determines these outputs?

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

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.

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.

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

The first one will look like as:
Code:
xxxxxx
xx   x
x x  x
x  x x
x   xx
xxxxxx

The second one should look like as:
Code:
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


Eus
 
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.
 
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
 
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.
 
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.
 
or,

j = i % num_cols;

good luck
 

Similar threads

Replies
9
Views
2K
Replies
1
Views
2K
Replies
27
Views
3K
Replies
34
Views
3K
Replies
3
Views
1K
Replies
23
Views
2K
Replies
8
Views
603
Replies
19
Views
1K
Back
Top