Hello all, I am working on an assignment and with help I have managed to complete the following:

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

namespace StudentGradeDistributions
{
    class Program
    {
        private static string asterics(double grade)
        {
            return new string(' ', (int)grade / 2) + "*" + new string(' ', 50 - (int)grade / 2);
        }

        double a, b, c, d, f;
        int aCount, bCount, cCount, dCount, fCount;

        private void GradeCheck(string MyGrade)
        {
            try
            {
                int Grade = Convert.ToInt32(MyGrade);

                if (Grade <= 50)
                    fCount++;
                else if (Grade > 50 && Grade <= 70)
                    dCount++;
                else if (Grade > 70 && Grade <= 80)
                    cCount++;
                else if (Grade > 80 && Grade <= 90)
                    bCount++;
                else if (Grade >= 90)
                    aCount++;
            }
            catch
            { Console.WriteLine("Not a valid grade!"); }
        }

        public static void Main(string[] args)
        {

            Program newProgram = new Program();
            newProgram.aCount = newProgram.bCount = newProgram.cCount = newProgram.dCount = newProgram.fCount = 0;
            string myGrade = "";

            while (myGrade != "END")
            {
                Console.WriteLine("Please enter a grade:");
                myGrade = Convert.ToString(Console.ReadLine().ToUpper());
                Console.WriteLine("\t{0}", myGrade);
                newProgram.GradeCheck(myGrade);
                Console.WriteLine("Enter End when complete");
            }

            int totalGrades = newProgram.aCount + newProgram.bCount + newProgram.cCount + newProgram.dCount + newProgram.fCount;

            newProgram.a = (Convert.ToDouble(newProgram.aCount) / totalGrades) * 100;
            newProgram.b = (Convert.ToDouble(newProgram.bCount) / totalGrades) * 100;
            newProgram.c = (Convert.ToDouble(newProgram.cCount) / totalGrades) * 100;
            newProgram.d = (Convert.ToDouble(newProgram.dCount) / totalGrades) * 100;
            newProgram.f = (Convert.ToDouble(newProgram.fCount) / totalGrades) * 100;

            Console.WriteLine("A grades are equal to {0}%", newProgram.a);
            Console.WriteLine("B grades are equal to {0}%", newProgram.b);
            Console.WriteLine("C grades are equal to {0}%", newProgram.c);
            Console.WriteLine("D grades are equal to {0}%", newProgram.d);
            Console.WriteLine("F grades are equal to {0}%", newProgram.f);

            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("There were " + totalGrades + " grades entered. Their distribution is as follows:");
            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("");

            Console.WriteLine("     0   10   20   30   40   50   60   70   80   90   100%");
            Console.WriteLine("     |    |    |    |    |    |    |    |    |    |    |");
            Console.WriteLine("");


            Console.WriteLine("A's: |" + asterics(newProgram.a) + newProgram.a + "%");
            Console.WriteLine("B's: |" + asterics(newProgram.b) + newProgram.b + "%");
            Console.WriteLine("C's: |" + asterics(newProgram.c) + newProgram.c + "&");
            Console.WriteLine("D's: |" + asterics(newProgram.d) + newProgram.d + "%");
            Console.WriteLine("F's: |" + asterics(newProgram.f) + newProgram.f + "%");



        }
    }
}

The code is working fine, I just have 2 issues...the precision of the percentages is very high, I need to display them to only 1-2 decimal places and The output should really be a bar chart - it displays the grade and then one asterisk at the right place, but I need to fill the line with asterisks upto the right grade.

Any help would be really appreciated!!

Thanks

Joanne

Recommended Answers

All 3 Replies

So.

private static string asterics(double grade)
        {
            return new string(' ', (int)grade / 2) + "*" + new string(' ', 50 - (int)grade / 2);
        }

The first clue is look carefully what you told it in the above code. Now you should see why its not full of *s

As for the decimal point issue - take a look at String.Format

private static string asterics(double grade)
{
return new string(' ', (int)grade / 2) + "*" + new string(' ', 50 - (int)grade / 2);
}


