C# exercise solutions-C# Array: stem leaf


C# array exercise: stem leaf

Exercise : Write a C# program to display an integer data set in the form of stem and leaf. The data points are input by the user from keyboard. This program will display the output similar to the one shown below:

C# program to answer about statistical information  

 

Solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Csharp_exercises
{
class Program
{
static void Main(string[] args)
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Projecttest
{
class Program
{
static void Main(string[] args)
{

  int n;
  Console.Write("Enter number of data points:");
  n = int.Parse(Console.ReadLine());
  if (n < 3)
   {
   Console.WriteLine("The number of data points should be greater than 2.");

    }
  else
  {

//declare an array of n size to store integral data points
  int[] dataset = new int[n];
//allow user inputs
   int i = 0;
   for (i = 0; i < n; i++)
  {
    Console.Write("[{0}]:", i);
    dataset[i] = int.Parse(Console.ReadLine());
   }

//sort the data set
   bubblesort(dataset, n);
//show data in stem and leaf display
//Display sorted array
  Console.WriteLine("The sorted array is:");
   for (i = 0; i < n; i++)
    {
      Console.Write("{0}\t", dataset[i]);
      if (i % 5 == 0 && i != 0) Console.Write("\n");
     }

//store stem and leaf in a 2D array
    int[,] stem_leaf = new int[n, 2];
    for (i = 0; i < n; i++)
    {
      stem_leaf[i, 0] = dataset[i] / 10;
      stem_leaf[i, 1] = dataset[i] % 10;
    }

//initialize 2D array storing numbers of occurences, and values
   int[,] mode = new int[n, 2];

   for (i = 0; i < n; i++)
     for (int j = 0; j < 2; j++) mode[i, j] = 0;

       mode[0, 0] = 1;
//find mode

   int count = 1;
   for (i = count - 1; i < n; i++)
   {
     for (int j = count - 1; j < n - 1; j++)
      {
        if (stem_leaf[i, 0] == stem_leaf[j + 1, 0]) { count++; mode[i, 0]++; mode[i, 1] = stem_leaf[i, 0]; }
       else if (i == 0) mode[i, 1] = stem_leaf[i, 0];
       }
   }
    Console.WriteLine();
    Console.WriteLine("Stem and Leaf Display:");
    Console.WriteLine("Frequency\tStem\tLeaf");
    Console.WriteLine("================================");
    int c = 0, leaf = 0;
    for (i = 0; i < n; i++)
     {
       if (mode[i, 1] != 0)
        {
          leaf += mode[i, 0];

          Console.Write("{0,-18}", mode[i, 0]);
          Console.Write("{0,-6}", mode[i, 1]);
          for (int j = c; j < leaf; j++)
            {
              Console.Write("{0}", stem_leaf[j, 1]);
             }

         c = leaf;
        Console.WriteLine();
      }
   }
 }
Console.ReadLine();
}

static void bubblesort(int[] dataset, int n)
{
  int i, j;
  for (i = 0; i < n; i++)
  for (j = n - 1; j > i; j--)
  if (dataset[j] < dataset[j - 1])
   {
     int temp = dataset[j];
     dataset[j] = dataset[j - 1];
     dataset[j - 1] = temp;
            }

      }

  }
}


}

}

}


Comments

Xinyao comment

 Xinyao

namespace Csharp_exercises
{
class Program
{
static void Main(string[] args)
{
int n;

Console.Write("Enter number of data points:");
n = int.Parse(Console.ReadLine());
int[] dataset = new int[n];

if (n < 3)
{
Console.WriteLine("The number of data points should be greater than 2.");

}
else
{

//declare an array of n size to store integral data points

//allow user inputs
int i = 0;
for (i = 0; i < n; i++)
{
Console.Write("[{0}]:", i);
dataset[i] = int.Parse(Console.ReadLine());
}

}

//sort the data set
bubblesort(dataset, n);

Console.WriteLine("The sorted array is:");

foreach (var item in dataset)
{
Console.Write(item + " ");
}


//2-column 2D array [stem, leave]

int[,] stem_leave = new int[n,2];


for (int i = 0; i < n; i++)
{
stem_leave[i,0] = dataset[i] / 10;
stem_leave[i,1] = dataset[i] % 10;
}

Console.WriteLine();

//2D array for grouping data [stem, stem count]
/* Console.WriteLine("What in the stem_leave:");
for (int i = 0; i < n; i++)
{
Console.WriteLine("{0} {1}", stem_leave[i, 0], stem_leave[i, 1]);
}

Console.ReadKey();
*/
int[,] mode = new int[n, 2];
int count = 1;
int index = 0;


//First stem
mode[0, 0] = stem_leave[0, 0];

for (int i = 1; i < n; i++)
{
if(stem_leave[i,0] == stem_leave[i-1, 0])
{
count++;
}
else
{
mode[index, 0] = stem_leave[i - 1, 0];
mode[index, 1] = count;
index++;
count = 1;
}

}
//Last stem

mode[index, 0] = stem_leave[n - 1, 0];
mode[index, 1] = count;


//what's in the mode

/*for (int i = 0; i < n; i++)
{
Console.WriteLine("{0} {1}", mode[i, 0], mode[i, 1]);
}

Console.ReadKey();

*/

//display the result

Console.WriteLine("Stem and Leaf Display: \nFrequency\tStem\tLeaf\n------------------------------------");



for (int i = 0; i <= index; i++)
{
Console.Write("{0}\t\t{1}\t", mode[i,1], mode[i,0]);
for (int j = 0; j < n; j++)
{
if (mode[i, 0] == stem_leave[j, 0])
Console.Write(stem_leave[j, 1]);
}

Console.WriteLine();
}

Console.ReadLine();

}






///bubble sort

static void bubblesort(int[] dataset, int n)
{
int i, j;
for (i = 0; i < n; i++)
for (j = n - 1; j > i; j--)
if (dataset[j] < dataset[j - 1])
{
int temp = dataset[j];
dataset[j] = dataset[j - 1];
dataset[j - 1] = temp;
}

}
}
}


