the objective of the code below is to take user entered variables (a and c), multiply them (a*c), find the factors of this number and then find which of these factors add up to another user entered variable (in this case, b). For some reason, it is not compiling correctly and I cannot understand why. When I try to use the command cout << sum1 << endl << sum2 << endl;it displays wild, unpredictable numbers. Can anyone tell me if there is something wrong with my code? This is also a void funtion that returns the values sum1 and sum2 (created in the function) to main. The variables a, b, c, and f[] are all defined in main. Here is the function declaration:
void findFactors(int, int, int, int[], int&, int&);
And the function itself:
void findFactors(int a, int b, int c, int f[], int& sum1, int& sum2)
{
int j=0;
int mult=a*c;
int i, z=j, temp;
for(i=1; i<=mult; i++)
{
if(mult%i==0)
{
f[j]=i;
j++;
}
}
for(j=0; j>=0; j--)
{
temp=mult/f[j];
if(temp+f[z]==b || temp-f[z]==b || f[z]-temp==b)
{
sum1 = f[z];
sum2 = temp;
}
}
}
If anyone can help me out, it would be greatly appreciated. Thanks :)
for(j=0; j>=0; j--)loop, doesn't it just execute once sincejis-1after the first loop?farray that's passed to the function?f[0] = 1since any number%1 == 0, then since yourjloop executes only once,tempwill equalmult / 1and then only ifb == temp + 1 or temp - 1, or 1 - tempwill yoursum1andsum2values be set