How to Generate a Specific Matrix Pattern in C Programming?

  • Thread starter Thread starter Eus
  • Start date Start date
  • Tags Tags
    Loop
Click For Summary

Discussion Overview

The discussion revolves around generating specific matrix patterns in C programming, focusing on two distinct patterns based on an input size. The first pattern involves a square of 'x' characters, while the second pattern consists of sequential numbers arranged in columns. Participants explore algorithms, rules, and potential improvements for generating these patterns.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant presents an algorithm for generating a number matrix based on an input size, questioning if there are better methods.
  • Another participant expresses confusion over the relationship between the input size and the generated patterns, suggesting a lack of defined rules.
  • Some participants propose that the first pattern is based on a 5x5 grid, with specific rules for placing 'x' characters on the boundaries and diagonals.
  • There is a suggestion that the second pattern can be generated using a nested loop with specific calculations for column and row indices.
  • Participants discuss the differences in number placement between columns, noting a consistent difference of seven in most cases.
  • Alternative methods for simplifying the calculation of row indices using modulus are proposed.

Areas of Agreement / Disagreement

Participants express differing views on the clarity and rules governing the patterns. While some agree on the basic structure of the first pattern, there is no consensus on the second pattern's generation or the underlying logic connecting the input size to the outputs.

Contextual Notes

Some participants note that the rules for generating the patterns are not explicitly defined, leading to confusion. There are also unresolved questions about how the input size directly influences the output patterns.

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 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 34 ·
2
Replies
34
Views
4K
  • · Replies 27 ·
Replies
27
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
Replies
3
Views
2K