Draw a chart
In this article, we will discuss the problem of drawing a chart based on a given set of data. The task is to visualize the data in the form of a chart, where the height of each bar represents the corresponding value in the dataset. We will provide a detailed explanation of the problem, along with a suitable example, algorithm, pseudocode, and the resultant output explanation.

Problem Statement
The problem is to draw a chart based on a given set of data, where each element in the dataset represents the number of runs scored by a player. The chart should represent the frequency of runs scored by each player in a visually appealing manner. Each bar in the chart corresponds to a particular run value, and its height represents the frequency of that run value in the dataset.
For example, consider the following dataset:
[4, 6, 2, 1, 4, 7, 3]
The chart for this dataset would look as follows:
7∣ ▒ 6∣ ▒ ▒ 5∣ ▒ ▒ 4∣ ▒ ▒ ▒ ▒ 3∣ ▒ ▒ ▒ ▒ ▒ 2∣ ▒ ▒ ▒ ▒ ▒ ▒ 1∣ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ∣-------------------------------------------------------- 4 6 2 1 4 7 3
The x-axis represents the different run values, and the y-axis represents the frequency of each run value. The bars in the chart are represented by '▒' characters, and the numbers at the bottom indicate the run values. The chart provides a visual representation of the frequency distribution of the runs scored by each player.
Algorithm and Pseudocode
To solve this problem, we can follow the given algorithm:
1. Find the maximum element in the given dataset. 2. Initialize a variable 'status' to 0. 3. Iterate from the maximum element down to 1: 4. Print the current value as the y-axis label. 5. Iterate over each element in the dataset: 6. If the element is equal to the current value: 7. Set 'status' to 1. 8. Decrement the element by 1. 9. Print a '▒' character to represent the bar. 10. If 'status' is 1: 11. Recursively call the function with the updated dataset. 12. Else: 13. Print a horizontal line to represent the bottom layer. 14. Print the run values at the bottom of the chart.
The pseudocode representation of the algorithm is as follows:
function find_max(result[], size):
max = result[0]
for i = 1 to size - 1:
if result[i] > max:
max = result[i]
return max
function print_chart(runs[], result[], size):
status = 0
max = find_max(result, size)
if max > 0:
print "\n \t%d∣", max
for i = 0 to size - 1:
print "\t"
if result[i] == max:
status = 1
result[i]--
print "▒"
if status == 1:
print_chart(runs, result, size)
else:
print "\n \t ∣"
for i = 0 to size:
print "-------"
print "\n \t "
for i = 0 to size - 1:
print "\t%d", runs[i]
function draw_chart(runs[], size):
create an array result[size]
for i = 0 to size - 1:
result[i] = runs[i]
print_chart(runs, result, size)
function main():
runs = [4, 6, 2, 1, 4, 7, 3]
size = length(runs)
draw_chart(runs, size)
return 0
In above chart given number are not large. and number of elements are less than 10. Here given code implementation process.
Code Solution
// 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
Time Complexity
The time complexity of the given algorithm depends on the size of the dataset, represented by 'size'. Let 'n' be the value of 'size'.
The function 'find_max' iterates through the 'result' array once, resulting in a time complexity of O(n).
The function 'print_chart' iterates through the 'result' array once for each unique run value, which can be at most 'n'. Hence, the time complexity of 'print_chart' is O(n^2).
The function 'draw_chart' copies the 'runs' array into the 'result' array, resulting in a time complexity of O(n).
Therefore, the overall time complexity of the code is O(n^2) due to the recursive nature of the 'print_chart' function.
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