Finding Average Prime Number and writing to file.

Page 1 of 1

14 Replies - 3866 Views - Last Post: 17 February 2010 - 05:01 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=156005&amp;s=000611dd44c06c0ce5b0fc7cb653407c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 lisasgenesis

Reputation: 0
• Posts: 11
• Joined: 30-January 10

Finding Average Prime Number and writing to file.

Posted 14 February 2010 - 03:12 PM

The point is to find the average of all prime numbers between 1 and 100 and write thsee numbers to a file. Then calculate the average of the prime numbers and display the value in a message on the console. But write now all I'm getting is the number 42 and it would be 47.something.

```using System;
using System.IO;
public class Lab5
{
public static void Main()
{
double dblAverage;
int intPrimeCount = 0;
int intPrimeTotal = 0;
bool blnPrime;
int intDivisor;
short intNumberToBeTested;
string strAverage;

for (intNumberToBeTested = 2; intNumberToBeTested <= 100; intNumberToBeTested++)
{
blnPrime = true;
for (intDivisor = 2; intDivisor < intNumberToBeTested; intDivisor++)
if (intNumberToBeTested % intDivisor == 0)
blnPrime = false;

if (blnPrime == true)
{
intPrimeCount = intPrimeCount + 1;
intPrimeTotal = intPrimeTotal + intNumberToBeTested;
}

}

dblAverage = intPrimeTotal / intPrimeCount;

strAverage = Convert.ToString(dblAverage);
Console.WriteLine(dblAverage);

}

}

```

Mod Edit:
Added in code tags. When posting code please use code tags like below.

Is This A Good Question/Topic? 0

Replies To: Finding Average Prime Number and writing to file.

#2 janne_panne

• WinRT Dev

Reputation: 428
• Posts: 1,047
• Joined: 09-June 09

Re: Finding Average Prime Number and writing to file.

Posted 14 February 2010 - 03:25 PM

I get 42 too so that I think that's the right number. I used a bit different code I have written earlier for finding primes and I don't think it contains any errors.

edit: Or actually it is 42,4. Didn't notice I was calculating with longs instead of doubles/decimals.

This post has been edited by janne_panne: 14 February 2010 - 03:40 PM

#3 lisasgenesis

Reputation: 0
• Posts: 11
• Joined: 30-January 10

Re: Finding Average Prime Number and writing to file.

Posted 14 February 2010 - 04:04 PM

janne_panne, on 14 February 2010 - 02:25 PM, said:

I get 42 too so that I think that's the right number. I used a bit different code I have written earlier for finding primes and I don't think it contains any errors.

edit: Or actually it is 42,4. Didn't notice I was calculating with longs instead of doubles/decimals.

How would I read each of these numbers to a file. I already have the output to the console screen.

#4 janne_panne

• WinRT Dev

Reputation: 428
• Posts: 1,047
• Joined: 09-June 09

Re: Finding Average Prime Number and writing to file.

Posted 14 February 2010 - 04:09 PM

You can use System.IO.File class for reading and writing:

http://msdn.microsof...le_members.aspx

There are billions of file writing tutorials on the net and probably here on DIC too, search a bit and you should find them.

#5 lisasgenesis

Reputation: 0
• Posts: 11
• Joined: 30-January 10

Re: Finding Average Prime Number and writing to file.

Posted 14 February 2010 - 04:50 PM

janne_panne, on 14 February 2010 - 03:09 PM, said:

You can use System.IO.File class for reading and writing:

http://msdn.microsof...le_members.aspx

There are billions of file writing tutorials on the net and probably here on DIC too, search a bit and you should find them.

Okay...I get two errors....any suggestions on where I'm going wrong with the SteamWriter?

```using System;
using System.IO;
public class Lab5
{
public static void Main()
{
double dblAverage;
int intPrimeCount = 0;
int intPrimeTotal = 0;
bool blnPrime;

int intDivisor;

short intNumberToBeTested;

string strAverage;

string file_name = "C:/testData3.txt";

System.IO.

StreamWriter objWriter;

objWriter = new System.IO.SreamWriter(file_name);

objWriter.Write(testData3.txt);

objWriter.Close();

for (intNumberToBeTested = 2; intNumberToBeTested <= 100; intNumberToBeTested++)

{

blnPrime = true;
blnPrime = false;

if (blnPrime == true) //If the number was found to be prime than update our variables
{

intPrimeCount = intPrimeCount + 1;
intPrimeTotal = intPrimeTotal + intNumberToBeTested;
}
}

dblAverage = intPrimeTotal / intPrimeCount;

Console.WriteLine(" The Average of all Prime Numbers between 1 and 100 is {0:F2}", dblAverage);

}

}

```

