Sunday, July 26, 2009

Help with c# code?

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 %26lt;=100; intNumberToBeTested++)


{


blnPrime = true ;


for (intDivisor = 4; intDivisor %26lt; intNumberToBeTested; intDivisor++) if (intNumberToBeTested % intDivisor == 0)blnPrime = false;


if (blnPrime == true)


{


}


}


dblAverage = intPrimeTotal / intPrimeCount;


Console.WriteLine(dblAverage);


}


}

Help with c# code?
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.
Reply: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++;


}
Reply:yeah cant help you but thanks for two points
Reply:po vale


No comments:

Post a Comment