Jacob
Jacob

Reputation: 728

C# Math percentage issue

I have a problem in calculating percentage. I couldn't work out what is the issue. The problem is the 'perc' variable used in the showData method, but I've included the whole source code. It just keeps outputting zero. Thanks in advance.

This is my code‌:

using System;
using System.Linq;

namespace Task6_5
{
class Traffic
{
    int[] trafficCount;
    int max = 0;
    int total = 0, index;
    double perc;

    public static void Main (string[] args)
    {
        Traffic myTraffic = new Traffic ();
        myTraffic.report ();
    }
    public Traffic() {
        trafficCount = new int[5];
    }
    public void enterCounts() {
        for (int i = 0; i < 5; i++) {
            Console.WriteLine ("Please enter Car Count No. " + (i + 1) + " : ");
            trafficCount [i] = Convert.ToInt32(Console.ReadLine ());
        }
    }
    public void showTotal() {
        for (int i = 0; i < trafficCount.Length; i++) {
            total += trafficCount [i];
        }
    }
    public void busiest() {
        max = trafficCount.Max ();
        index = Array.IndexOf (trafficCount, max) + 1;
        Console.WriteLine ("Busiest Hour : " + index);
    }
    public void showData() {
        Console.WriteLine ("Traffic Report");
        Console.WriteLine ("====================");
        Console.WriteLine ("Hour        Car Count       Percentage Total");
        Console.WriteLine ("================================================");
        for (int i = 0; i < trafficCount.Length; i++) {
            perc = trafficCount[i] / total * 100;
            Console.WriteLine ((i + 1) + "              " + trafficCount [i] + "         " + perc + "%");
    }
        Console.WriteLine ("Total Number of cars seen today : " + total);
}
    public void report() {
        enterCounts ();
        showTotal ();
        showData ();
        busiest ();
    }
}
}

Upvotes: 1

Views: 151

Answers (3)

recursive
recursive

Reputation: 86064

Integer division yields integers in c#. You can re-order your expression to correct for it though:

perc = trafficCount[i] * 100 / total;

Upvotes: 3

Habib
Habib

Reputation: 223237

You statement:

perc = trafficCount[i] / total * 100;

is working with integer data type. Although you have defined perc as double but the right hand side of the assignment is all integer and hence the calculation is being done in integer type. You need at least one of the operand to be of type double/float to get a floating point number. like:

perc = ((double)trafficCount[i] / total) * (double)100;

In your current code all the calculations are being done in integer type , thus resulting an integer value so for something like 0.12 you would end up with 0. By casting your operands to double you inflict floating point asthmatics. You can either cast to double or use 100d

Upvotes: 2

BRAHIM Kamel
BRAHIM Kamel

Reputation: 13765

you should change this to double because int / int return an int in c#

 double [] trafficCount;
    double total = 0; 

or you can just

((double)trafficCount[i]) / ((double)total * 100.0f);

Upvotes: 3

Related Questions