Everyone Is An Achiever

Tuesday, December 30, 2014

program to calculate stems and leafs

1:34 PM Posted by Unknown No comments


After you got an idea about stems and leafs lets program it. I'm gonna do something different in this tutorial. I'll post the entire code at once, then I'll explain it line by line.

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

namespace TestConsole
{

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

            Console.WriteLine("Enter the number of stems:");
            int stems = int.Parse(Console.ReadLine());
            string[][] stemsAndLeafs = new string[stems][];

            for (int i = 0; i < stemsAndLeafs.Length; i++)
                stemsAndLeafs[i] = new string[2];

            for (int i = 0; i < stems; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    if (j == 0)
                        Console.WriteLine("Enter stem {0} value", i + 1);
                    if (j == 1)
                        Console.WriteLine("Enter leafs of stem {0} seperated by space", i + 1);
                    stemsAndLeafs[i][j] = Console.ReadLine();
                }
            }

            Console.WriteLine("-----------------------");
            Console.WriteLine("Stems\tLeafs");
            for (int i = 0; i < stems; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    if (j == 0)
                        Console.Write(stemsAndLeafs[i][j] + "\t");
                    else
                        Console.Write(stemsAndLeafs[i][j]);
                }
                Console.WriteLine();
            }
            Console.WriteLine("-----------------------");

            int[] sumOneRowValues = new int[stems];
            ConvertToIntegers con = new ConvertToIntegers();
           
            for (int i = 0; i < stems; i++)
            {

                string[] stringNumbers = stemsAndLeafs[i][1].Split(' ');
                byte[] leafs = con.doConvert(stringNumbers);
                sumOneRowValues[i] = doSum(stemsAndLeafs[i][0], leafs);

            }

            Console.WriteLine("Sum of all stems\n------------------");
            for (int i = 0; i < stems; i++)
                Console.WriteLine("sum of stem {0} = {1}", stemsAndLeafs[i][0], sumOneRowValues[i]);

            Console.ReadLine();
        }

        private static int doSum(string stem, byte[] leafs)
        {
            int sum = 0;

            int stemValue = (Convert.ToChar(stem) - '0') * 10;

            for (int i = 0; i < leafs.Length; i++)
                sum = sum + stemValue + leafs[i];
            return sum;
        }
    }
}

Lets start analyzing the code: First lets recall the example we used in the theory tutorial:
stem
leafs
1
2 3 5 6 7
2
1 1 0
3
0
4
9 8 2

• As you can see, the table has 1 column for stems and the second for leafs. Therefore, we will have a matrix of n * 2. The variable n is the number of stems are there, so we start the program by asking the user to enter the number of stems, then we instantiate a matrix of (stems * 2). But what data type do we have to use? Notice that the leafs part consist of numbers and white-spaces, hence we will create a string matrix of (stems * 2) order.

Console.WriteLine("Enter the number of stems:");
            int stems = int.Parse(Console.ReadLine());
            string[][] stemsAndLeafs = new string[stems][];

            for (int i = 0; i < stemsAndLeafs.Length; i++)
                stemsAndLeafs[i] = new string[2];

• Then we ask the user to enter the stems and leafs. The leafs should be separated by white-space

for (int i = 0; i < stems; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    if (j == 0)
                        Console.WriteLine("Enter stem {0} value", i + 1);
                    if (j == 1)
                        Console.WriteLine("Enter leafs of stem {0} seperated by space", i + 1);
                    stemsAndLeafs[i][j] = Console.ReadLine();
                }
            }
I used a conditions here to just print the proper message to the user. When (j == 0) it means that we are at the beginning of new row first column so in that case the value will be the stem's and when (j == 1) it means we are in the leafs column.
 • Then we print the stems and leafs

Console.WriteLine("-----------------------");
            Console.WriteLine("Stems\tLeafs");
            for (int i = 0; i < stems; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    if (j == 0)
                        Console.Write(stemsAndLeafs[i][j] + "\t");
                    else
                        Console.Write(stemsAndLeafs[i][j]);
                }
                Console.WriteLine();
            }
            Console.WriteLine("-----------------------");

Up to here you're just fine. If you need to do more calculation to the stems and leafs then continue. In this tutorial we will add the values of each row (a stem and its leafs).

• The steps are:
 Create an array that will have the same size as the number of stems and in this array we will add the sum of one row (a stem and its leafs). We will create a method to do the sum part

 Create an object to the class ConvertToInteger ( see the improved version of converting whole numbers into integers)
            int[] sumOneRowValues = new int[stems];
            ConvertToIntegers con = new ConvertToIntegers();

• Then:
 Splite the leafs by whitespace using Split(' ') function
 The returning type of the splitting is array of strings, so, call the conversion method in the ConvertToInteger class
 We create a byte leaf array that will hold the values of the leafs after converting them to integers
 Call the sum method and pass two arguments: the stem, the leaf array

for (int i = 0; i < stems; i++)
            {

                string[] stringNumbers = stemsAndLeafs[i][1].Split(' ');
                byte[] leafs = con.doConvert(stringNumbers);
                sumOneRowValues[i] = doSum(stemsAndLeafs[i][0], leafs);

            }


• Now print the values:
            Console.WriteLine("Sum of all stems\n------------------");
            for (int i = 0; i < stems; i++)
                Console.WriteLine("sum of stem {0} = {1}", stemsAndLeafs[i][0], sumOneRowValues[i]);

The last thing to mention is the doSum() method.
 The method receives the stem as string and the leafs as array of bytes
 We calculate the stem value. Since the stems are in the Tens place, we multiply the stem by 10 (read the theory part)
 We add the stem value to the leaf
private static int doSum(string stem, byte[] leafs)
        {
            int sum = 0;

            int stemValue = (Convert.ToChar(stem) - '0') * 10;

            for (int i = 0; i < leafs.Length; i++)
                sum = sum + stemValue + leafs[i];
            return sum;
        }

output:

That's all for now. Hope you enjoyed it :) 

0 comments:

Post a Comment