Mod Edit:
Please use code tags like below.

#6 Sergio Tapia

• D.I.C Lover

Reputation: 1256
• Posts: 4,168
• Joined: 27-January 10

Re: Finding Average Prime Number and writing to file.

Posted 14 February 2010 - 05:16 PM

Wrap your code with "[code]" tags.

#7 lisasgenesis

Reputation: 0
• Posts: 11
• Joined: 30-January 10

Re: Finding Average Prime Number and writing to file.

Posted 14 February 2010 - 05:25 PM

stapia.gutierrez, on 14 February 2010 - 04:16 PM, said:

Wrap your code with "[ code]" tags.

This is going to give me new grey hairs.
Okay. I changed the code to write to a file...and the file does exist but only has the average of the prime numbers in it. What I need is to modify this code so the prime numbers are stored in the file, then retrieve the values and calculate the average of the prime numbers found and that gets written with a message to the console (which I already have). But where am I going wrong with writing each prime number to the text file.

Here's the code:

```using System;
using System.IO;
public class Lab5
{
public static void Main()
{
double dblAverage;
int intPrimeCount = 0;
int intPrimeTotal = 0;
bool blnPrime;
int intDivisor;
short intNumberToBeTested;
string strAverage;

for (intNumberToBeTested = 2; intNumberToBeTested <= 100; intNumberToBeTested++)
{
blnPrime = true;
blnPrime = false;
if (blnPrime == true) //If the number was found to be prime than update our variables
{
intPrimeCount = intPrimeCount + 1;
intPrimeTotal = intPrimeTotal + intNumberToBeTested;
}
}
dblAverage = intPrimeTotal / intPrimeCount;
Console.WriteLine(" The Average of all Prime Numbers between 1 and 100 is {0:F2}",
dblAverage);

StreamWriter sw;
sw = new StreamWriter ("mytextfile.txt");
sw.WriteLine(dblAverage);

sw.Close();

}

}

```

This post has been edited by lisasgenesis: 14 February 2010 - 06:05 PM

#8 lisasgenesis

Reputation: 0
• Posts: 11
• Joined: 30-January 10

Re: Finding Average Prime Number and writing to file.

Posted 14 February 2010 - 05:47 PM

stapia.gutierrez, on 14 February 2010 - 04:16 PM, said:

Wrap your code with "[code]" tags.

I really need some direction. Pleeezzzeee. I know it's something stupid.

#9 n8wxs

• --... ...-- -.. . -. ---.. .-- -..- ...

Reputation: 972
• Posts: 3,878
• Joined: 07-January 08

Re: Finding Average Prime Number and writing to file.

Posted 14 February 2010 - 05:58 PM

Thanks!

#10 SixOfEleven

• using Caffeine;

Reputation: 947
• Posts: 6,342
• Joined: 18-October 08

Re: Finding Average Prime Number and writing to file.

Posted 14 February 2010 - 06:05 PM

You had System.IO where you didn't need it just above StreamWriter. You may have a problem writing to C:\ if you are using Vista.

```using System;
using System.IO;
public class Lab5
{
public static void Main()
{
double dblAverage;
int intPrimeCount = 0;
int intPrimeTotal = 0;
bool blnPrime;

int intDivisor;

short intNumberToBeTested;

string strAverage;

string file_name = "C:/testData3.txt";
StreamWriter objWriter;

objWriter = new System.IO.SreamWriter(file_name);

objWriter.Write(testData3.txt);

objWriter.Close();

for (intNumberToBeTested = 2; intNumberToBeTested <= 100; intNumberToBeTested++)

{

blnPrime = true;
blnPrime = false;

if (blnPrime == true) //If the number was found to be prime than update our variables
{

intPrimeCount = intPrimeCount + 1;
intPrimeTotal = intPrimeTotal + intNumberToBeTested;
}
}

dblAverage = intPrimeTotal / intPrimeCount;

Console.WriteLine(" The Average of all Prime Numbers between 1 and 100 is {0:F2}", dblAverage);

}

}

```

#11 lisasgenesis

Reputation: 0
• Posts: 11
• Joined: 30-January 10

Re: Finding Average Prime Number and writing to file.

Posted 14 February 2010 - 06:08 PM

n8wxs, on 14 February 2010 - 04:58 PM, said:

Thanks!

Thank you for explaining it. I'm fairly new here. I think I've edited correctly now.

SixOfEleven, on 14 February 2010 - 05:05 PM, said:

You had System.IO where you didn't need it just above StreamWriter. You may have a problem writing to C:\ if you are using Vista.

