954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

dropping the min value from the average

im supposed to write a code that you prompt to enter 10 different grades then drop the minimum value, while computing the average of the remaining 9. I'm getting infinity as the answer when I run the program so I know my min is messed up, somewhere but i cant find it can anyone help?

using System;

namespace gradeblaster
{
   class Program
   {
      public static void Main(string[] args)
      {
         getValues();
      } // end Main
      static void getValues()
      {
         double total;
         double gradeCounter;
         double grade;
         double average;
         double min = Double.MinValue;

         total = 0;
         gradeCounter = 1;
         {
            while (gradeCounter <= 10) // while loop
            {
               Console.Write("Enter grade: ");
               grade = Convert.ToDouble(Console.ReadLine());
               total = total + grade - min;
               gradeCounter = gradeCounter + 1;

               average = (total - min) / 9;
               Console.WriteLine("\nAfter the last score is dropped, the class average is {0}", average);
            }
         }
      }
   } // end class
} // end namespace
rzhaley
Newbie Poster
19 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

double.MinValue is -1 divided by 10^300 or so.
Line 17 of your code is OK, but in line 26 and 29 you are substracting this value two times(x 10) from your total. What you have to do is after every input of a grade check if that input is smaller than min. If it is assign it to min. Make your total, don't substract min here! After 10 times min will now contain the smallest input.
Put lines 29 and 30 outside the while loop.
Succes!

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

If I understand the question correctly you need to remove the lowest value from the ten inputs and then average the remaining nine values.

Currently you are not doing that as the min is never changed from Double.MinValue (-1.7976931348623157E+308 ).

I think you need to add all the grades together and watch for the lowest grade, remove that from the total, then calculate the avereage by dividing by nine. Somthing like this:

double min = double.MaxValue;
            
            //...
            //...

            while (gradeCounter <= 10) // while loop           
            {               
                Console.Write("Enter grade: ");               
                grade = Convert.ToDouble(Console.ReadLine());
                if (grade < min) min = grade;
                total += grade;             
                gradeCounter = gradeCounter + 1;           
            }

            average = (total - min) / 9;
            Console.WriteLine("\nAfter the last score is dropped, the class average is {0}", average);
privatevoid
Junior Poster in Training
92 posts since Feb 2009
Reputation Points: 34
Solved Threads: 21
 

You are right privatevoid, for a min-function you have to test on the maxvalue of course! Thanks for reminding.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

thanks guys........i dont know why i never thought of asking anyone for help at the beginning of the semester then i wouldn't be so behind!

rzhaley
Newbie Poster
19 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

We're all learning, and we're all here to help each other.
Welcome to Daniweb, as long as you show a willingness to learn and make an effort for yourself we will always be happy to help you :)

Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: