View Single Post
Old Feb 19th, 2008, 10:56 AM   #1
gmann145
Newbie
 
Join Date: Jan 2008
Posts: 7
Rep Power: 0 gmann145 is on a distinguished road
Exclamation prime numbers now with arrays

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. }
gmann145 is offline   Reply With Quote