Tuesday, October 25, 2011

Assignment4 Morning Class


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

namespace Assignment4Morning
{
    class Program
    {
        /// 
        /// This program calculates GPAs
        /// Based on grades and credits
        /// entered
        /// Steve Conger 10/25/2011
        /// 
      
        static void Main(string[] args)
        {
            Program p = new Program();

            string quit = "y";
            while (quit != "n")
            {
                p.CreateGPAArrays();
                Console.WriteLine("Continue y/n?");
                quit = Console.ReadLine();
               quit= quit.ToLower();
            }
            //Console.ReadKey();
        }//end main

        private void CreateGPAArrays()
        {
            Console.WriteLine("Enter how many grades you want to enter");
            int number = int.Parse(Console.ReadLine());

            double[] grades = new double[number];
            int[] credits = new int[number];

            FillGPAArrays(grades, credits);
        }//end Create

        private void FillGPAArrays(double[] grades, int[] credits)
        {
            for (int index = 0; index < grades.Length; index++)
            {
                double grade=0;
                
                do
                {
                    Console.WriteLine("enter grade");
                    grade = double.Parse(Console.ReadLine());
                    if (grade < 0 || grade > 4)
                    {
                        Console.WriteLine("Grades must be between 0 and 4");
                        
                    }
                } while (grade < 0 || grade > 4);
                grades[index] = grade;

                Console.WriteLine("enter Credits");
                credits[index] = int.Parse(Console.ReadLine());

            
            }//end for
            CalculateGPA(grades, credits);
        }//end Fill

        private void CalculateGPA(double[] grades, int[] credits)
        {
            double weight=0;
            int totalCredits = 0;

            for (int index = 0; index < grades.Length; index++)
            {
                weight += grades[index] * credits[index];
                totalCredits += credits[index];
            }

            //alternate way to sum credits
            //c# 4.0 and above only
            int total = credits.Sum();

            double gpa = weight / totalCredits;

            Console.WriteLine("Your GPA is {0}", Math.Round(gpa,1));
            
        }


    }//end class
}

No comments:

Post a Comment