How to resolve the algorithm Stem-and-leaf plot step by step in the C# programming language
How to resolve the algorithm Stem-and-leaf plot step by step in the C# programming language
Table of Contents
Problem Statement
Create a well-formatted stem-and-leaf plot from the following data set, where the leaves are the last digits: The primary intent of this task is the presentation of information. It is acceptable to hardcode the data set or characteristics of it (such as what the stems are) in the example, insofar as it is impractical to make the example generic to any data set. For example, in a computation-less language like HTML the data set may be entirely prearranged within the example; the interesting characteristics are how the proper visual formatting is arranged. If possible, the output should not be a bitmap image. Monospaced plain text is acceptable, but do better if you can. It may be a window, i.e. not a file.
Note: If you wish to try multiple data sets, you might try this generator.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Stem-and-leaf plot step by step in the C# programming language
The provided code is a C# program that generates a stem-and-leaf plot for a given array of integers, it takes an array of integers as input and displays a stem-and-leaf plot, which is a graphical representation of the distribution of data.
Here's a detailed explanation of the code:
-
The program starts by defining a constant string
data
that contains a space-separated list of integers. -
The string
data
is split into an array of integers using theSplit
andSelect
methods. -
The
StemAndLeafPlot
method is called to generate the stem-and-leaf plot for the array of integers. -
Inside the
StemAndLeafPlot
method:- The maximum and minimum values of the stems are calculated by dividing the maximum and minimum values of the array by 10, respectively.
- The array of integers is sorted in ascending order.
- A loop iterates through the range of possible stems, from the minimum to the maximum, inclusive.
- For each stem, the loop prints the stem followed by a pipe character and a space.
- Another loop iterates through the array of integers and prints the leaf (the last digit of the integer) for each integer that falls within the current stem's range.
- After printing the leaves for the current stem, the loop prints a newline character.
-
The program then waits for the user to press any key before exiting.
Overall, the code takes an array of integers, calculates the stems and leaves, and generates a stem-and-leaf plot, which is a useful visual representation of the data distribution.
Source code in the csharp programming language
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Program
{
static void Main()
{
const string data =
"12 127 28 42 39 113 42 18 44 118 44 37 113 124 37 48 127 36 29 31 " +
"125 139 131 115 105 132 104 123 35 113 122 42 117 119 58 109 23 " +
"105 63 27 44 105 99 41 128 121 116 125 32 61 37 127 29 113 121 58 " +
"114 126 53 114 96 25 109 7 31 141 46 13 27 43 117 116 27 7 68 40 31 " +
"115 124 42 128 52 71 118 117 38 27 106 33 117 116 111 40 119 47 " +
"105 57 122 109 124 115 43 120 43 27 27 18 28 48 125 107 114 34 " +
"133 45 120 30 127 31 116 146";
int[] ints = data.Split(' ').Select(int.Parse).ToArray();
StemAndLeafPlot(ints);
Console.ReadKey();
}
public static void StemAndLeafPlot(int[] arr)
{
int stemMax = arr.Max() / 10;
int stemMin = arr.Min() / 10;
Array.Sort(arr);
for (int i = stemMin; i <= stemMax; i++)
{
Console.Write("{0,3} | ", i);
foreach (var t in arr)
{
if (t < 10 * i)
continue;
if (t >= 10 * (i + 1))
break;
Console.Write("{0} ", t % 10);
}
Console.WriteLine("");
}
}
}
You may also check:How to resolve the algorithm Sorting algorithms/Comb sort step by step in the Python programming language
You may also check:How to resolve the algorithm Parsing/RPN calculator algorithm step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Odd word problem step by step in the Java programming language
You may also check:How to resolve the algorithm Wilson primes of order n step by step in the C++ programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Pict programming language