0

My problem is that when I try to calculate the grade with assignments where there is less than 25/25 the grade goes to 75%. Disregarding any other points in my section.

So if every assignment has 100% on I get:

Grade   A
Percentage  105%
Labs    250 / 250

But when 1 point out of labs is missing:

Grade   C
Percentage  75%
Labs    249 / 250

Why would that be? I'm bit confused with this.

This is how I gather input from my text fields. Every text field is calling this: AutoPostBack="True" OnTextChanged="textbox_TextChanged".

    foreach (Control control in Page.Controls)
    {
        foreach (Control textbox in control.Controls)
        {
            if (textbox is TextBox)
            {
                TextBox tb = textbox as TextBox;

                char type = tb.ID.ToCharArray()[0];

                if (!string.IsNullOrWhiteSpace(tb.Text))
                {
                    if (type == 'l')
                    {
                        int outtt;
                        string inn = tb.Text;
                        Int32.TryParse(inn, out outtt);

                        labs += outtt;
                        labPoints += 25;
                    }
                    else if (type == 'q')
                    {
                        ...
                    }
                    ...
                }
            }
        }
    }

This is how I calculate the percentage:

        percent = (((labs / labPoints) * 0.3) + // labs
                ((quizzes / quizPoints) * 0.1) + // quizzes
                ((exams / examPoints) * 0.3) + // exams
                ((project / projectPoints) * 0.2) + // project
                ((extra / extraPoints) * 0.05) + // extra credit
                ((part / partPoints) * 0.10)) * 100;// participation
HelpNeeder
  • 6,383
  • 24
  • 91
  • 155
  • 3
    Because it is integer division not floating point division. – qqbenq Mar 31 '14 at 22:35
  • Are you sure you're getting the correct values for each of your variables? And what's the data type of percent? Try making it `double` – Nathan Mar 31 '14 at 22:35
  • 1
    Please post types and values for `labs` and all other variables involved. Side note: consider separating UI/collecting data from computation. I.e. collect all data into a list of `{Points, Subject}` and than update your sample code here with just computation part - so you'll be able to easily show/test what data causes wrong values. – Alexei Levenkov Mar 31 '14 at 22:36
  • 2
    possible duplicate of [Why integer division in c# returns an integer but not a float?](http://stackoverflow.com/questions/10851273/why-integer-division-in-c-sharp-returns-an-integer-but-not-a-float) – qqbenq Mar 31 '14 at 22:38
  • But labs and labPoints are integers I guess :) – qqbenq Mar 31 '14 at 22:39
  • Change `(labs / labPoints) * 0.3` to `(0.3 * labs) / labPoints` (and the same for the others) and you should see the answer you want. – hatchet - done with SOverflow Mar 31 '14 at 22:40
  • INT problem indeed. Thanks – HelpNeeder Mar 31 '14 at 22:42
  • 1
    @Nath - the two equations in my comment (apart from automatic type conversions) are essentially equivalent. Whether that is a correct grade formula is an independent question. – hatchet - done with SOverflow Mar 31 '14 at 22:44
  • @hatchet yup, that was my bad. I guess I was seeing differently then. Time for bed. LOL – Nathan Mar 31 '14 at 22:47

1 Answers1

4

You are using integer division, which will always result in an integer. For a quick and dirty fix you should cast your variables to some floating point variables when you divide them.

e.g.:

(double)labs / (double)labPoints

(If any of the arguments is a double then the result will be a double.)

qqbenq
  • 10,220
  • 4
  • 40
  • 45