Page 2 of 6 FirstFirst 123456 LastLast
Results 16 to 30 of 88

Thread: C++ Help

  1. #16
    Posts Occur in Real Time edczxcvbnm's Avatar
    Join Date
    Aug 2000
    Location
    The World
    Posts
    7,920

    Default

    I could do it if I knew what it looked like but I told you what to do. Print out the logo and get some graph paper and plot the logo. It isn't rocket science. I doubt it has to be perfect. That or do something easier such as the Samus Metroid symbol. It is straight lines and perfect circles.

  2. #17
    Old-Ones Studios Cruise Control's Avatar
    Join Date
    Apr 2005
    Location
    Seattle, WA
    Posts
    3,799

    Default

    I got them and I'll post them later. But now I need an idea of how to use a for loop to check for prime numbers. I don't think I can use an if statement in a loop checking for predertimined values.

    EX:
    I cant do
    Code:
    for (int cd=0; cd<100; cd++)
    {
     if (n =2||3||5||7||11||13||17||19||23||29||31||37||41||43||47||53||59||61||67||71||73||79||83||89||97);
      cout << I'm sorry, " << n << "is not an integer.";
    }
    So does anyone have any ideas.
    Leave some shards under the belly
    Lay some grease inside my hand
    It's a sentimental jury
    And the makings of a good plan

  3. #18
    Old school, like an old fool. Flying Mullet's Avatar
    Join Date
    Apr 2003
    Location
    Napping in a peach tree.
    Posts
    19,185
    Articles
    6
    Blog Entries
    7
    Contributions
    • Former Administrator
    • Former Cid's Knight
    • Former Senior Site Staff

    Default

    This is where Google becomes your best friend. I would suggest searching for examples on the web. I'm sure that someone has done this before.

    EDIT: I know it's Java, but the languages are very similar. Try searching at http://forum.java.sun.com/index.jspa. I searched on "prime number" and it returned plenty of results. Perhaps one will be able to help you.
    Figaro Castle

  4. #19
    Old-Ones Studios Cruise Control's Avatar
    Join Date
    Apr 2005
    Location
    Seattle, WA
    Posts
    3,799

    Default

    You can use google for something besides porn!

    EDIT1: I figured it out all by myself.

    Code:
    int main()//Initializes program
    {
     int cdn;     //Countdown start #
     int mr1;      //Number stored from modulus 2
     int mr2;      //Number stored from modulus 3
     int mr3;      //Number stored from modulus 5
     int mr4;      //Number stored from modulus 7
     for (int cdn=1; cdn<101; cdn++)
     {
      mr1=cdn%2;
      mr2=cdn%3;
      mr3=cdn%5;
      mr4=cdn%7;
      if (mr1!=0&&mr2!=0&&mr3!=0&&mr4!=0)
      {
       cout << cdn << ", ";
       outfile << cdn
      }
     }
     getch();
     return 0;
    }
    EDIT2: The coordinates for your Zanarkand Abes Logo are:
    moveto (10, 30);
    lineto (40, 0);
    lineto (60, 10);
    lineto (70, 30);
    lineto (80, 10);
    lineto (100, 0);
    lineto (130, 30);
    lineto (100, 50);
    lineto (110, 30);
    lineto (100, 20);
    lineto (80, 40);
    lineto (80, 50);
    lineto (90, 40);
    lineto (100, 90);
    lineto (70, 140);
    lineto (30, 60);
    lineto (60, 80);
    lineto (50, 80);
    lineto (70, 110);
    lineto (80, 90);
    lineto (60, 50);
    lineto (50, 40);
    lineto (60, 40);
    lineto (60, 30);
    lineto (40, 20);
    lineto (30, 30);
    lineto (40, 50);
    lineto (10, 30);
    floodfill(11, 30, fg);

    Thanks for the graphing idea edczxcvbnm .
    Last edited by Cruise Control; 02-17-2006 at 05:00 PM.
    Leave some shards under the belly
    Lay some grease inside my hand
    It's a sentimental jury
    And the makings of a good plan

  5. #20
    Prinny God Recognized Member Endless's Avatar
    Join Date
    Aug 2000
    Location
    Prinny Moon
    Posts
    2,641
    Contributions
    • Former Cid's Knight

    Default

    Quote Originally Posted by bbomber72000
    I got them and I'll post them later. But now I need an idea of how to use a for loop to check for prime numbers. I don't think I can use an if statement in a loop checking for predertimined values.
    So does anyone have any ideas.
    It should be obvious that you are to use what you were asked to do before, which is, to test if a number is an integer.
    It's not actual c code, but you should be able to adapt it.

    "number" = the number inputed.

    Code:
    bool isprime=true;
    if ((number/2)-int(number/2) == 0.0)
        {
         isprime = false;
        }
    for (int i=3, i<=int(sqrt(number))+1 && isprime, i+2) /* We only need to test up to the square root for divisions, 
                                                             since it's symmetrical. Also, if the number is a multiple of 2,
                                                             then we don't bother with the loop (isprime == false). */
        {
             if ((number/i)-int(number/i) == 0.0) /* Is the result of the division an integer? */
                  {
                   isprime = false; /* Result is an integer which means that i is a factor of number. 
                                       Also exits the loop */
                  }
        }
    if (isprime)
         {
          cout << "The number " << number << " is prime";
         } else
         {
          cout << "The number " << number << " is not prime";
         }
    Edit: silly me, I forgot to comment. *adds comments to code*
    Alternatively, you can test if "number mod i" == 0. If it's the case, it's not prime.

    Edit2: Optimized the code to cut by two the number of iterations.
    Last edited by Endless; 02-18-2006 at 12:28 AM.

    And then there is Death

  6. #21
    Golden Boy Mr. Wertz's Avatar
    Join Date
    Apr 2005
    Location
    Where ever I wanna live!
    Posts
    20

    Default

    //Bryan Wertz II
    //February 10, 2006
    //Terminal 1, Intermediate Computer Prgramming.
    //Switch Program


    //These are my header files.
    #include <iostream.h>
    #include <conio.h>
    #include <fstream.h>
    #include <iomanip.h>
    #include <string.h>


    //This is the start of my program.
    int main()
    {
    //prints to the text file.
    ofstream outfile ("PrimeNumberProgram.txt");

    //Declaring my variables.
    int countdown; //number that starts the countdown.
    int remainder2; //number that is stored from modulus 2.
    int remainder3; //number that is stored from modulus 3.
    int remainder5; //number that is stored from modulus 5.
    int remainder7; //number that is stored from modulus 7.

    cout << "1,\n2,\n3,\n5,\n7,\n";
    outfile << "1,\n2,\n3,\n5,\n7,\n";
    // counting to 100 using a for loop.
    for (int countdown=11; countdown<101; countdown++)
    {
    remainder2=countdown%2;
    remainder3=countdown%3;
    remainder5=countdown%5;
    remainder7=countdown%7;
    if (remainder2!=0&&remainder3!=0&&remainder5!=0&&remainder7!=0)
    {
    cout << countdown << ",\n";
    outfile << countdown << ",\n";
    }
    }

    //That stuff at the end.
    getch();
    return 0;
    }

  7. #22
    Prinny God Recognized Member Endless's Avatar
    Join Date
    Aug 2000
    Location
    Prinny Moon
    Posts
    2,641
    Contributions
    • Former Cid's Knight

    Default

    Is your program supposed to give you a list of prime numbers up to 101, or check if a particular number is prime?
    If it's the former, testing for mod2 is useless, and your for loop should go from odd number to odd number (even numbers can't be prime, what's the point of testing them?). Also, as long as your loop is below 25, mod5 is useless, and as long as it's below 49, mod7 is useless aswell. And last, were you specifically told to list them up to 101, or did you choose that as a random stopping point?
    If it's the latter, my pseudo-program is better. In fact, I'm confident I can make a prime number generator using my code that takes any stopping point as input, whereas yours needs to be rewritten each time.

    And then there is Death

  8. #23
    Old-Ones Studios Cruise Control's Avatar
    Join Date
    Apr 2005
    Location
    Seattle, WA
    Posts
    3,799

    Default

    This is now the official thread for intermediate C++ students to help each other.

    Dreaper, do you know why this doesn't work, the != doesn't make a difference.
    Code:
    //Chris Taylor
    //February 22, 2006
    //Terminal 6, Intermediate Computer Prgramming.
    //Switch Program
    
    
    //These are my header files.
    #include <conio.h>
    #include <fstream.h>
    #include <iomanip.h>
    #include <iostream.h>
    
    //This is the start of my program.
    int main()
    {
     //prints to the text file.
     ofstream outfile ("500.txt");
    
     //Declaring my variables.
     int cd=5; //number that starts the countdown.
     int r1;   //number that is stored from division of 5.
     int r2;   //number that is stored from modulus of r1.
     int r3;   //number that is stored from division of 7.
     int r4;   //number that is stored from modulus of r3.
     while (cd<500)
     {
      r1=cd/5;
      r2=r1%5;
      r3=cd/7;
      r4=r3%7;
    
      if (r2==0||r4==0)
       cout << cd << ", ";
       cd++;
     }
    
     //That stuff at the end.
     getch();
     return 0;
    }
    EDIT: It works now.
    Code:
    int main()
    {
     ofstream outfile ("PN.txt");
    
     int cd=5; //number that starts the countdown.
     int r1;   //number that is stored from modulus of r1.
     int r2;   //number that is stored from modulus of r3.
    
     while (cd<500)
     {
      r1=cd%5;
      r2=cd%7;
    
      if (r1==0||r2==0)
       cout << cd << ", ";
       cd++;
     }
    
     getch();
     return 0;
    }
    Last edited by Cruise Control; 02-23-2006 at 04:21 PM.
    Leave some shards under the belly
    Lay some grease inside my hand
    It's a sentimental jury
    And the makings of a good plan

  9. #24
    Old-Ones Studios Cruise Control's Avatar
    Join Date
    Apr 2005
    Location
    Seattle, WA
    Posts
    3,799

    Default

    Since the user is able to enter a value less than, what happens if they enter a max count of less than 5? Our starting value IS 5. We wouldn't want it to run anyway. Not to mention Do While loops are gay.
    Leave some shards under the belly
    Lay some grease inside my hand
    It's a sentimental jury
    And the makings of a good plan

  10. #25
    Golden Boy Mr. Wertz's Avatar
    Join Date
    Apr 2005
    Location
    Where ever I wanna live!
    Posts
    20

    Default

    no idea what that means, dee dee dee.

  11. #26
    Old-Ones Studios Cruise Control's Avatar
    Join Date
    Apr 2005
    Location
    Seattle, WA
    Posts
    3,799

    Default

    x.x Just write it.
    Leave some shards under the belly
    Lay some grease inside my hand
    It's a sentimental jury
    And the makings of a good plan

  12. #27
    Golden Boy Mr. Wertz's Avatar
    Join Date
    Apr 2005
    Location
    Where ever I wanna live!
    Posts
    20

    Default

    that isnt a answer though.

    It makes no sense.

  13. #28
    Old-Ones Studios Cruise Control's Avatar
    Join Date
    Apr 2005
    Location
    Seattle, WA
    Posts
    3,799

    Default

    Yes it is, it explains the reasoning.
    Leave some shards under the belly
    Lay some grease inside my hand
    It's a sentimental jury
    And the makings of a good plan

  14. #29
    Prinny God Recognized Member Endless's Avatar
    Join Date
    Aug 2000
    Location
    Prinny Moon
    Posts
    2,641
    Contributions
    • Former Cid's Knight

    Default

    Your code doesn't list primes, only multiples of 5 and 7.

    And then there is Death

  15. #30
    ORANGE Dr Unne's Avatar
    Join Date
    Dec 1999
    Posts
    7,394
    Articles
    1
    Contributions
    • Former Administrator
    • Former Developer
    • Former Tech Admin

    Default

    Quote Originally Posted by bbomber72000
    Since the user is able to enter a value less than, what happens if they enter a max count of less than 5? Our starting value IS 5. We wouldn't want it to run anyway. Not to mention Do While loops are gay.
    While, do-while, for, etc. are all equally powerful. You can use any of them to construct the equivalent of any of the others. (Generally, depending how a language is implemented.) All loops are just syntactic sugar over a LOOP / GOTO / END construct when it comes down to it.

    Since this is apparently a general C++ thread now, I'll offer some other advice. r1, r2, r3 etc. are very bad choices for variable names. If something stores the modulus of 5, call it at bare minimum "modulus_of_5". If something is a countdown variable, "countdown" is much better than "cd". If you named your variables like this, you wouldn't even need most of the comments you have in your code there. We call it "self-documenting".

    Comments should never say WHAT your code is doing. If you can't tell what you're doing in the code just by reading the code, the code probably needs to be rewritten or clarified. Comments should say things like 1) Overall purpose / summary of entire blocks or functions 2) WHY you're doing something (not WHAT you're doing or HOW you're doing it), 3) Anything happening that's subtle or not immediately obvious, 4) Assumptions that are being made about input/output, which your code relies upon. Endless has good comments above. Source code is meant for human beings to read. Machines only understand binary. Good code = easy to read code.

    Another piece of advice: if something is a constant (modulus of 5, etc) you should make it a const variable in your code. It prevents accidentally changing the value of the variable. Ideally your program should crash any time anything happens that you didn't anticipate; declaring things as constants is a good way to crash your program. It forces you to correct the problem, and makes it easier to find it to debug. Silent bugs are the worst kind.

    So far as finding prime numbers, you could always use the Sieve of Eratosthenes. It's a very simple, famous algorithm for finding primes. I just wrote up a C++ version of it, but then I realized everyone should have the opportunity to enjoy writing it themselves.

    *wanders off*

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •