Skip to main content

Draw a chart

Given collection of positive integers, Our goal is to draw a chart using of this data. Let see an example.

Simple chart

In above chart given number are not large. and number of elements are less than 10. Here given code implementation process.

// C program
// Draw a chart 
#include <stdio.h>

//Find the maximum element of given result
int find_max(int result[], int size)
{
	int max = result[0];
	for (int i = 1; i < size; ++i)
	{
		if (result[i] > max)
		{
			max = result[i];
		}
	}
	return max;
}
//Display chart of given elements
void print_chart(int runs[], int result[], int size)
{
	int status = 0;
	int i = 0;
	int max = find_max(result, size);
	if (max > 0)
	{
		printf("\n \t%d∣", max);
		for (i = 0; i < size; ++i)
		{
			printf("\t");
			if (result[i] == max)
			{
				status = 1;
				result[i]--;
				printf("▒");
			}
		}
	}
	if (status == 1)
	{
		//Recursively executing function print chart
		print_chart(runs, result, size);
	}
	else
	{
		//Display bottom layer
		printf("\n \t ∣");
		for (i = 0; i <= size; ++i)
		{
			//printf("▔▔▔▔▔▔▔");
			printf("-------");
		}
		printf("\n \t ");
		for (i = 0; i < size; ++i)
		{
			printf("\t%d", runs[i]);
		}
	}
}
void draw_chart(int runs[], int size)
{
	int result[size];
	//Copy array elements
	for (int i = 0; i < size; ++i)
	{
		//check condition here when invalid input 
		//such as negative element
		//max run in a ball etc
		//we are assume that given input valid
		result[i] = runs[i];
	}
	print_chart(runs, result, size);
}
int main()
{
	//Assume that each element are indicate  runs of a ball
	//No negative elements are possible
	//When ball is no ball then max run can be 7 and add one extra ball
	int runs[] = {
		4,
		6,
		2,
		1,
		4,
		7,
		3
	};
	int size = sizeof(runs) / sizeof(runs[0]);
	draw_chart(runs, size);
	return (0);
}

Output

 	7∣						▒
 	6∣		▒				▒
 	5∣		▒				▒
 	4∣	▒	▒			▒	▒
 	3∣	▒	▒			▒	▒	▒
 	2∣	▒	▒	▒		▒	▒	▒
 	1∣	▒	▒	▒	▒	▒	▒	▒
 	 ∣--------------------------------------------------------
 	 	4	6	2	1	4	7	3
// Java program 
// Draw a chart  
class MyPattern
{
	//Find the maximum element of given result
	public int find_max(int[] result, int size)
	{
		int max = result[0];
		for (int i = 1; i < size; ++i)
		{
			if (result[i] > max)
			{
				max = result[i];
			}
		}
		return max;
	}
	//Display chart of given elements
	public void print_chart(int[] runs, int[] result, int size)
	{
		int status = 0;
		int i = 0;
		int max = find_max(result, size);
		if (max > 0)
		{
			System.out.print("\n \t" + max + "∣");
			for (i = 0; i < size; ++i)
			{
				System.out.print("\t");
				if (result[i] == max)
				{
					status = 1;
					result[i]--;
					System.out.print("▒");
				}
			}
		}
		if (status == 1)
		{
			//Recursively executing function print chart
			print_chart(runs, result, size);
		}
		else
		{
			System.out.print("\n \t ∣");
			for (i = 0; i <= size; ++i)
			{
				System.out.print("-------");
			}
			System.out.print("\n \t ");
			for (i = 0; i < size; ++i)
			{
				System.out.print("\t" + runs[i] + "");
			}
		}
	}
	public void draw_chart(int[] runs, int size)
	{
		int[] result = new int[size];
		//Copy array elements
		for (int i = 0; i < size; ++i)
		{
			//check condition here when invalid input 
			//such as negative element
			//max run in a ball etc
			//we are assume that given input valid
			result[i] = runs[i];
		}
		print_chart(runs, result, size);
	}
	public static void main(String[] args)
	{
		MyPattern obj = new MyPattern();
		//Assume that each element are indicate  runs of a ball
		//No negative elements are possible
		//When ball is no ball then max run can be 7 and add one extra ball
		int[] runs = {
			4,
			6,
			2,
			1,
			4,
			7,
			3
		};
		int size = runs.length;
		obj.draw_chart(runs, size);
	}
}

