0

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 :)

4
  • Please also share the code using the function Commented May 12, 2016 at 19:08
  • 2
    why for(j=0; j>=0; j--) loop, doesn't it just execute once since j is -1 after the first loop? Commented May 12, 2016 at 19:52
  • How big is the f array that's passed to the function? Commented May 12, 2016 at 19:57
  • Given the above code, f[0] = 1 since any number %1 == 0, then since your j loop executes only once, temp will equal mult / 1 and then only if b == temp + 1 or temp - 1, or 1 - temp will your sum1 and sum2 values be set Commented May 12, 2016 at 20:16

1 Answer 1

1
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; // here z == 0

   for(i = 1; i <= mult; i++)
   {
      if(mult % i == 0)
      {
         f[j] = i;   // Any # % 1 == 0, so f[0] = 1
         j++;        // note: z is still == 0 here
      }   
   }

   for(j = 0; j >= 0; j--)   // This loop executes only once
   {
      temp = mult / f[j]; // f[0] == 1, so temp == mult here
      // since z==0 here, f[0] == 1
      // so only if: b == (temp + 1) || (temp - 1) || (1 - temp) 
      if(temp + f[z] == b || temp - f[z] == b || f[z] - temp == b)
      {
         sum1 = f[z];   // z is still 0, so f[0] is 1, sum1 == 1
         sum2 = temp;   // temp == mult here, so sum2 == mult
      }
   }
} 

Not sure if this function is working how you expect it to, but unless those conditions for the variable b are met, then sum1 and sum2 are never getting set, and if they ARE, sum1 should be being set to 1, and sum2 should be temp or mult

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.