How to Generate a Specific Matrix Pattern in C Programming?

  • Thread starter Eus
  • Start date
  • Tags
    Loop
In summary: This conversation is about producing a specific pattern given an input number. It is not about printing columns or rows. Please provide a summary of the conversation about the pattern, starting with "In summary, ".In summary, the conversation is about producing a specific pattern given an input number. The first pattern is a 5x5 square block with an "x" in the boundary and on the right diagonal. The second pattern is a folded number line that is missing certain numbers, which are printed in columns. The discussion includes determining the rules for producing these patterns and finding a better algorithm for the second pattern. Suggestions include using a nested loop and calculating with modulus.
  • #1
Eus
94
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
  • #2
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
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
 
  • #4
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
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
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
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
or,

j = i % num_cols;

good luck
 

1. What is an exercising loop pattern?

An exercising loop pattern is a type of programming loop that repeats a set of instructions until a specific condition is met. It is commonly used in software development to automate repetitive tasks or to process large amounts of data.

2. How is an exercising loop pattern different from other types of loops?

Unlike other types of loops, an exercising loop pattern has a specific condition that determines when the loop will end. This condition is usually based on a counter or a boolean expression, rather than a set number of iterations.

3. What are some common mistakes to avoid when using an exercising loop pattern?

One common mistake is forgetting to update the counter or condition that controls the loop, which can result in an infinite loop. It is also important to ensure that the loop's instructions are properly indented and that all necessary variables are declared.

4. Can an exercising loop pattern be nested within another loop?

Yes, an exercising loop pattern can be nested within another loop. This can be useful for more complex tasks that require multiple levels of repetition.

5. What are the advantages of using an exercising loop pattern?

An exercising loop pattern allows for efficient and automated processing of large amounts of data. It also helps to reduce the amount of code needed, making programs more concise and easier to maintain. Additionally, the specific end condition of an exercising loop pattern can make it more flexible and adaptable to different situations.

Similar threads

  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
27
Views
2K
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
679
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
19
Views
972
Replies
3
Views
478
  • Nuclear Engineering
Replies
4
Views
2K
Back
Top