Output

 	7∣						▒
 	6∣		▒				▒
 	5∣		▒				▒
 	4∣	▒	▒			▒	▒
 	3∣	▒	▒			▒	▒	▒
 	2∣	▒	▒	▒		▒	▒	▒
 	1∣	▒	▒	▒	▒	▒	▒	▒
 	 ∣--------------------------------------------------------
 	 	4	6	2	1	4	7	3
//Include header file
#include <iostream>

using namespace std;
// C++ program 
// Draw a chart  
class MyPattern
{
	public:
		//Find the maximum element of given result
		int find_max(int result[], int size)
		{
			int max = result[0];
			for (int i = 1; i < size; ++i)
			{
				if (result[i] > max)
				{
					max = result[i];
				}
			}
			return max;
		}
	//Display chart of given elements
	void print_chart(int runs[], int result[], int size)
	{
		int status = 0;
		int i = 0;
		int max = this->find_max(result, size);
		if (max > 0)
		{
			cout << "\n \t" << max << "∣";
			for (i = 0; i < size; ++i)
			{
				cout << "\t";
				if (result[i] == max)
				{
					status = 1;
					result[i]--;
					cout << "▒";
				}
			}
		}
		if (status == 1)
		{
			//Recursively executing function print chart
			this->print_chart(runs, result, size);
		}
		else
		{
			cout << "\n \t ∣";
			for (i = 0; i <= size; ++i)
			{
				cout << "-------";
			}
			cout << "\n \t ";
			for (i = 0; i < size; ++i)
			{
				cout << "\t" << runs[i] << "";
			}
		}
	}
	void draw_chart(int runs[], int size)
	{
		int result[size];
		//Copy array elements
		for (int i = 0; i < size; ++i)
		{
			//check condition here when invalid input 
			//such as negative element
			//max run in a ball etc
			//we are assume that given input valid
			result[i] = runs[i];
		}
		this->print_chart(runs, result, size);
	}
};
int main()
{
	MyPattern obj = MyPattern();
	int runs[] = {
		4 , 6 , 2 , 1 , 4 , 7 , 3
	};
	int size = sizeof(runs) / sizeof(runs[0]);
	obj.draw_chart(runs, size);
	return 0;
}

Output

 	7∣						▒
 	6∣		▒				▒
 	5∣		▒				▒
 	4∣	▒	▒			▒	▒
 	3∣	▒	▒			▒	▒	▒
 	2∣	▒	▒	▒		▒	▒	▒
 	1∣	▒	▒	▒	▒	▒	▒	▒
 	 ∣--------------------------------------------------------
 	 	4	6	2	1	4	7	3
