Posted on by Kalkicode
Code Pattern

Print collate shape of given size

p>The task is to write a C program that prints a collate shape of a given size. The collate shape consists of a pattern of asterisks (*) arranged in a specific way to form a symmetrical shape. The program takes an integer value as input, representing the size of the shape, and outputs the collate shape pattern.

Problem Statement

The problem is to create a collate shape pattern using asterisks (*) of a given size. The collate shape is formed by printing asterisks in a specific arrangement that results in a symmetrical pattern. The pattern should be printed using spaces and asterisks to create the desired shape. The program should handle various input sizes and print the corresponding collate shape for each size.

For example, when the size is 9, the collate shape looks like:

* * * * * * * * *
  *           *
    *       *
      *   *
        *
      *   *
    *       *
  *           *
* * * * * * * * *

Similarly, for size 7, the collate shape looks like:

* * * * * * *
  *       *
    *   *
      *
    *   *
  *       *
* * * * * * *

And for size 10, the collate shape looks like:

* * * * * * * * * *
  *             *
    *         *
      *     *
        * *
        * *
      *     *
    *         *
  *             *
* * * * * * * * * *

Algorithm and Pseudocode

To solve this problem, we can use nested loops to iterate through each row and column of the shape. The outer loop controls the rows, and the inner loop controls the columns. We need to check certain conditions to determine whether to print an asterisk or a space at each position.

The algorithm can be described as follows:

  1. Declare a function named print_collate that takes an integer size as input.
  2. Inside the function, check if the size is less than or equal to 2. If so, return from the function.
  3. Print the value of size to indicate the size of the collate shape.
  4. Use two nested loops, i for rows and j for columns, both ranging from 0 to size-1.
  5. Inside the nested loops, check the following conditions:
    • If i is equal to 0, j, i equals size-1, or size-1-i equals j, print an asterisk (*).
    • Otherwise, print a space.
  6. After each row is printed, add a newline character to move to the next row.
  7. End the function.
  8. In the main function, call the print_collate function with different test sizes to demonstrate the output.

The pseudocode representation of the algorithm:

function print_collate(size):
	if size <= 2:
		return
	print "size: ", size
	for i from 0 to size-1:
		for j from 0 to size-1:
			if i == 0 or j == i or i == size-1 or size-1-i == j:
				print "* "
			else:
				print "  "
		print newline

main():
	print_collate(9)
	print_collate(7)
	print_collate(10)

Code Solution

Here given code implementation process.

//C Program 
//Print collate shape of given size
#include <stdio.h>

//Print collate star pattern of given size
void print_collate(int size)
{
	if (size <= 2)
	{
		return;
	}
	printf("\nsize : %d  \n\n", size);
	//Loop controlling variables
	int i = 0, j = 0;
	//Loop, which is control the printing row operations
	for (i = 0; i < size; ++i)
	{
		//Loop, which is control the printing column operations
		for (j = 0; j < size; ++j)
		{
			//Condition which is printing a star element
			if (i == 0 || j == i || i == size - 1 || size - 1 - i == j)
			{
				printf("* ");
			}
			else
			{
				printf("  ");
			}
		}
		printf("\n");
	}
}
int main()
{
	/*
	Example size 9

	* * * * * * * * * 
	  *           *   
	    *       *     
	      *   *       
	        *         
	      *   *       
	    *       *     
	  *           *   
	* * * * * * * * * 


	*/
	//Simple test
	print_collate(9);
	print_collate(7);
	print_collate(10);
	return 0;
}

Output

size : 9

* * * * * * * * *
  *           *
    *       *
      *   *
        *
      *   *
    *       *
  *           *
* * * * * * * * *

size : 7

* * * * * * *
  *       *
    *   *
      *
    *   *
  *       *
* * * * * * *

size : 10

* * * * * * * * * *
  *             *
    *         *
      *     *
        * *
        * *
      *     *
    *         *
  *             *
