Author |
Message |
jamonathin
![](http://compsci.ca/v3/uploads/user_avatars/57683465145f851a43dd9a.gif)
|
Posted: Mon May 02, 2005 8:44 am Post subject: 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 . . .
c++: |
#include <iostream>
int num = 1, count = 1;
int calculate ()
{
int store [num];
for (int i (1); i <= num; i++)
{
for (int q (1); q <= num; q++)
{
if (i * q == num)
{
store [count] = q;
count += 1;
store [count] = i;
count += 1;
}
}
}
std::cout << "The factors of " << num << " are:\n";
for (int w (1); w <= count/ 2; w+=2)
{
std::cout << " " << store [w] << " x " << store [w+1] << "\n";
}
int wait;
std::cin >> wait;
return 0;
}
int main ()
{
std::cout << "Enter any number to find its factors.\n";
std::cin >> num;
std::cout << "\n Please Wait . . .\n\n";
calculate ();
} |
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
wtd
|
Posted: Mon May 02, 2005 10:57 am Post subject: (No subject) |
|
|
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.
c++: | #include <vector>
std::vector<int> factors(int number)
{
// ...
} |
![Smile Smile](http://compsci.ca/v3/images/smiles/icon_smile.gif) |
|
|
|
|
![](images/spacer.gif) |
jamonathin
![](http://compsci.ca/v3/uploads/user_avatars/57683465145f851a43dd9a.gif)
|
Posted: Mon May 02, 2005 1:10 pm Post subject: (No subject) |
|
|
I understand what you mean (your concept), I just don't know how I would call calculate now. I have . . .
c++: |
std::vector<int> calculate (int numb)
|
But I don't know how to make calculate work, like this doesn't work.
![Confused Confused](http://compsci.ca/v3/images/smiles/icon_confused.gif) |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Mon May 02, 2005 6:54 pm Post subject: (No subject) |
|
|
jamonathin wrote: I understand what you mean (your concept), I just don't know how I would call calculate now. I have . . .
c++: |
std::vector<int> calculate (int numb)
|
But I don't know how to make calculate work, like this doesn't work.
![Confused Confused](http://compsci.ca/v3/images/smiles/icon_confused.gif)
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. |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Mon May 02, 2005 7:48 pm Post subject: (No subject) |
|
|
Take a look at this implementation, and if you have any questions, feel free to ask.
c++: | #include <iostream>
#include <vector>
bool is_prime(int number)
{
for (int i(2); i <= number / i; ++i)
{
if (number % i == 0)
{
return false;
}
}
return true;
}
std::vector<int> factors(int number)
{
std::vector<int> results;
while (!is_prime(number))
{
bool found(false);
for (int i(2); i <= number / i && !found; ++i)
{
if (number % i == 0)
{
results.push_back(i);
number /= i;
found = true;
}
}
}
results.push_back(number);
return results;
}
int main()
{
int num;
std::cin >> num;
std::vector<int> f(factors(num));
for (std::vector<int>::iterator i(f.begin()); i != f.end(); i++)
{
std::cout << *i << std::endl;
}
return 0;
} |
|
|
|
|
|
![](images/spacer.gif) |
jamonathin
![](http://compsci.ca/v3/uploads/user_avatars/57683465145f851a43dd9a.gif)
|
Posted: Tue May 03, 2005 8:47 am Post subject: (No subject) |
|
|
I'm just going to say what I think the main things are (what I think they mean).
bool is_prime(int number) - Means its a boolean function that's an array (from a previous number and the array size can change with whatever int number is used with the function)
if (number % i == 0) - No idea. I tried the % in a different program, and I normally aways got 0 and the odd 1.
while (!is_prime(number)) - Nada clue
results.push_back(number); - Kinda get it, maybe a little explanation could help.
And lastly . .
for (std::vector<int>::iterator i(f.begin()); i != f.end(); i++)
- Not really sure how that works out Are you varing that vector then, and keep varing it, or are you just calling it for every i value. And not too sure what != means. |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Tue May 03, 2005 3:05 pm Post subject: (No subject) |
|
|
c++: | bool is_prime(int number) |
Is a function which takes an int as an argument and returns true or false.
% is the modulo operator. It finds the remainder in integer division.
c++: | while (!is_prime(number)) |
Do the following while "number" is not prime.
We create an iterator, then advance it each time until it's at the end of the vector. != is "not equal". |
|
|
|
|
![](images/spacer.gif) |
jamonathin
![](http://compsci.ca/v3/uploads/user_avatars/57683465145f851a43dd9a.gif)
|
Posted: Wed May 04, 2005 7:02 am Post subject: (No subject) |
|
|
Ohhh ok, sounds good man, all makes sense, thanks ![Very Happy Very Happy](http://compsci.ca/v3/images/smiles/icon_biggrin.gif) |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
|