Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
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
Old Feb 19th, 2008, 1:27 PM   #2
peaceofpi
hi: for(;;) goto hi;
 
peaceofpi's Avatar
 
Join Date: Jun 2006
Posts: 85
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
Old Feb 19th, 2008, 4:55 PM   #3
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
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.
OpenLoop is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Calculate prime numbers peaceofpi Show Off Your Open Source Projects 23 Nov 2nd, 2006 1:02 AM
Prime Numbers Konnor C++ 6 Sep 12th, 2005 6:46 PM
Generating prime and composite numbers saadmir C 5 Sep 9th, 2005 4:08 PM
Prime numbers thomzor C++ 24 May 20th, 2005 2:02 PM
Help with a couple prime numbers programs!! BehemothPhoenix C++ 3 Mar 20th, 2005 2:06 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:41 PM.

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