//Include namespace system
using System;
// C# program 
// Draw a chart  
class MyPattern
{
	//Find the maximum element of given result
	public int find_max(int[] result, int size)
	{
		int max = result[0];
		for (int i = 1; i < size; ++i)
		{
			if (result[i] > max)
			{
				max = result[i];
			}
		}
		return max;
	}
	//Display chart of given elements
	public void print_chart(int[] runs, int[] result, int size)
	{
		int status = 0;
		int i = 0;
		int max = find_max(result, size);
		if (max > 0)
		{
			Console.Write("\n \t" + max + "∣");
			for (i = 0; i < size; ++i)
			{
				Console.Write("\t");
				if (result[i] == max)
				{
					status = 1;
					result[i]--;
					Console.Write("▒");
				}
			}
		}
		if (status == 1)
		{
			//Recursively executing function print chart
			print_chart(runs, result, size);
		}
		else
		{
			Console.Write("\n \t ∣");
			for (i = 0; i <= size; ++i)
			{
				Console.Write("-------");
			}
			Console.Write("\n \t ");
			for (i = 0; i < size; ++i)
			{
				Console.Write("\t" + runs[i] + "");
			}
		}
	}
	public void draw_chart(int[] runs, int size)
	{
		int[] result = new int[size];
		//Copy array elements
		for (int i = 0; i < size; ++i)
		{
			//check condition here when invalid input 
			//such as negative element
			//max run in a ball etc
			//we are assume that given input valid
			result[i] = runs[i];
		}
		print_chart(runs, result, size);
	}
	public static void Main(String[] args)
	{
		MyPattern obj = new MyPattern();
		int[] runs = {
			4 , 6 , 2 , 1 , 4 , 7 , 3
		};
		int size = runs.Length;
		obj.draw_chart(runs, size);
	}
}

Output

 	7∣						▒
 	6∣		▒				▒
 	5∣		▒				▒
 	4∣	▒	▒			▒	▒
 	3∣	▒	▒			▒	▒	▒
 	2∣	▒	▒	▒		▒	▒	▒
 	1∣	▒	▒	▒	▒	▒	▒	▒
 	 ∣--------------------------------------------------------
 	 	4	6	2	1	4	7	3
<?php
// Php program 
// Draw a chart  
class MyPattern
{
	//Find the maximum element of given result
	public	function find_max( & $result, $size)
	{
		$max = $result[0];
		for ($i = 1; $i < $size; ++$i)
		{
			if ($result[$i] > $max)
			{
				$max = $result[$i];
			}
		}
		return $max;
	}
	//Display chart of given elements
	public	function print_chart( & $runs, & $result, $size)
	{
		$status = 0;
		$i = 0;
		$max = $this->find_max($result, $size);
		if ($max > 0)
		{
			echo "\n \t". $max ."∣";
			for ($i = 0; $i < $size; ++$i)
			{
				echo "\t";
				if ($result[$i] == $max)
				{
					$status = 1;
					$result[$i]--;
					echo "▒";
				}
			}
		}
		if ($status == 1)
		{
			//Recursively executing function print chart
			$this->print_chart($runs, $result, $size);
		}
		else
		{
			echo "\n \t ∣";
			for ($i = 0; $i <= $size; ++$i)
			{
				echo "-------";
			}
			echo "\n \t ";
			for ($i = 0; $i < $size; ++$i)
			{
				echo "\t". $runs[$i] ."";
			}
		}
	}
	public	function draw_chart( & $runs, $size)
	{
		$result = array_fill(0, $size, 0);
		//Copy array elements
		for ($i = 0; $i < $size; ++$i)
		{
			//check condition here when invalid input 
			//such as negative element
			//max run in a ball etc
			//we are assume that given input valid
			$result[$i] = $runs[$i];
		}
		$this->print_chart($runs, $result, $size);
	}
}

function main()
{
	$obj = new MyPattern();
	//Assume that each element are indicate  runs of a ball
	//No negative elements are possible
	//When ball is no ball then max run can be 7 and add one extra ball
	$runs = array(4, 6, 2, 1, 4, 7, 3);
	$size = count($runs);
	$obj->draw_chart($runs, $size);
}
main();

Output

 	7∣						▒
 	6∣		▒				▒
 	5∣		▒				▒
 	4∣	▒	▒			▒	▒
 	3∣	▒	▒			▒	▒	▒
 	2∣	▒	▒	▒		▒	▒	▒
 	1∣	▒	▒	▒	▒	▒	▒	▒
 	 ∣--------------------------------------------------------
 	 	4	6	2	1	4	7	3
// Node Js program 
// Draw a chart  
class MyPattern
{
	//Find the maximum element of given result
	find_max(result, size)
	{
		var max = result[0];
		for (var i = 1; i < size; ++i)
		{
			if (result[i] > max)
			{
				max = result[i];
			}
		}
		return max;
	}
	//Display chart of given elements
	print_chart(runs, result, size)
	{
		var status = 0;
		var i = 0;
		var max = this.find_max(result, size);
		if (max > 0)
		{
			process.stdout.write("\n \t" + max + "∣");
			for (i = 0; i < size; ++i)
			{
				process.stdout.write("\t");
				if (result[i] == max)
				{
					status = 1;
					result[i]--;
					process.stdout.write("▒");
				}
			}
		}
		if (status == 1)
		{
			//Recursively executing function print chart
			this.print_chart(runs, result, size);
		}
		else
		{
			process.stdout.write("\n \t ∣");
			for (i = 0; i <= size; ++i)
			{
				process.stdout.write("-------");
			}
			process.stdout.write("\n \t ");
			for (i = 0; i < size; ++i)
			{
				process.stdout.write("\t" + runs[i] + "");
			}
		}
	}
	draw_chart(runs, size)
	{
		var result = Array(size).fill(0);
		//Copy array elements
		for (var i = 0; i < size; ++i)
		{
			//check condition here when invalid input 
			//such as negative element
			//max run in a ball etc
			//we are assume that given input valid
			result[i] = runs[i];
		}
		this.print_chart(runs, result, size);
	}
}

function main()
{
	var obj = new MyPattern();
	//Assume that each element are indicate  runs of a ball
	//No negative elements are possible
	//When ball is no ball then max run can be 7 and add one extra ball
	var runs = [4, 6, 2, 1, 4, 7, 3];
	var size = runs.length;
	obj.draw_chart(runs, size);
}
main();

Output

 	7∣						▒
 	6∣		▒				▒
 	5∣		▒				▒
 	4∣	▒	▒			▒	▒
 	3∣	▒	▒			▒	▒	▒
 	2∣	▒	▒	▒		▒	▒	▒
 	1∣	▒	▒	▒	▒	▒	▒	▒
 	 ∣--------------------------------------------------------
 	 	4	6	2	1	4	7	3
#  Python 3 program 
#  Draw a chart  
class MyPattern :
	# Find the maximum element of given result
	def find_max(self, result, size) :
		max = result[0]
		i = 1
		while (i < size) :
			if (result[i] > max) :
				max = result[i]
			
			i += 1
		
		return max
	
	# Display chart of given elements
	def print_chart(self, runs, result, size) :
		status = 0
		i = 0
		max = self.find_max(result, size)
		if (max > 0) :
			print("\n \t", max ,"∣", end = "")
			i = 0
			while (i < size) :
				print("\t", end = "")
				if (result[i] == max) :
					status = 1
					result[i] -= 1
					print("▒", end = "")
				
				i += 1
			
		
		if (status == 1) :
			# Recursively executing function print chart
			self.print_chart(runs, result, size)
		else :
			print("\n \t ∣", end = "")
			i = 0
			while (i <= size) :
				print("-------", end = "")
				i += 1
			
			print("\n \t ", end = "")
			i = 0
			while (i < size) :
				print("\t", runs[i] ,"", end = "")
				i += 1
			
		
	
	def draw_chart(self, runs, size) :
		result = [0] * size
		# Copy array elements
		i = 0
		while (i < size) :
			# check condition here when invalid input 
			# such as negative element
			# max run in a ball etc
			# we are assume that given input valid
			result[i] = runs[i]
			i += 1
		
		self.print_chart(runs, result, size)
	

def main() :
	obj = MyPattern()
	# Assume that each element are indicate  runs of a ball
	# No negative elements are possible
	# When ball is no ball then max run can be 7 and add one extra ball
	runs = [4, 6, 2, 1, 4, 7, 3]
	size = len(runs)
	obj.draw_chart(runs, size)

