Thread: C++ Help

  1. #1
    Unregistered
    Guest

    Cool C++ Help

    I need code to find the first 100 prime numbers, then print them from an array. I'm lost - please help me find my way. Thanks.

  2. #2
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230

    Angry Haw-Haw!

    No... I refuse to... You wanna know why? Because you are asking for code without contributing your own code (or ideas).

    Don't you think that the people on this board have something better to do than your homework?

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I need code to find the first 100 prime numbers
    You obviously don't need it badly enough to write your own code or simply search the net for the hundreds of examples for this problem.

    These boards are meant to help people learn and solve problems that they get stuck on while writing programs. We are not here to give you code so that you can breeze through a class without trying.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Unregistered
    Guest
    Please be nice, I'm new here. I did do some of the code. I need help finishing. I'll post the code I have done later, and hopefully you guys can help me. I'm a new girl on the block, and I'm sorry if I offended any of you guys. Thanks

  5. #5
    Unregistered
    Guest
    Ok, here's what I have so far. My main problem is that I'm a little confused by arrays, What I need is to calaculate the 1st 100 (ignoring 1 and 2), store them in an array, then to print them out.


    #include <iostream>
    #include <string>
    using namespace std;

    bool Prime(int i) {

    for ( int j = 2; j <= i-1; j++);
    { if (i % j = 0)
    return false;}
    return true;}

    int count = 0
    for (i = 3; count < 100; i+=2);


    cout << Prime << endl;

    Any Help you guys can provide would be very appreciated,

    Thank You

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    i dont mean to offend, but your code that you have there is really messy, missing a lot of semicolons and brackets...

    also, you want to store the prime numbers in an array, but you dont have a single array in your code.

    bool values only have true and false, so at the end of your code, when you print "Prime" you arent going to have any output, since it is just a true/false value.

    an array would look like this:

    int Prime[101];

    to use specific values in the array, you could use

    Prime[1];

    which would return the first value in the array. or

    int i = 1;
    Prime[i];
    ++i;

    which, if looped, would return every value in the array. i take that back. if you loop that whole block of code, it would keep setting i back to 1. just loop the second 2 lines.

    this array can store 100 values and 1 terminating null (hence 101 instead of 100. you will always need one more in an array than you are going to use. the last byte is the terminating null).

    also, for If statements to test equality, you use a double equal sign. so instead of

    if ( i % j = 0 )

    you have

    if ( i % j == 0 )

    a single equal sign assigns value, it does not test for equality. always remember that, it will save you countless hours of headache.

    your program is also severely lacking a main() statement.

    i wrote a program that calculates prime numbers indefinitely. it has no arrays in it, but maybe you can get some ideas from it:


    #include <stdio.h>
    #include <stdlib.h>

    void prime(); //function to find primes.

    int i=3,x=0,y,z,w=1; //x, y, and z are just numbers used
    //to test and retain values. z could very
    //well have been a bool, but i
    //dont use bools often. w is merely to
    //stop printing after a certain number
    //of times so that the person running
    //the program has a chance to read the
    //output.

    int main(){

    printf("Prime Numbers:\n");

    system("PAUSE");

    printf("1\n2\n");
    while ( i > 2 ){
    z=0;
    x= i;
    prime();
    i = ++i;
    if ( w > 21 ){
    system("PAUSE");
    w = 1; }
    }
    }

    void prime (){ // the actual prime function.
    //pretty straightforward.

    while ( x != 2 ){

    y = i % (x-1);
    x = --x;

    if ( y == 0 ){
    x = 2;z = 1;}

    }

    if ( z == 0 ){
    printf("%d\n", i); w++;}

    }
    Last edited by ...; 03-06-2002 at 09:45 AM.

  7. #7
    Unregistered
    Guest
    Revised - My main problem is that I'm a little confused by arrays, What I need is to calaculate the 1st 100 (ignoring 1 and 2), store them in an array, then to print them out.


    #include <iostream>
    #include <string>
    using namespace std;

    int main() {


    bool Prime [101];

    for ( int j = 2; j <= i-1; j++);
    { if (i % j == 0);
    return false;}
    return true;}

    int count = 0;

    for (i = 3; count < 100; i+=2);


    cout << Prime << endl;

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    12
    #include<iostream>
    using namespace std;

    void main()
    {
    int count=1,i=0, n=0, j=0;
    while (count <=100)
    {
    n=0;
    i +=1;
    for (j=1 ; j<=i ; j++ )
    {
    if ( i%j == 0)
    {
    n +=1;
    }
    }
    if (n <=2 && n != 0)
    {
    cout<<i<<"\n";
    count +=1;
    }

    }
    cout<<"\n\nPress Enter to close this Window";
    char m; cin>>m;
    }

  9. #9
    Unregistered
    Guest
    Thanks for your help so far guys. I'm not offended by your comments. I'm new to C++ and any comments you post will help me along. I posted here just for that purpose. I've read a lot of the posts on this site, and the help available here is priceless. I can't wait for the day when I know enough C++ to be able to help others, as you guys do. Thanks Again

  10. #10
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    change

    bool Prime[101];

    to

    int Prime[101];

    and make another variable (lets make it 'a')

    int a = 1;

    then, in your code, instead of returning true or false, set i equal to Prime[a] and then do ++a.

    then when you print it, set 'a' back equal to 1, and then loop it while incrimenting a and printing. like so:

    a = 1;
    while ( a <= 100 ) {
    cout << Prime[a] << endl;
    ++a; }


    as a side note, take a look at this section of code:

    for ( int j = 2; j <= i-1; j++);
    { if (i % j == 0);
    return false;}
    return true;}

    notice that there are only 3 brackets total. for every bracket, you must have a closing bracket. there should be an opening bracket after the if statement. like so:

    for ( int j = 2; j <= i-1; j++);
    {
    if (i % j == 0); {
    return false;}
    return true;
    }

    also, there must be a closing bracket at the end of your code, to close your main statement.

    another suggestion. at the very end of your code, right before you close your main statement, you should probably put something to stop your code from ending before the user can read the information. i suggest something along the lines of

    int x;
    cin >> x;

    that way, the user has to input something, and once he does, the code finishes.

    good luck.

  11. #11
    Unregistered
    Guest
    Thanks again you guys.

  12. #12
    Registered User Sekti's Avatar
    Join Date
    Feb 2002
    Posts
    163

    if you want to help others...

    Register here and become a regular?
    +++
    ++
    + Sekti
    ++
    +++

Popular pages Recent additions subscribe to a feed