* * * * * * * * * *
/*
Java program
Print collate shape of given size
*/
class MyPattern
{
	//Print collate star pattern of given size
	public void print_collate(int size)
	{
		if (size <= 2)
		{
			return;
		}
		System.out.print("\nsize : " + size + " \n\n");
		//Loop controlling variables
		int i = 0, j = 0;
		//Loop, which is control the printing row operations
		for (i = 0; i < size; ++i)
		{
			//Loop, which is control the printing column operations
			for (j = 0; j < size; ++j)
			{
				//Condition which is printing a star element
				if (i == 0 || j == i || i == size - 1 || size - 1 - i == j)
				{
					System.out.print("* ");
				}
				else
				{
					System.out.print("  ");
				}
			}
			System.out.print("\n");
		}
	}
	public static void main(String[] args)
	{
		MyPattern obj = new MyPattern();
		//Simple test
		obj.print_collate(9);
		obj.print_collate(7);
		obj.print_collate(10);
	}
}

Output

size : 9

* * * * * * * * *
  *           *
    *       *
      *   *
        *
      *   *
    *       *
  *           *
* * * * * * * * *

size : 7

* * * * * * *
  *       *
    *   *
      *
    *   *
  *       *
* * * * * * *

size : 10

* * * * * * * * * *
  *             *
    *         *
      *     *
        * *
        * *
      *     *
    *         *
  *             *
* * * * * * * * * *
//Include header file
#include <iostream>

using namespace std;
/*
C++ program
Print collate shape of given size
*/
class MyPattern
{
	public:
		//Print collate star pattern of given size
		void print_collate(int size)
		{
			if (size <= 2)
			{
				return;
			}
			cout << "\nsize : " << size << " \n\n";
			//Loop controlling variables
			int i = 0, j = 0;
			//Loop, which is control the printing row operations
			for (i = 0; i < size; ++i)
			{
				//Loop, which is control the printing column operations
				for (j = 0; j < size; ++j)
				{
					//Condition which is printing a star element
					if (i == 0 || j == i || i == size - 1 || size - 1 - i == j)
					{
						cout << "* ";
					}
					else
					{
						cout << "  ";
					}
				}
				cout << "\n";
			}
		}
};
int main()
{
	MyPattern obj = MyPattern();
	//Simple test
	obj.print_collate(9);
	obj.print_collate(7);
	obj.print_collate(10);
	return 0;
}

Output

size : 9

* * * * * * * * *
  *           *
    *       *
      *   *
        *
      *   *
    *       *
  *           *
* * * * * * * * *

size : 7

* * * * * * *
  *       *
    *   *
      *
    *   *
  *       *
* * * * * * *

size : 10

* * * * * * * * * *
  *             *
    *         *
      *     *
        * *
        * *
      *     *
    *         *
  *             *
* * * * * * * * * *
/*
C# program
Print collate shape of given size
*/
//Include namespace system
using System;
class MyPattern
{
	//Print collate star pattern of given size
	public void print_collate(int size)
	{
		if (size <= 2)
		{
			return;
		}
		Console.Write("\nsize : " + size + " \n\n");
		//Loop controlling variables
		int i = 0, j = 0;
		//Loop, which is control the printing row operations
		for (i = 0; i < size; ++i)
		{
			//Loop, which is control the printing column operations
			for (j = 0; j < size; ++j)
			{
				//Condition which is printing a star element
				if (i == 0 || j == i || i == size - 1 || size - 1 - i == j)
				{
					Console.Write("* ");
				}
				else
				{
					Console.Write("  ");
				}
			}
			Console.Write("\n");
		}
	}
	public static void Main(String[] args)
	{
		MyPattern obj = new MyPattern();
		//Simple test
		obj.print_collate(9);
		obj.print_collate(7);
		obj.print_collate(10);
	}
}

Output

size : 9

* * * * * * * * *
  *           *
    *       *
      *   *
        *
      *   *
    *       *
  *           *
* * * * * * * * *

size : 7

* * * * * * *
  *       *
    *   *
      *
    *   *
  *       *
* * * * * * *

size : 10

* * * * * * * * * *
  *             *
    *         *
      *     *
        * *
        * *
      *     *
    *         *
  *             *
