Click here to Skip to main content
15,878,814 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
C#
namespace program
{
    class program
    {
        static void Main(string[] args)
        {
            int[] grade = new int[10];
            int[] cre = new int[10];
            string[] gpa = new string[10];
            double[] gs = new double[10];
            int T;
            double G;

            for (int i = 0; i < grade.Length; i++)
            {
                //input to array---------------------------------------------------------------------------------------------------------------------------------
                Console.WriteLine("Enter the course grade (-1 to end): ");
                grade[i] = Convert.ToInt32(Console.In.ReadLine());
                if (grade[i] == -1)
                {
                    break;
                }
                Console.WriteLine("Enter the course credit hours:  ");
                cre[i] = Convert.ToInt32(Console.In.ReadLine());
                Console.WriteLine(" ");

                //Grade formating------------------------------------------------------------------------------------------------------------------------------------
                if (grade[i] >= 90)
                {
                    gpa[i] = "A";
                }
                else if (grade[i] <= 89 && grade[i] >= 82)
                { gpa[i] = "B+"; }
                else if (grade[i] <= 81 && grade[i] >= 74)
                { gpa[i] = "B"; }
                else if (grade[i] <= 73 && grade[i] >= 66)
                { gpa[i] = "C+"; }
                else if (grade[i] <= 65 && grade[i] >= 58)
                { gpa[i] = "C"; }
                else if (grade[i] <= 57 && grade[i] >= 50)
                { gpa[i] = "D"; }

                G = grade.Sum() / cre.Sum();


                //total number of credit hours-------------------------------------------------------------------------------------------------------------------------
                if (gpa[i] == "A")
                { gs[i] = 4.00; }
                else if (gpa[i] == "B+")
                { gs[i] = 3.50; }
                else if (gpa[i] == "B")
                { gs[i] = 3.00; }
                else if (gpa[i] == "C+")
                { gs[i] = 2.50; }
                else if (gpa[i] == "C")
                { gs[i] = 2.00; }
                else if (gpa[i] == "D")
                { gs[i] = 1.50; }


                // printing formatings---------------------------------------------------------------------------------------------------------------------------------

            }

            for (int i = 0; i < 1; i++)
            {
                T = cre.Sum();
                G = gs[i] * cre[i] / T;
                //Console.WriteLine("The grades of your courses are : " + ($"{string.Join(", ",gpa)}"));

                Console.WriteLine("The grades of your courses are : " + gpa[i]);
                Console.WriteLine("The total number of credit hours is: " + T);
                Console.WriteLine("Your GPA is:  " + G);
                Console.WriteLine("Your final grade is ");

            }

        }
    }
}


What I have tried:

I need to print all in the "gpa array "without printing the empty ones in this array as well as the user has input them.
so what should I do?
Posted
Updated 26-Oct-22 7:05am
v3
Comments
PIEBALDconsult 25-Oct-22 18:29pm    
Is it not working?
Ahmed Salah Oct2022 25-Oct-22 18:50pm    
yes.
Ahmed Salah Oct2022 25-Oct-22 18:50pm    
logically

You need to add a test statement in your second loop...
C#
for (int i = 0; i < grade.Length; i++)
{
    if (Grade[i] < 1) break;

    // do work here...
}
 
Share this answer
 
v2

What I would suggest is to use generic lists rather than arrays as the list size is not a fixed value so you avoid the empty array cell value problem. Rather than have multiple if then else statements in your code you can calculate the results you require. Something along these lines.

C#
private static void Main(string[] args)
       {
           double creditHoursMin = 1.0;
           double creditHoursInc = 0.5;
           //hold all the ranges  and their associated grades
           //in a list of value tuples.
           List<(int higher, int lower, string grade)> ranges = new() {
                 (100,90,"A"),(89,82,"B+"),(81,74,"B"),
                 (73,66,"C+"),(65,58,"C"),(57,50,"D"),
                 (49,0,"F")};

           // use a list to store the results
           List<(string? grade, double creditHrs)> courseResults = new();
           //using test data here rather than actual user input
           int[] testData = new[] { 10, 55, 60, 70, 76, 84, 92 };
           foreach (int score in testData)
           {
               string? grade = ranges.Where(r => score >= r.lower && score <= r.higher)
                   .Select(t => t.grade)
                   .FirstOrDefault();
               //get the index value of the grade to calculate the credit hours.
               var index = ranges.Select(r => r.grade).ToList().IndexOf(grade);
               //the index value is -1 if the grade is not found
               var creditHrs = index != -1 ? index * creditHoursInc + creditHoursMin : 0;
               courseResults.Add((grade, creditHrs));
           }
           //get all the grades
           IEnumerable<string?> courseResultsGrades = courseResults.Select(cr => cr.grade);
           //print them out
           Console.WriteLine($"The grades of your courses are : {string.Join(',', courseResultsGrades)} ");
           //sum the credit hours
           var creditTotal = courseResults.Select(cr => cr.creditHrs).Sum();
           Console.WriteLine($"The total number of credit hours is: {creditTotal} ");
           Console.ReadLine();

       }

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900