if __name__ == "__main__": main()

Output

 	 7 ∣						▒
 	 6 ∣		▒				▒
 	 5 ∣		▒				▒
 	 4 ∣	▒	▒			▒	▒
 	 3 ∣	▒	▒			▒	▒	▒
 	 2 ∣	▒	▒	▒		▒	▒	▒
 	 1 ∣	▒	▒	▒	▒	▒	▒	▒
 	 ∣--------------------------------------------------------
 	 	 4 	 6 	 2 	 1 	 4 	 7 	 3
#  Ruby program 
#  Draw a chart  
class MyPattern

	# Find the maximum element of given result
	def find_max(result, size)
	
		max = result[0]
		i = 1
		while (i < size)
		
			if (result[i] > max)
			
				max = result[i]
			end
			i += 1
		end
		return max
	end
	# Display chart of given elements
	def print_chart(runs, result, size)
	
		status = 0
		i = 0
		max = self.find_max(result, size)
		if (max > 0)
		
			print("\n \t", max ,"∣")
			i = 0
			while (i < size)
			
				print("\t")
				if (result[i] == max)
				
					status = 1
					result[i] -= 1
					print("▒")
				end
				i += 1
			end
		end
		if (status == 1)
		
			# Recursively executing function print chart
			self.print_chart(runs, result, size)
		else
		
			print("\n \t ∣")
			i = 0
			while (i <= size)
			
				print("-------")
				i += 1
			end
			print("\n \t ")
			i = 0
			while (i < size)
			
				print("\t", runs[i] ,"")
				i += 1
			end
		end
	end
	def draw_chart(runs, size)
	
		result = Array.new(size) {0}
		# Copy array elements
		i = 0
		while (i < size)
		
			# check condition here when invalid input 
			# such as negative element
			# max run in a ball etc
			# we are assume that given input valid
			result[i] = runs[i]
			i += 1
		end
		self.print_chart(runs, result, size)
	end
end
def main()

	obj = MyPattern.new()
	# Assume that each element are indicate  runs of a ball
	# No negative elements are possible
	# When ball is no ball then max run can be 7 and add one extra ball
	runs = [4, 6, 2, 1, 4, 7, 3]
	size = runs.length
	obj.draw_chart(runs, size)
end
main()

Output

 	7∣						▒	
 	6∣		▒				▒	
 	5∣		▒				▒	
 	4∣	▒	▒			▒	▒	
 	3∣	▒	▒			▒	▒	▒
 	2∣	▒	▒	▒		▒	▒	▒
 	1∣	▒	▒	▒	▒	▒	▒	▒
 	 ∣--------------------------------------------------------
 	 	4	6	2	1	4	7	3
// Scala program 
// Draw a chart  
class MyPattern
{
	//Find the maximum element of given result
	def find_max(result: Array[Int], size: Int): Int = {
		var max: Int = result(0);
		var i: Int = 1;
		while (i < size)
		{
			if (result(i) > max)
			{
				max = result(i);
			}
			i += 1;
		}
		return max;
	}
	//Display chart of given elements
	def print_chart(runs: Array[Int], result: Array[Int], size: Int): Unit = {
		var status: Int = 0;
		var i: Int = 0;
		var max: Int = find_max(result, size);
		if (max > 0)
		{
			print("\n \t" + max + "∣");
			i = 0;
			while (i < size)
			{
				print("\t");
				if (result(i) == max)
				{
					status = 1;
					result(i) -= 1;
					print("▒");
				}
				i += 1;
			}
		}
		if (status == 1)
		{
			//Recursively executing function print chart
			print_chart(runs, result, size);
		}
		else
		{
			print("\n \t ∣");
			i = 0;
			while (i <= size)
			{
				print("-------");
				i += 1;
			}
			print("\n \t ");
			i = 0;
			while (i < size)
			{
				print("\t" + runs(i) + "");
				i += 1;
			}
		}
	}
	def draw_chart(runs: Array[Int], size: Int): Unit = {
		var result: Array[Int] = Array.fill[Int](size)(0);
		//Copy array elements
		var i: Int = 0;
		while (i < size)
		{
			//check condition here when invalid input 
			//such as negative element
			//max run in a ball etc
			//we are assume that given input valid
			result(i) = runs(i);
			i += 1;
		}
		print_chart(runs, result, size);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyPattern = new MyPattern();
		//Assume that each element are indicate  runs of a ball
		//No negative elements are possible
		//When ball is no ball then max run can be 7 and add one extra ball
		var runs: Array[Int] = Array(4, 6, 2, 1, 4, 7, 3);
		var size: Int = runs.length;
		obj.draw_chart(runs, size);
	}
}