* * * * * * * * * *
<?php
/*
Php program
Print collate shape of given size
*/
class MyPattern
{
	//Print collate star pattern of given size
	public	function print_collate($size)
	{
		if ($size <= 2)
		{
			return;
		}
		echo "\nsize : ". $size ." \n\n";
		//Loop controlling variables
		$i = 0;
		$j = 0;
		//Loop, which is control the printing row operations
		for ($i = 0; $i < $size; ++$i)
		{
			//Loop, which is control the printing column operations
			for ($j = 0; $j < $size; ++$j)
			{
				//Condition which is printing a star element
				if ($i == 0 || $j == $i || $i == $size - 1 || $size - 1 - $i == $j)
				{
					echo "* ";
				}
				else
				{
					echo "  ";
				}
			}
			echo "\n";
		}
	}
}

function main()
{
	$obj = new MyPattern();
	//Simple test
	$obj->print_collate(9);
	$obj->print_collate(7);
	$obj->print_collate(10);
}
main();

Output

size : 9

* * * * * * * * *
  *           *
    *       *
      *   *
        *
      *   *
    *       *
  *           *
* * * * * * * * *

size : 7

* * * * * * *
  *       *
    *   *
      *
    *   *
  *       *
* * * * * * *

size : 10

* * * * * * * * * *
  *             *
    *         *
      *     *
        * *
        * *
      *     *
    *         *
  *             *
* * * * * * * * * *
/*
Node Js program
Print collate shape of given size
*/
class MyPattern
{
	//Print collate star pattern of given size
	print_collate(size)
	{
		if (size <= 2)
		{
			return;
		}
		process.stdout.write("\nsize : " + size + " \n\n");
		//Loop controlling variables
		var i = 0;
		var j = 0;
		//Loop, which is control the printing row operations
		for (i = 0; i < size; ++i)
		{
			//Loop, which is control the printing column operations
			for (j = 0; j < size; ++j)
			{
				//Condition which is printing a star element
				if (i == 0 || j == i || i == size - 1 || size - 1 - i == j)
				{
					process.stdout.write("* ");
				}
				else
				{
					process.stdout.write("  ");
				}
			}
			process.stdout.write("\n");
		}
	}
}

function main()
{
	var obj = new MyPattern();
	//Simple test
	obj.print_collate(9);
	obj.print_collate(7);
	obj.print_collate(10);
}
main();

Output

size : 9

* * * * * * * * *
  *           *
    *       *
      *   *
        *
      *   *
    *       *
  *           *
* * * * * * * * *

size : 7

* * * * * * *
  *       *
    *   *
      *
    *   *
  *       *
* * * * * * *

size : 10

* * * * * * * * * *
  *             *
    *         *
      *     *
        * *
        * *
      *     *
    *         *
  *             *
* * * * * * * * * *
# Python 3 program
# Print collate shape of given size

class MyPattern :
	# Print collate star pattern of given size
	def print_collate(self, size) :
		if (size <= 2) :
			return
		
		print("\nsize : ", size ," \n\n", end = "")
		# Loop controlling variables
		i = 0
		j = 0
		# Loop, which is control the printing row operations
		while (i < size) :
			j = 0
			# Loop, which is control the printing column operations
			while (j < size) :
				# Condition which is printing a star element
				if (i == 0 or j == i or i == size - 1 or size - 1 - i == j) :
					print("* ", end = "")
				else :
					print("  ", end = "")
				
				j += 1
			
			print("\n", end = "")
			i += 1
		
	

def main() :
	obj = MyPattern()
	# Simple test
	obj.print_collate(9)
	obj.print_collate(7)
	obj.print_collate(10)

if __name__ == "__main__": main()

Output

size :  9

* * * * * * * * *
  *           *
    *       *
      *   *
        *
      *   *
    *       *
  *           *
* * * * * * * * *

size :  7

* * * * * * *
  *       *
    *   *
      *
    *   *
  *       *
* * * * * * *

size :  10

* * * * * * * * * *
  *             *
    *         *
      *     *
        * *
        * *
      *     *
    *         *
  *             *
* * * * * * * * * *
# Ruby program
# Print collate shape of given size

