Isolating numbers in a for statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Geddes
    New Member
    • Feb 2007
    • 4

    Isolating numbers in a for statement

    I'm making a program which factors a number which is given by a user. I've gotten a way to output the factors using a for statement, but i would like, if possible to isolate the numbers which are given as factors so i can use them at a later time. I'm a C++ noob, so if there is a simple answer to this, don't be afraid to point it out =)
    Code:
    #include <iostream.h>
    #include <math.h>
    
    using namespace std;
    
    int main()
    {
        cout<<endl;
        cout<<"     Factor Application - Finding factors of #s"<<endl;
        cout<<"     ------------------------------------------"<<endl;
        cout<<endl;
        cout<<endl;
        cout<<"Y = AX^2 + BX + C"<<endl;
        cout<<endl;
        cout<<endl;
        cout<<"Insert the B value: ";
        int bvar;
        cin>>bvar;
        cout<<endl;
        cout<<"Factors: ";
        if(bvar > 1)
        {
        for (int i = 10000; i > 0; i--)
        {
        int xvar;
        xvar = ((bvar/i)*(i));
        if ( xvar == bvar)
        {
        int xyvar[100];
        xyvar[100] = (bvar/i);
        cout<<xyvar[100]<<" ";
        }
        }
        cout<<endl;
        }
        cout<<endl;
    
        system("PAUSE");
        return (0);
        
    }
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    store them in a vector or array?

    Comment

    • Geddes
      New Member
      • Feb 2007
      • 4

      #3
      Originally posted by horace1
      store them in a vector or array?
      array preferably..i don't even know how vectors work.
      if a vector is more efficient or better in some way i'm up to learning it though.

      thanks for the quick response.
      Last edited by Geddes; Feb 10 '07, 07:48 AM. Reason: wrong grammar

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        Originally posted by Geddes
        array preferably..i don't even know how vectors work.
        if a vector is more efficient or better in some way i'm up to learning it though.

        thanks for the quick response.
        if you are working in C++ vectors are preferable in that it will expand dynamically as you add elements - it is also easy to use. Have a look at
        http://www.cprogrammin g.com/tutorial/stl/vector.html

        Comment

        • Geddes
          New Member
          • Feb 2007
          • 4

          #5
          Originally posted by horace1
          if you are working in C++ vectors are preferable in that it will expand dynamically as you add elements - it is also easy to use. Have a look at
          http://www.cprogramming.com/tutorial/stl/vector.html
          I read that tutorial, and i think i understand what vectors do; however, i'm still not sure how to apply them to my code. Do I modify the if statement so that my xyvar variable is a vector? Even then, how do I access these numbers later?

          thanks

          Comment

          • Geddes
            New Member
            • Feb 2007
            • 4

            #6
            Actually I think i'm beginning to understand a bit better. If i declare a vector and use the push back command to add every number produced by the factoring code, I should be able to simply access the number later using: vectorname[a number]. Can you correct me if i am wrong?

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              Nope - you've got the basic idea.

              Go ahead and try that to see if it will work for you - come back with any questions.

              Comment

              Working...