Output

 	7∣						▒
 	6∣		▒				▒
 	5∣		▒				▒
 	4∣	▒	▒			▒	▒
 	3∣	▒	▒			▒	▒	▒
 	2∣	▒	▒	▒		▒	▒	▒
 	1∣	▒	▒	▒	▒	▒	▒	▒
 	 ∣--------------------------------------------------------
 	 	4	6	2	1	4	7	3
// Swift program 
// Draw a chart  
class MyPattern
{
	//Find the maximum element of given result
	func find_max(_ result: [Int], _ size: Int) -> Int
	{
		var max: Int = result[0];
		var i: Int = 1;
		while (i < size)
		{
			if (result[i] > max)
			{
				max = result[i];
			}
			i += 1;
		}
		return max;
	}
	//Display chart of given elements
	func print_chart(_ runs: [Int], _ result: inout[Int], _ size: Int)
	{
		var status: Int = 0;
		var i: Int = 0;
		let max: Int = self.find_max(result, size);
		if (max > 0)
		{
			print("\n \t", max ,"∣", terminator: "");
			i = 0;
			while (i < size)
			{
				print("\t", terminator: "");
				if (result[i] == max)
				{
					status = 1;
					result[i] -= 1;
					print("▒", terminator: "");
				}
				i += 1;
			}
		}
		if (status == 1)
		{
			//Recursively executing function print chart
			self.print_chart(runs, &result, size);
		}
		else
		{
			print("\n \t ∣", terminator: "");
			i = 0;
			while (i <= size)
			{
				print("-------", terminator: "");
				i += 1;
			}
			print("\n \t ", terminator: "");
			i = 0;
			while (i < size)
			{
				print("\t", runs[i] ,"", terminator: "");
				i += 1;
			}
		}
	}
	func draw_chart(_ runs: [Int], _ size: Int)
	{
		var result: [Int] = Array(repeating: 0, count: size);
		//Copy array elements
		var i: Int = 0;
		while (i < size)
		{
			//check condition here when invalid input 
			//such as negative element
			//max run in a ball etc
			//we are assume that given input valid
			result[i] = runs[i];
			i += 1;
		}
		self.print_chart(runs, &result, size);
	}
}
func main()
{
	let obj: MyPattern = MyPattern();
	//Assume that each element are indicate  runs of a ball
	//No negative elements are possible
	//When ball is no ball then max run can be 7 and add one extra ball
	let runs: [Int] = [4, 6, 2, 1, 4, 7, 3];
	let size: Int = runs.count;
	obj.draw_chart(runs, size);
}
main();

Output

 	 7 ∣						▒
 	 6 ∣		▒				▒
 	 5 ∣		▒				▒
 	 4 ∣	▒	▒			▒	▒
 	 3 ∣	▒	▒			▒	▒	▒
 	 2 ∣	▒	▒	▒		▒	▒	▒
 	 1 ∣	▒	▒	▒	▒	▒	▒	▒
 	 ∣--------------------------------------------------------
 	 	 4 	 6 	 2 	 1 	 4 	 7 	 3




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