Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   prime numbers now with arrays (http://www.programmingforums.org/showthread.php?t=15213)

gmann145 Feb 19th, 2008 10:56 AM

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. :?:


:

  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. }


peaceofpi Feb 19th, 2008 1:27 PM

Re: prime numbers now with arrays
 
Quote:

Originally Posted by gmann145 (Post 141230)
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. :?:


:

  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.

OpenLoop Feb 19th, 2008 4:55 PM

Re: prime numbers now with arrays
 
I'm surprised you got any output since your code does not compile. You should make sure your program works using standard output first.


All times are GMT -5. The time now is 12:31 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC