14 Replies - 3451 Views - Last Post: 17 February 2010 - 05:01 PM Rate Topic: -----

#1 lisasgenesis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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); 

Console.ReadLine(); 
}

}



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

Is This A Good Question/Topic? 0
  • +

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

#2 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 lisasgenesis  Icon User is offline

  • New D.I.C Head

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

Re: Finding Average Prime Number and writing to file.

Posted 14 February 2010 - 04:04 PM

View Postjanne_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.
Was This Post Helpful? 0
  • +
  • -

#4 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 lisasgenesis  Icon User is offline

  • New D.I.C Head

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

Re: Finding Average Prime Number and writing to file.

Posted 14 February 2010 - 04:50 PM

View Postjanne_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); 

Console.ReadLine(); 

}

}



Mod Edit:
Please use code tags like below.
:code:
Was This Post Helpful? 0
  • +
  • -

#6 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1251
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#7 lisasgenesis  Icon User is offline

  • New D.I.C Head

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

Re: Finding Average Prime Number and writing to file.

Posted 14 February 2010 - 05:25 PM

View Poststapia.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();
Console.ReadLine(); 

}

}



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

Was This Post Helpful? 0
  • +
  • -

#8 lisasgenesis  Icon User is offline

  • New D.I.C Head

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

Re: Finding Average Prime Number and writing to file.

Posted 14 February 2010 - 05:47 PM

View Poststapia.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.
Was This Post Helpful? 0
  • +
  • -

#9 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Finding Average Prime Number and writing to file.

Posted 14 February 2010 - 05:58 PM

Please use the code tags around your code posts:

:code:

Thanks!
Was This Post Helpful? 0
  • +
  • -

#10 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 942
  • View blog
  • 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); 

Console.ReadLine(); 

}

}


Was This Post Helpful? 0
  • +
  • -

#11 lisasgenesis  Icon User is offline

  • New D.I.C Head

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

Re: Finding Average Prime Number and writing to file.

Posted 14 February 2010 - 06:08 PM

View Postn8wxs, on 14 February 2010 - 04:58 PM, said:

Please use the code tags around your code posts:

:code:

Thanks!


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

View PostSixOfEleven, 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); 

Console.ReadLine(); 

}

}


Was This Post Helpful? 0
  • +
  • -

#12 lisasgenesis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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(); 
Console.ReadLine();  
} 
}



Was This Post Helpful? 0
  • +
  • -

#13 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 942
  • View blog
  • 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(); 
Console.ReadLine();  
} 
}


Was This Post Helpful? 0
  • +
  • -

#14 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#15 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 942
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1