English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I need to try and get this code to come out to 42.4 any suggestions?

Here is my code it's coming out to 42 so I am off somewhere but can't figure it out.
//Declare all required variables
double dblAverage; //the average should allow for decimal values
int intPrimeCount = 0; //this represents the total count of prime numbers
int intPrimeTotal = 0; //this represents the sum of all prime numbers
bool blnPrime;
int intDivisor; //use this variable in the Mod operation
short intNumberToBeTested;
for(intNumberToBeTested = 2; intNumberToBeTested <=100; intNumberToBeTested++)
{
blnPrime = true ;
for (intDivisor = 4; intDivisor < intNumberToBeTested; intDivisor++) if (intNumberToBeTested % intDivisor == 0)blnPrime = false;
if (blnPrime == true)
{
}
}
dblAverage = intPrimeTotal / intPrimeCount;
Console.WriteLine(dblAverage);
}
}

2006-12-02 13:16:58 · 4 answers · asked by Christina 2 in Computers & Internet Programming & Design

4 answers

The problem resides in the following line:
dblAverage = intPrimeTotal / intPrimeCount;

Technically, you're performing integer math to compute the average of 42, then assigning it to a float... 42.0

Change the line to this:
dblAverage = (double) intPrimeTotal / intPrimeCount;

That should do it.

2006-12-02 13:19:31 · answer #1 · answered by TankAnswer 4 · 1 0

dblAverage = intPrimeTotal / intPrimeCount;

its throws Exception: System.DivideByZero Exception: Attemped to divide by zero.

the "intPrimeCount" is Zero

in:
if (blnPrime == true)
{
// do something related to intPrimeCount
// iam talking about increasing the intPrimeCount like intPrimeCount++;
}

2006-12-02 13:45:58 · answer #2 · answered by Hossam Ahmed 2 · 0 0

po vale

2006-12-02 13:19:17 · answer #3 · answered by x_woman_32 5 · 0 0

yeah cant help you but thanks for two points

2006-12-02 13:21:34 · answer #4 · answered by ashley l 3 · 0 0

fedest.com, questions and answers