Print double headed arrow pattern
In this article, we will discuss how to print a double-headed arrow pattern using C programming language. We will explain the problem statement, provide a suitable example, discuss the algorithm and pseudocode, and explain the output with the time complexity of the code.
Problem Statement
The problem is to print a double-headed arrow pattern of a given size. The pattern consists of two arrowheads facing opposite directions, connected by a vertical line of stars. The size of the pattern determines its height and width.
Example:
Let's consider an example to understand the pattern better. For a size of 4, the pattern would look like this:
* ** ** *** *** **** **** *** *** ** ** *
The pattern starts with a single star at the top, followed by the arrowheads on each side. The arrowheads decrease in size as they move towards the middle. The vertical line of stars connects the two arrowheads.
Algorithm and Pseudocode
To print the double-headed arrow pattern, we can follow the following algorithm:
- Define three functions:
space()
,print_star()
, anddouble_arrow()
. - The
space()
function takes an integersize
as input and prints the required number of spaces. - The
print_star()
function takes an integersize
as input and prints the required number of stars. - The
double_arrow()
function takes an integersize
as input and prints the double-headed arrow pattern. - In the
double_arrow()
function, iterate from 0 tosize
to print the upper half layers of the pattern. - Inside the loop, use the
space()
function to print the initial spaces, followed by theprint_star()
function to print the stars. - If the index
i
is greater than or equal to 2, include additional spaces and stars to create the arrowhead shape. - Print a new line after each layer is printed.
- After printing the upper half layers, iterate from 0 to
size
again to print the bottom half layers of the pattern. - Inside this loop, use the
space()
andprint_star()
functions to print the spaces and stars in a reverse order compared to the upper half layers. - Print a new line after each layer is printed.
- Call the
double_arrow()
function with different sizes to test the pattern.
The pseudocode for the above algorithm is as follows:
Function space(size):
for counter from 0 to size:
print space
Function print_star(size):
for counter from 0 to size:
print star
Function double_arrow(size):
print "Size : size"
for i from 0 to size:
space((size * 2) - (i * 2))
print_star(i)
if i >= 2:
space((i - 1) * 2 - 1)
print_star(i)
print new line
for i from 0 to size:
space(i * 2)
print_star(size - i)
if i + 1 != size:
space(((size - 1) * 2) - (i * 2) - 1)
print_star(size - i)
print new line
Main function:
double_arrow(3)
double_arrow(4)
double_arrow(7)
double_arrow(9)
Output Explanation
The output consists of the printed double-headed arrow patterns for different sizes. Each pattern is preceded by a line indicating the size of the pattern being printed.
Let's explain the output for the size of 4:
* ** ** *** *** **** **** *** *** ** ** *
The pattern starts with a single star at the top. The first layer of the arrowhead contains two stars on each side, separated by spaces. The second layer has four stars, and the third layer has six stars. The vertical line of stars connects the arrowheads, and the pattern is symmetric. The bottom half layers mirror the upper half layers in reverse order.
Time Complexity
The time complexity of the given code is O(n^2), where n is the size of the pattern. This is because the code contains nested loops that iterate up to the size of the pattern. As the size increases, the number of iterations and the time taken will increase quadratically.
Code Solution
Here given code implementation process.
//C Program
//Print double headed arrow pattern
#include <stdio.h>
#include <stdlib.h>
/*Include Space of given size*/
void space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
//Add space
printf(" ");
}
}
/*Include star of given size*/
void print_star(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
//Add star
printf("*");
}
}
//Display the double arrow of given size
void double_arrow(int size)
{
printf("\n Size : %d \n", size);
int i = 0;
//print upper half layers
for (i = 0; i < size; i++)
{
//include initial space
space((size *2) - (i *2));
//print star
print_star(i);
if (i >= 2)
{
space((i - 1) *2 - 1);
print_star(i);
}
printf("\n");
}
//print bottom half layers
for (i = 0; i < size; i++)
{
//include initial space
space(i *2);
//print star
print_star(size - i);
if (i + 1 != size)
{
space(((size - 1) *2) - (i *2) - 1);
print_star(size - i);
}
printf("\n");
}
}
int main()
{
//Test Cases
double_arrow(3);
double_arrow(4);
double_arrow(7);
double_arrow(9);
return 0;
}
Output
Size : 3
*
** **
*** ***
** **
*
Size : 4
*
** **
*** ***
**** ****
*** ***
** **
*
Size : 7
*
** **
*** ***
**** ****
***** *****
****** ******
******* *******
****** ******
***** *****
**** ****
*** ***
** **
*
Size : 9
*
** **
*** ***
**** ****
***** *****
****** ******
******* *******
******** ********
********* *********
******** ********
******* *******
****** ******
***** *****
**** ****
*** ***
** **
*
/*
Java Program
Print double headed arrow pattern
*/
class MyPattern
{
public void space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
//Add space
System.out.print(" ");
}
}
public void print_star(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
//Add star
System.out.print("*");
}
}
//Display the double arrow of given size
public void double_arrow(int size)
{
System.out.print("\n Size : "+size+" \n");
int i = 0;
//print upper half layers
for (i = 0; i < size; i++)
{
//include initial space
space((size *2) - (i *2));
//print star
print_star(i);
if (i >= 2)
{
space((i - 1) *2 - 1);
print_star(i);
}
System.out.print("\n");
}
//print bottom half layers
for (i = 0; i < size; i++)
{
//include initial space
space(i *2);
//print star
print_star(size - i);
if (i + 1 != size)
{
space(((size - 1) *2) - (i *2) - 1);
print_star(size - i);
}
System.out.print("\n");
}
}
public static void main(String[] args)
{
MyPattern obj = new MyPattern();
//Test evaluation
obj.double_arrow(3);
obj.double_arrow(4);
obj.double_arrow(7);
obj.double_arrow(9);
}
}
Output
Size : 3
*
** **
*** ***
** **
*
Size : 4
*
** **
*** ***
**** ****
*** ***
** **
*
Size : 7
*
** **
*** ***
**** ****
***** *****
****** ******
******* *******
****** ******
***** *****
**** ****
*** ***
** **
*
Size : 9
*
** **
*** ***
**** ****
***** *****
****** ******
******* *******
******** ********
********* *********
******** ********
******* *******
****** ******
***** *****
**** ****
*** ***
** **
*
/*
C++ Program
Print double headed arrow pattern
*/
#include<iostream>
using namespace std;
class MyPattern
{
public: void space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
cout << " ";
}
}
void print_star(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
cout << "*";
}
}
//Display the double arrow of given size
void double_arrow(int size)
{
cout << "\n Size : " << size << " \n";
int i = 0;
//print upper half layers
for (i = 0; i < size; i++)
{
//include initial space
this->space((size * 2) - (i * 2));
//print star
this->print_star(i);
if (i >= 2)
{
this->space((i - 1) * 2 - 1);
this->print_star(i);
}
cout << "\n";
}
//print bottom half layers
for (i = 0; i < size; i++)
{
//include initial space
this->space(i * 2);
//print star
this->print_star(size - i);
if (i + 1 != size)
{
this->space(((size - 1) * 2) - (i * 2) - 1);
this->print_star(size - i);
}
cout << "\n";
}
}
};
int main()
{
MyPattern obj = MyPattern();
//Test evaluation
obj.double_arrow(3);
obj.double_arrow(4);
obj.double_arrow(7);
obj.double_arrow(9);
return 0;
}
Output
Size : 3
*
** **
*** ***
** **
*
Size : 4
*
** **
*** ***
**** ****
*** ***
** **
*
Size : 7
*
** **
*** ***
**** ****
***** *****
****** ******
******* *******
****** ******
***** *****
**** ****
*** ***
** **
*
Size : 9
*
** **
*** ***
**** ****
***** *****
****** ******
******* *******
******** ********
********* *********
******** ********
******* *******
****** ******
***** *****
**** ****
*** ***
** **
*
/*
C# Program
Print double headed arrow pattern
*/
using System;
class MyPattern
{
public void space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
//Add space
Console.Write(" ");
}
}
public void print_star(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
//Add star
Console.Write("*");
}
}
//Display the double arrow of given size
public void double_arrow(int size)
{
Console.Write("\n Size : " + size + " \n");
int i = 0;
//print upper half layers
for (i = 0; i < size; i++)
{
//include initial space
space((size * 2) - (i * 2));
//print star
print_star(i);
if (i >= 2)
{
space((i - 1) * 2 - 1);
print_star(i);
}
Console.Write("\n");
}
//print bottom half layers
for (i = 0; i < size; i++)
{
//include initial space
space(i * 2);
//print star
print_star(size - i);
if (i + 1 != size)
{
space(((size - 1) * 2) - (i * 2) - 1);
print_star(size - i);
}
Console.Write("\n");
}
}
public static void Main(String[] args)
{
MyPattern obj = new MyPattern();
//Test evaluation
obj.double_arrow(3);
obj.double_arrow(4);
obj.double_arrow(7);
obj.double_arrow(9);
}
}
Output
Size : 3
*
** **
*** ***
** **
*
Size : 4
*
** **
*** ***
**** ****
*** ***
** **
*
Size : 7
*
** **
*** ***
**** ****
***** *****
****** ******
******* *******
****** ******
***** *****
**** ****
*** ***
** **
*
Size : 9
*
** **
*** ***
**** ****
***** *****
****** ******
******* *******
******** ********
********* *********
******** ********
******* *******
****** ******
***** *****
**** ****
*** ***
** **
*
<?php
/*
Php Program
Print double headed arrow pattern
*/
class MyPattern
{
public function space($size)
{
$counter = 0;
for ($counter = 0; $counter < $size; $counter++)
{
//Add space
echo(" ");
}
}
public function print_star($size)
{
$counter = 0;
for ($counter = 0; $counter < $size; $counter++)
{
//Add star
echo("*");
}
}
//Display the double arrow of given size
public function double_arrow($size)
{
echo("\n Size : ". $size ." \n");
$i = 0;
//print upper half layers
for ($i = 0; $i < $size; $i++)
{
//include initial space
$this->space(($size * 2) - ($i * 2));
//print star
$this->print_star($i);
if ($i >= 2)
{
$this->space(($i - 1) * 2 - 1);
$this->print_star($i);
}
echo("\n");
}
//print bottom half layers
for ($i = 0; $i < $size; $i++)
{
//include initial space
$this->space($i * 2);
//print star
$this->print_star($size - $i);
if ($i + 1 != $size)
{
$this->space((($size - 1) * 2) - ($i * 2) - 1);
$this->print_star($size - $i);
}
echo("\n");
}
}
}
function main()
{
$obj = new MyPattern();
//Test evaluation
$obj->double_arrow(3);
$obj->double_arrow(4);
$obj->double_arrow(7);
$obj->double_arrow(9);
}
main();
Output
Size : 3
*
** **
*** ***
** **
*
Size : 4
*
** **
*** ***
**** ****
*** ***
** **
*
Size : 7
*
** **
*** ***
**** ****
***** *****
****** ******
******* *******
****** ******
***** *****
**** ****
*** ***
** **
*
Size : 9
*
** **
*** ***
**** ****
***** *****
****** ******
******* *******
******** ********
********* *********
******** ********
******* *******
****** ******
***** *****
**** ****
*** ***
** **
*
/*
Node Js Program
Print double headed arrow pattern
*/
class MyPattern
{
space(size)
{
var counter = 0;
for (counter = 0; counter < size; counter++)
{
//Add space
process.stdout.write(" ");
}
}
print_star(size)
{
var counter = 0;
for (counter = 0; counter < size; counter++)
{
//Add star
process.stdout.write("*");
}
}
//Display the double arrow of given size
double_arrow(size)
{
process.stdout.write("\n Size : " + size + " \n");
var i = 0;
//print upper half layers
for (i = 0; i < size; i++)
{
//include initial space
this.space((size * 2) - (i * 2));
//print star
this.print_star(i);
if (i >= 2)
{
this.space((i - 1) * 2 - 1);
this.print_star(i);
}
process.stdout.write("\n");
}
//print bottom half layers
for (i = 0; i < size; i++)
{
//include initial space
this.space(i * 2);
//print star
this.print_star(size - i);
if (i + 1 != size)
{
this.space(((size - 1) * 2) - (i * 2) - 1);
this.print_star(size - i);
}
process.stdout.write("\n");
}
}
}
function main(args)
{
var obj = new MyPattern();
//Test evaluation
obj.double_arrow(3);
obj.double_arrow(4);
obj.double_arrow(7);
obj.double_arrow(9);
}
main();
Output
Size : 3
*
** **
*** ***
** **
*
Size : 4
*
** **
*** ***
**** ****
*** ***
** **
*
Size : 7
*
** **
*** ***
**** ****
***** *****
****** ******
******* *******
****** ******
***** *****
**** ****
*** ***
** **
*
Size : 9
*
** **
*** ***
**** ****
***** *****
****** ******
******* *******
******** ********
********* *********
******** ********
******* *******
****** ******
***** *****
**** ****
*** ***
** **
*
# Python 3 Program
# Print double headed arrow pattern
class MyPattern :
def space(self, size) :
counter = 0
while (counter < size) :
print(" ", end = "")
counter += 1
def print_star(self, size) :
counter = 0
while (counter < size) :
print("*", end = "")
counter += 1
# Display the double arrow of given size
def double_arrow(self, size) :
print("\n Size : ", size )
i = 0
# print upper half layers
while (i < size) :
# include initial space
self.space((size * 2) - (i * 2))
# print star
self.print_star(i)
if (i >= 2) :
self.space((i - 1) * 2 - 1)
self.print_star(i)
print(end = "\n")
i += 1
# print bottom half layers
i = 0
while (i < size) :
# include initial space
self.space(i * 2)
# print star
self.print_star(size - i)
if (i + 1 != size) :
self.space(((size - 1) * 2) - (i * 2) - 1)
self.print_star(size - i)
print(end = "\n")
i += 1
def main() :
obj = MyPattern()
# Test evaluation
obj.double_arrow(3)
obj.double_arrow(4)
obj.double_arrow(7)
obj.double_arrow(9)
if __name__ == "__main__": main()
Output
Size : 3
*
** **
*** ***
** **
*
Size : 4
*
** **
*** ***
**** ****
*** ***
** **
*
Size : 7
*
** **
*** ***
**** ****
***** *****
****** ******
******* *******
****** ******
***** *****
**** ****
*** ***
** **
*
Size : 9
*
** **
*** ***
**** ****
***** *****
****** ******
******* *******
******** ********
********* *********
******** ********
******* *******
****** ******
***** *****
**** ****
*** ***
** **
*
# Ruby Program
# Print double headed arrow pattern
class MyPattern
def space(size)
counter = 0
while (counter < size)
print(" ")
counter += 1
end
end
def print_star(size)
counter = 0
while (counter < size)
print("*")
counter += 1
end
end
# Display the double arrow of given size
def double_arrow(size)
print("\n Size : ", size ," \n")
i = 0
# print upper half layers
while (i < size)
# include initial space
self.space((size * 2) - (i * 2))
# print star
self.print_star(i)
if (i >= 2)
self.space((i - 1) * 2 - 1)
self.print_star(i)
end
print("\n")
i += 1
end
# print bottom half layers
i = 0
while (i < size)
# include initial space
self.space(i * 2)
# print star
self.print_star(size - i)
if (i + 1 != size)
self.space(((size - 1) * 2) - (i * 2) - 1)
self.print_star(size - i)
end
print("\n")
i += 1
end
end
end
def main()
obj = MyPattern.new()
# Test evaluation
obj.double_arrow(3)
obj.double_arrow(4)
obj.double_arrow(7)
obj.double_arrow(9)
end
main()
Output
Size : 3
*
** **
*** ***
** **
*
Size : 4
*
** **
*** ***
**** ****
*** ***
** **
*
Size : 7
*
** **
*** ***
**** ****
***** *****
****** ******
******* *******
****** ******
***** *****
**** ****
*** ***
** **
*
Size : 9
*
** **
*** ***
**** ****
***** *****
****** ******
******* *******
******** ********
********* *********
******** ********
******* *******
****** ******
***** *****
**** ****
*** ***
** **
*
/*
Scala Program
Print double headed arrow pattern
*/
class MyPattern
{
def space(size: Int): Unit = {
var counter: Int = 0;
while (counter < size)
{
print(" ");
counter += 1;
}
}
def print_star(size: Int): Unit = {
var counter: Int = 0;
while (counter < size)
{
print("*");
counter += 1;
}
}
//Display the double arrow of given size
def double_arrow(size: Int): Unit = {
print("\n Size : " + size + " \n");
var i: Int = 0;
//print upper half layers
while (i < size)
{
//include initial space
space((size * 2) - (i * 2));
//print star
print_star(i);
if (i >= 2)
{
space((i - 1) * 2 - 1);
print_star(i);
}
print("\n");
i += 1;
}
//print bottom half layers
i = 0;
while (i < size)
{
//include initial space
space(i * 2);
//print star
print_star(size - i);
if (i + 1 != size)
{
space(((size - 1) * 2) - (i * 2) - 1);
print_star(size - i);
}
print("\n");
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyPattern = new MyPattern();
//Test evaluation
obj.double_arrow(3);
obj.double_arrow(4);
obj.double_arrow(7);
obj.double_arrow(9);
}
}
Output
Size : 3
*
** **
*** ***
** **
*
Size : 4
*
** **
*** ***
**** ****
*** ***
** **
*
Size : 7
*
** **
*** ***
**** ****
***** *****
****** ******
******* *******
****** ******
***** *****
**** ****
*** ***
** **
*
Size : 9
*
** **
*** ***
**** ****
***** *****
****** ******
******* *******
******** ********
********* *********
******** ********
******* *******
****** ******
***** *****
**** ****
*** ***
** **
*
/*
Swift Program
Print double headed arrow pattern
*/
class MyPattern
{
func space(_ size: Int)
{
var counter: Int = 0;
while (counter < size)
{
print(" ", terminator: "");
counter += 1;
}
}
func print_star(_ size: Int)
{
var counter: Int = 0;
while (counter < size)
{
print("*", terminator: "");
counter += 1;
}
}
//Display the double arrow of given size
func double_arrow(_ size: Int)
{
print("\n Size : ", size ," \n", terminator: "");
var i: Int = 0;
//print upper half layers
while (i < size)
{
//include initial space
self.space((size * 2) - (i * 2));
//print star
self.print_star(i);
if (i >= 2)
{
self.space((i - 1) * 2 - 1);
self.print_star(i);
}
print("\n", terminator: "");
i += 1;
}
//print bottom half layers
i = 0;
while (i < size)
{
//include initial space
self.space(i * 2);
//print star
self.print_star(size - i);
if (i + 1 != size)
{
self.space(((size - 1) * 2) - (i * 2) - 1);
self.print_star(size - i);
}
print("\n", terminator: "");
i += 1;
}
}
}
func main()
{
let obj: MyPattern = MyPattern();
//Test evaluation
obj.double_arrow(3);
obj.double_arrow(4);
obj.double_arrow(7);
obj.double_arrow(9);
}
main();
Output
Size : 3
*
** **
*** ***
** **
*
Size : 4
*
** **
*** ***
**** ****
*** ***
** **
*
Size : 7
*
** **
*** ***
**** ****
***** *****
****** ******
******* *******
****** ******
***** *****
**** ****
*** ***
** **
*
Size : 9
*
** **
*** ***
**** ****
***** *****
****** ******
******* *******
******** ********
********* *********
******** ********
******* *******
****** ******
***** *****
**** ****
*** ***
** **
*
The pattern starts with a single star at the top. The first layer of the arrowhead contains two stars on each side, separated by spaces. The second layer has four stars, and the third layer has six stars. The vertical line of stars connects the arrowheads, and the pattern is symmetric. The bottom half layers mirror the upper half layers in reverse order.
Time Complexity
The time complexity of the given code is O(n^2), where n is the size of the pattern. This is because the code contains nested loops that iterate up to the size of the pattern. As the size increases, the number of iterations and the time taken will increase quadratically.
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