
-----------------------------------
jamonathin
Mon May 02, 2005 8:44 am

First Unassisted Program
-----------------------------------
This is the first program I've written without the "assistance" of another.  All it does is calculate the factors of a number.  Although I do give my thanks to wtd and martin for their help with other crap such as the following  . . .

#include 

int num = 1, count = 1;

int calculate ()
{
    int store 

-----------------------------------
wtd
Mon May 02, 2005 10:57 am


-----------------------------------
My first thought:

Don't use global variables.  If you need a piece of data to pass from one function to another, pass it an argument.  Then simply calculate the factors, put them into a vector (or other data structure), and return that so the rest of the program can do what it will with that data.

#include 

std::vector factors(int number)
{
   // ...
}

:)

-----------------------------------
jamonathin
Mon May 02, 2005 1:10 pm


-----------------------------------
I understand what you mean (your concept), I just don't know how I would call calculate now.  I have . . .

std::vector calculate (int numb)  

But I don't know how to make calculate work, like this doesn't work.

calculate (num); 

 :?

-----------------------------------
wtd
Mon May 02, 2005 6:54 pm


-----------------------------------
I understand what you mean (your concept), I just don't know how I would call calculate now.  I have . . .

std::vector calculate (int numb)  

But I don't know how to make calculate work, like this doesn't work.

calculate (num); 

 :?

If you implement it correctly it will work, but the thing to understand is that it won't be doing anything immediately apparent itself.  Instead it'll be generating and returning a collection of ints which represent the factors of the input number.  

You could then loop over them to output those numbers.

-----------------------------------
wtd
Mon May 02, 2005 7:48 pm


-----------------------------------
Take a look at this implementation, and if you have any questions, feel free to ask.

#include 
#include 

bool is_prime(int number)
{
   for (int i(2); i  num;

   std::vector f(factors(num));

   for (std::vector::iterator i(f.begin()); i != f.end(); i++)
   {
      std::cout 