class MyPattern

	# Print collate star pattern of given size
	def print_collate(size)
	
		if (size <= 2)
		
			return
		end
		print("\nsize : ", size ," \n\n")
		# Loop controlling variables
		i = 0
		j = 0
		# Loop, which is control the printing row operations
		while (i < size)
		
			j = 0
			# Loop, which is control the printing column operations
			while (j < size)
			
				# Condition which is printing a star element
				if (i == 0 || j == i || i == size - 1 || size - 1 - i == j)
				
					print("* ")
				else
				
					print("  ")
				end
				j += 1
			end
			print("\n")
			i += 1
		end
	end
end
def main()

	obj = MyPattern.new()
	# Simple test
	obj.print_collate(9)
	obj.print_collate(7)
	obj.print_collate(10)
end
main()

Output

size : 9 

* * * * * * * * * 
  *           *   
    *       *     
      *   *       
        *         
      *   *       
    *       *     
  *           *   
* * * * * * * * * 

size : 7 

* * * * * * * 
  *       *   
    *   *     
      *       
    *   *     
  *       *   
* * * * * * * 

size : 10 

* * * * * * * * * * 
  *             *   
    *         *     
      *     *       
        * *         
        * *         
      *     *       
    *         *     
  *             *   
* * * * * * * * * * 
/*
Scala program
Print collate shape of given size
*/
class MyPattern
{
	//Print collate star pattern of given size
	def print_collate(size: Int): Unit = {
		if (size <= 2)
		{
			return;
		}
		print("\nsize : " + size + " \n\n");
		//Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		//Loop, which is control the printing row operations
		while (i < size)
		{
			j = 0;
			//Loop, which is control the printing column operations
			while (j < size)
			{
				//Condition which is printing a star element
				if (i == 0 || j == i || i == size - 1 || size - 1 - i == j)
				{
					print("* ");
				}
				else
				{
					print("  ");
				}
				j += 1;
			}
			print("\n");
			i += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyPattern = new MyPattern();
		//Simple test
		obj.print_collate(9);
		obj.print_collate(7);
		obj.print_collate(10);
	}
}

Output

size : 9

* * * * * * * * *
  *           *
    *       *
      *   *
        *
      *   *
    *       *
  *           *
* * * * * * * * *

size : 7

* * * * * * *
  *       *
    *   *
      *
    *   *
  *       *
* * * * * * *

size : 10

* * * * * * * * * *
  *             *
    *         *
      *     *
        * *
        * *
      *     *
    *         *
  *             *
* * * * * * * * * *
/*
Swift program
Print collate shape of given size
*/
class MyPattern
{
	//Print collate star pattern of given size
	func print_collate(_ size: Int)
	{
		if (size <= 2)
		{
			return;
		}
		print("\nsize : ", size ," \n\n", terminator: "");
		//Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		//Loop, which is control the printing row operations
		while (i < size)
		{
			j = 0;
			//Loop, which is control the printing column operations
			while (j < size)
			{
				//Condition which is printing a star element
				if (i == 0 || j == i || i == size - 1 || size - 1 - i == j)
				{
					print("* ", terminator: "");
				}
				else
				{
					print("  ", terminator: "");
				}
				j += 1;
			}
			print("\n", terminator: "");
			i += 1;
		}
	}
}
func main()
{
	let obj: MyPattern = MyPattern();
	//Simple test
	obj.print_collate(9);
	obj.print_collate(7);
	obj.print_collate(10);
}
main();

Output

size :  9

* * * * * * * * *
  *           *
    *       *
      *   *
        *
      *   *
    *       *
  *           *
* * * * * * * * *

size :  7

* * * * * * *
  *       *
    *   *
      *
    *   *
  *       *
* * * * * * *

size :  10

* * * * * * * * * *
  *             *
    *         *
      *     *
        * *
        * *
      *     *
    *         *
  *             *
* * * * * * * * * *

Time Complexity

The time complexity of the algorithm is O(n^2), where n represents the size of the collate shape. This is because we use two nested loops that iterate from 0 to size-1, resulting in a quadratic time complexity. As the size increases, the number of iterations increases quadratically.

Therefore, the program efficiently generates the collate shape pattern for a given size using nested loops and conditional statements, providing a visually appealing output.

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment