I dont understand why this won't work? All three of the variables (factors(array), i, and c) are all integers. This is such an annoying problem as i'm sure there is something obvious i'm missing :s
@Bazzy ... sorry counter is also int and yeah i knew there was something stupid i was missing. As jsmith said counter is never incremented. Thats also why the curly braces were there, i just forgot to put it in lol.
Still getting odd results, i wrote this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
void factorise(int &a, int&b, int&c)
{
int i, num, counter = 0;
int factors[80];
for(i=1;i<=c;i++)
if(!(c%i))
{
factors[counter]= i;
counter++;
}
for(int a=0;factors[a];a++) cout << factors[a] << ' ';
}
and added in that last loop temporarily to make sure it was working but i keep getting odd results. For example, if i put in any number it out puts all the factors first then loads of randome numbers. When c = 12, apparently the factors are: 1, 2, 3, 4, 6, 12, 1999387003, -14076282940, 4469856 - something tells me thats not quite right.
@jsmith ... thanks for reply and tip, that was just silly of me.
ahh bazzy you'r a genius! How long have you been programming for? you seem to have an answer for everything lol.
By the way, just so i'm understanding this correctly, does the line:
if( !(c%i) ) factors[counter++] = i;
mean that if c%i is 0, then it will be added to the factors array, and then counter will be incremented? So if it was changed to factors[++counter] counter would be incremented before the value was added to the array and the array would be have no value at factors[0]?
In your last example, what was the num variable supposed to be for? It seems like it would have been a lot easier if you would have created a main function to show the actual values that you are passing to the function. That is just a recommendation for the future since it appears that you have the answers that you need.
Thanks kempofighter for bringing that to my attention. Basically, i'm writing a maths based program where it will include various different functions and this function is for factorising quadratic expressions. I already have a fair amount of it done and it compiles nicely but this one i knew was going to be a bit tricky so i made it a seperate .cpp file just so its easy to look at and alter. That num variable was simply there because i didn't like what I had previously and forgot to remove it :( <<< n00b lol