I have tried it another way around, this time i am getting the asterisks along the line, but I am still not getting the decimals to just 1 or 2 places?

Thanks Mandy

Heres the code:

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

namespace StudentGradeDistributions
{
    class Program
    {
        private static string asterics(double grade)
        {
            return new string(' ', (int)grade / 2) + "*" + new string(' ', 50 - (int)grade / 2);
        }

        double a, b, c, d, f;
        int aCount, bCount, cCount, dCount, fCount;

        private void GradeCheck(string MyGrade)
        {
            try
            {
                int Grade = Convert.ToInt32(MyGrade);

                if (Grade <= 50)
                    fCount++;
                else if (Grade > 50 && Grade <= 70)
                    dCount++;
                else if (Grade > 70 && Grade <= 80)
                    cCount++;
                else if (Grade > 80 && Grade <= 90)
                    bCount++;
                else if (Grade >= 90)
                    aCount++;
            }
            catch
            { Console.WriteLine("Not a valid grade!"); }
        }

        public static void Main(string[] args)
        {

            Program newProgram = new Program();
            newProgram.aCount = newProgram.bCount = newProgram.cCount = newProgram.dCount = newProgram.fCount = 0;
            string myGrade = "";

            while (myGrade != "END")
            {
                Console.WriteLine("Please enter a grade:");
                myGrade = Convert.ToString(Console.ReadLine().ToUpper());
                Console.WriteLine("\t{0}", myGrade);
                newProgram.GradeCheck(myGrade);
                Console.WriteLine("Enter End when complete");
            }

            int totalGrades = newProgram.aCount + newProgram.bCount + newProgram.cCount + newProgram.dCount + newProgram.fCount;

            newProgram.a = (Convert.ToDouble(newProgram.aCount) / totalGrades) * 100;
            newProgram.b = (Convert.ToDouble(newProgram.bCount) / totalGrades) * 100;
            newProgram.c = (Convert.ToDouble(newProgram.cCount) / totalGrades) * 100;
            newProgram.d = (Convert.ToDouble(newProgram.dCount) / totalGrades) * 100;
            newProgram.f = (Convert.ToDouble(newProgram.fCount) / totalGrades) * 100;

            Console.WriteLine("A grades are equal to {0}%", newProgram.a);
            Console.WriteLine("B grades are equal to {0}%", newProgram.b);
            Console.WriteLine("C grades are equal to {0}%", newProgram.c);
            Console.WriteLine("D grades are equal to {0}%", newProgram.d);
            Console.WriteLine("F grades are equal to {0}%", newProgram.f);

            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("There were " + totalGrades + " grades entered. Their distribution is as follows:");
            Console.WriteLine("");
            Console.WriteLine("");

            Console.WriteLine("0  10  20  30  40  50  60  70  80  90  100%");
            Console.WriteLine("|  |   |   |   |   |   |   |   |   |   |");
            Console.WriteLine("");

            Console.WriteLine("____________________________________________");
            for (int i = 0; i < newProgram.aCount; i++)
                Console.Write("**");
            Console.Write(" A" + Environment.NewLine);
            Console.WriteLine("____________________________________________");
            for (int i = 0; i < newProgram.bCount; i++)
                Console.Write("**");
            Console.Write(" B" + Environment.NewLine);
            Console.WriteLine("____________________________________________");
            for (int i = 0; i < newProgram.cCount; i++)
                Console.Write("**");
            Console.Write(" C" + Environment.NewLine);
            Console.WriteLine("____________________________________________");
            for (int i = 0; i < newProgram.dCount; i++)
                Console.Write("**");
            Console.Write(" D" + Environment.NewLine);
            Console.WriteLine("____________________________________________");
            for (int i = 0; i < newProgram.fCount; i++)
                Console.Write("**");
            Console.Write(" F" + Environment.NewLine);
            Console.WriteLine("____________________________________________");


        }
    }
}

Well what did you reading of String.Format tell you?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.