2017-07-14
Xinyao comment

 Xinyao


namespace Csharp_exercises
{
class Program
{
static void Main(string[] args)
{
int n;

Console.Write("Enter number of data points:");
n = int.Parse(Console.ReadLine());
int[] dataset = new int[n];

if (n < 3)
{
Console.WriteLine("The number of data points should be greater than 2.");

}
else
{

//declare an array of n size to store integral data points

//allow user inputs
int i = 0;
for (i = 0; i < n; i++)
{
Console.Write("[{0}]:", i);
dataset[i] = int.Parse(Console.ReadLine());
}

}

//sort the data set
bubblesort(dataset, n);

Console.WriteLine("The sorted array is:");

foreach (var item in dataset)
{
Console.Write(item + " ");
}


//2-column 2D array [stem, leave]

int[,] stem_leave = new int[n,2];


for (int i = 0; i < n; i++)
{
stem_leave[i,0] = dataset[i] / 10;
stem_leave[i,1] = dataset[i] % 10;
}

Console.WriteLine();

//2D array for grouping data [stem, stem count]
/* Console.WriteLine("What in the stem_leave:");
for (int i = 0; i < n; i++)
{
Console.WriteLine("{0} {1}", stem_leave[i, 0], stem_leave[i, 1]);
}

Console.ReadKey();
*/
int[,] mode = new int[n, 2];
int count = 1;
int index = 0;


//First stem
mode[0, 0] = stem_leave[0, 0];

for (int i = 1; i < n; i++)
{
if(stem_leave[i,0] == stem_leave[i-1, 0])
{
count++;
}
else
{
mode[index, 0] = stem_leave[i - 1, 0];
mode[index, 1] = count;
index++;
count = 1;
}

}
//Last stem

mode[index, 0] = stem_leave[n - 1, 0];
mode[index, 1] = count;


//what's in the mode

/*for (int i = 0; i < n; i++)
{
Console.WriteLine("{0} {1}", mode[i, 0], mode[i, 1]);
}

Console.ReadKey();

*/

//display the result

Console.WriteLine("Stem and Leaf Display: \nFrequency\tStem\tLeaf\n------------------------------------");



for (int i = 0; i <= index; i++)
{
Console.Write("{0}\t\t{1}\t", mode[i,1], mode[i,0]);
for (int j = 0; j < n; j++)
{
if (mode[i, 0] == stem_leave[j, 0])
Console.Write(stem_leave[j, 1]);
}

Console.WriteLine();
}

Console.ReadLine();

}






///bubble sort

static void bubblesort(int[] dataset, int n)
{
int i, j;
for (i = 0; i < n; i++)
for (j = n - 1; j > i; j--)
if (dataset[j] < dataset[j - 1])
{
int temp = dataset[j];
dataset[j] = dataset[j - 1];
dataset[j - 1] = temp;
}

}
}
}


2017-07-14
Zet comment

 Zet

LINQ version:

Console.WriteLine("Stem and Leaf");
var stemAndLeaf = sortedArray.GroupBy(p => p / 10).Select(q => new { stem = q.Key,
leafs = q.Select(i=>i%10),
frequency = q.ToList().Count() });
Console.WriteLine("Frequency\tStem\tLeaf");
foreach(var i in stemAndLeaf)
{
Console.WriteLine("{0}\t\t{1}\t{2}",i.frequency,i.stem, String.Join(" ",i.leafs));
}


2017-04-13



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.