-1

You have to introduce a string, count the letters and give the percentage of distribution of the letters from the string.

For example: for the word : "abracadabra" it should display

a 45.45
b 18.18
r 18.18
c 9.09
d 9.09
namespace CharsDistribution
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = Console.ReadLine();
            char[] chs = new char[0];
            for (int i = 0; i < s.Length; i++)
            {
                int k = -1;
                for (int j = 0; j < chs.Length; j++)
                {
                    if (chs[j] == s[i])
                    {
                        k = j;
                    }
                }

                if (k == -1)
                {
                    Array.Resize(ref chs, chs.Length + 1);
                    chs[chs.Length - 1] = s[i];
                }
            }

            int[] chsC = new int[chs.Length];

            for (int i = 0; i < chs.Length; i++)
            {
                for (int j = 0; j < s.Length; j++)
                {
                    if (chs[i] == s[j])
                    {
                        chsC[i]++;
                    }
                }
            }

            double[] p = new double[chsC.Length];
            for (int i = 0; i < chsC.Length; i++)
            {
                p[i] = chsC[i] * 100 / d s.Length;
            }

            for (int i = 0; i < chs.Length; i++)
            {
                Console.WriteLine("{0} {1:F2}", chs[i], p[i]);
            }

            Console.Read();
        }
    }
}

It displays me the percentage but without the decimals, like:

a 45.00
b 18.00
c 9.00
etc.
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122

1 Answers1

0

This is an integer division

p[i] = chsC[i] * 100 / s.Length;

If you divide an integer by another, the compiler will give you another integer. You should first cast one of the operands to a double:

p[i] = chsC[i] * 100 / (double)s.Length;

or even

p[i] = chsC[i] * 100.0 / s.Length;