Thread: Help please.

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    21

    Question Help please.

    I am currently working on a project for my C++ course and it requires that i find the first 500 prime numbers using an array. How would I go about doing this?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Please use more descriptive subject titles.

    And nobody here is going to do your homework for you. You will get help if you try it on your own first and then come to us with a specific problem, but you clearly haven't done that yet. Either try writing it on your own, or search the board for some tips.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Welcome to the boards. If you haven't already done so then please take some time to familiarise yourself with the faq:
    http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

    Make sure you have a look at the the posting guidelines:
    http://cboard.cprogramming.com/annou...ouncementid=51
    Following the rules will ensure you get a prompt answer to your question.

    Remember, too, that the board has a search facility, a link is at the top of your screen:
    http://cboard.cprogramming.com/search.php
    It will often get you a quicker answer to your questions than waiting for a response to one you have posted.


    If you have any questions about this you may ask or you can contact one of our forum leaders:

    http://cboard.cprogramming.com/showgroups.php

    ----------

    with that aside, here's your answer:
    1. find out how to figure out a prime number
    2. figure out how to use arrays
    3. figure out how to find a prime number using arrays.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    21
    I apologize if by my post it seemed as if I had not attempted the problem. So far I have been working on it for about 3 days now. I understand that to find a prime number I need to divide each individual test value by all of the previous numbers in the array, but for some reason I can't come up with a logical code that will accomplish that. i think that if someone could just push me in the right direction that would be a big help.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Post the code that you have so far. If you haven't even started, start out with a simple loop that will do what you said above.

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    21
    As of now this is what I have written in:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int prime[500];
    	int test = 3;
    	int i = 1;
    
    	cout << "\nThese are the first 500 prime numbers:\n";
    	prime[0] = 2;
    	for ()
    	{
    		while()
    		{
    			if ((test%prime[i]) = 0)
    				test = test + 2;
    			else
    				prime[i + 1] = test;
    		}
    	}
    I'm not quite sure what to put in the parameters for the for and while loops though...

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Well you do want to have 2 (maybe more- depending on how you do each one) loops. The first is just going to loop through the code to find prime numbers until you have 500. The second loop is going to test each prime number by dividing it by each number below it.

    I haven't looked at it that thoroughly, but it looks like you've done a pretty decent job on the logic inside you loops. Keep the purpose of each loop in mind, and that should help you decide how you should set them up.

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    21
    thanks for your help and I'll be sure that my future posts are a little more thought out before I put them up. I'll just keep plugging away at it.

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Sounds good. And now that we've seen that you have actually put work into this (we get a lot of requests from people expecting us to do everything), I will remind of you what I suggested earlier: search the boards. This is a very common assignment, and you might get some ideas from seeing how other people have handled it.

  10. #10
    Registered User
    Join Date
    Apr 2005
    Posts
    21
    Just wanted to post to let you know that i did figure that problem out. I just kinda tinkered with it (got a TON of errors) but ended up with this as my result:

    Code:
    	for (j = 0; j <499; j++)
    	{
    		for (i = 0; i <= j; i++)
    		{
    			if ((test%prime[i]) == 0)
    			{
    				test = test + 2;
    			}
    		}
    		prime[i] = test;
    		test = test + 2;
    	}
    Thanks for your help in pushing me in the right direction, I'll be sure to come back around for questions and to possibly help out anyone else, if I can.

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Congratulations - I'm glad you got it working.

Popular pages Recent additions subscribe to a feed