View Single Post
Old Feb 19th, 2008, 1:27 PM   #2
peaceofpi
hi: for(;;) goto hi;
 
peaceofpi's Avatar
 
Join Date: Jun 2006
Posts: 93
Rep Power: 3 peaceofpi is on a distinguished road
Send a message via AIM to peaceofpi Send a message via MSN to peaceofpi
Re: prime numbers now with arrays

Quote:
Originally Posted by gmann145 View Post
so this really got me i, i have to write the numbers to an array then to the file, all i keep getting is numbers 0-99 however in the file help would be greatly appreciated.


c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. bool isPrime(int c);
  6. void write_array(int x, ofsteam &outfile);
  7. void write_prime(int num, int p);
  8.  
  9. int main()
  10. {
  11. int p = 0;
  12. int count;
  13. const int arrays = 100
  14. ofstream pfile("Prime.txt"); //Declare variables
  15.  
  16. for (int c =0; c<100,c++) //Write all the arrays to 0
  17. arrays[c]=0;
  18.  
  19.  
  20. for (int x = 2; x < 100; x++) //Finding The Prime Numbers
  21. {
  22. if(isPrime(x))
  23. {
  24. write_array(x, pos); //Writing Primes to Array
  25. pos++
  26. }
  27.  
  28. }
  29.  
  30.  
  31. for(count = 0; arrays[count] != 0; count ++) //Write arrays to file
  32. write_prime(arrays[count],pfile);
  33.  
  34. pfile.close();
  35. return 0;
  36. }
  37.  
  38. bool isPrime(int c)
  39. {
  40. for (int y = 2; y < c; y++)
  41. {
  42. if(c % y == 0)
  43. {
  44. return false;
  45. break;
  46. }
  47.  
  48. }
  49. return true;
  50. }
  51.  
  52. void write_prime(int p, ofstream &pfile)
  53. {
  54. pfile << p << endl;
  55.  
  56. }
  57.  
  58. void write_array(int num, int p)
  59. {
  60. arrays[p]=num;
  61.  
  62. return;
  63. }
Wow, where to start? arrays is a const int (with no semicolon), but later you try to use it as an array. Also your for loop has a comma instead of a semicolon. p is apparently trying to be pos. Your functions are too simple to be functions (you make it more confusing than helpful).

	for (int c =0; c<100;c++) //Write all the arrays to 0
		arrays[c]=0;
This can be eliminated by just using pos as a limit in the last for loop.

I think you should go over a tutorial from the beginning and rethink your logic here, everything is more complicated than it needs to be.
__________________
How do you play Religious Roulette?
Stand around in a circle and blaspheme till someone gets struck by lightning.
peaceofpi is offline   Reply With Quote