```using System;
using System.IO;
public class Lab5
{
public static void Main()
{
double dblAverage;
int intPrimeCount = 0;
int intPrimeTotal = 0;
bool blnPrime;

int intDivisor;

short intNumberToBeTested;

string strAverage;

string file_name = "C:/testData3.txt";
StreamWriter objWriter;

objWriter = new System.IO.SreamWriter(file_name);

objWriter.Write(testData3.txt);

objWriter.Close();

for (intNumberToBeTested = 2; intNumberToBeTested <= 100; intNumberToBeTested++)

{

blnPrime = true;
blnPrime = false;

if (blnPrime == true) //If the number was found to be prime than update our variables
{

intPrimeCount = intPrimeCount + 1;
intPrimeTotal = intPrimeTotal + intNumberToBeTested;
}
}

dblAverage = intPrimeTotal / intPrimeCount;

Console.WriteLine(" The Average of all Prime Numbers between 1 and 100 is {0:F2}", dblAverage);

}

}

```

#12 lisasgenesis

Reputation: 0
• Posts: 11
• Joined: 30-January 10

Re: Finding Average Prime Number and writing to file.

Posted 14 February 2010 - 06:33 PM

I've changed my code. I get the console screen to display the average of all prime numbers between 1 and 100. So that's good. But I am suppose to write to a file each of those prime numbers. The code to write to the file is at the bottom. It does create a text file, but is only putting in the average of all the prime numbers. I know it's something stupid. And what I think is that in the code that determines if the number is prime, do I need another variable to hold the number so it can be written to the file? Soemthing like that?

```
using System;
using System.IO;
public class Lab5
{
public static void Main()
{
double dblAverage;
int intPrimeCount = 0;
int intPrimeTotal = 0;
bool blnPrime;
int intDivisor;
short intNumberToBeTested;
string strAverage;

for (intNumberToBeTested = 2; intNumberToBeTested <= 100; intNumberToBeTested++)
{
blnPrime = true;
blnPrime = false;
if (blnPrime == true) //If the number was found to be prime than update our variables
{
intPrimeCount = intPrimeCount + 1;
intPrimeTotal = intPrimeTotal + intNumberToBeTested;
}
}
dblAverage = intPrimeTotal / intPrimeCount;
Console.WriteLine(" The Average of all Prime Numbers between 1 and 100 is {0:F2}",
dblAverage);

StreamWriter sw;
sw = new StreamWriter ("mytextfile.txt");
sw.WriteLine(dblAverage);

sw.Close();
}
}

```

#13 SixOfEleven

• using Caffeine;

Reputation: 947
• Posts: 6,342
• Joined: 18-October 08

Re: Finding Average Prime Number and writing to file.

Posted 14 February 2010 - 07:47 PM

What you need to do, is when you are determining what numbers are prime, is write them out to the text file as you find them. To do that, open the file at the top of the program. When you find a prime number write it to the file.

```
using System;
using System.IO;
public class Lab5
{
public static void Main()
{
double dblAverage;
int intPrimeCount = 0;
int intPrimeTotal = 0;
bool blnPrime;
int intDivisor;
short intNumberToBeTested;
string strAverage;

StreamWriter sw;
sw = new StreamWriter ("mytextfile.txt");

for (intNumberToBeTested = 2; intNumberToBeTested <= 100; intNumberToBeTested++)
{
blnPrime = true;
blnPrime = false;
if (blnPrime == true) //If the number was found to be prime than update our variables
{
intPrimeCount = intPrimeCount + 1;
intPrimeTotal = intPrimeTotal + intNumberToBeTested;
sw.WriteLine(intNumberToBeTested);
}
}
dblAverage = intPrimeTotal / intPrimeCount;
Console.WriteLine(" The Average of all Prime Numbers between 1 and 100 is {0:F2}",
dblAverage);

sw.WriteLine(dblAverage);

sw.Close();
}
}

```

Reputation: 66
• Posts: 560
• Joined: 27-October 09

Re: Finding Average Prime Number and writing to file.

Posted 17 February 2010 - 04:17 AM

wouldn't it be better to save all the numbers to a list so you only have to have the stream writer open to put the array to file? I always try to keep readers/writers open as little as possible.

#15 SixOfEleven

• using Caffeine;

Reputation: 947
• Posts: 6,342
• Joined: 18-October 08

Re: Finding Average Prime Number and writing to file.

Posted 17 February 2010 - 05:01 PM

That would be an alternative of course. If this is a topic from a programming class, the person might not have been introduced to List<T> yet. If they were calculating a lot of primes, in to the millions, they that would be a much better idea as the stream would be open much longer than is probably needed to be.