Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello
My c++ code is not working
There are no compilation error in my program.
The few lines of my code which asks the user to put value of T A and B is working but there output coming

Basically My code is to get the GCD of (A,B)

I have used Debugger but can not find the flaw.
Only thing I can find is that after reverse(line 27) function there is some error.

What I have tried:

C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void Divisor(int n,vector<int>vec)
{
    for(int i=1;i<n;i++)>
    {
        if(n%i==0)
        {
            vec.push_back(i);// It will be already sorted ha
        }
    }
}

int main()
{
    vector<int>vec1;
    int T;
    int A;
    int B;
    cin >> T;
    for(int j=0;j<t;++j)>
    {
        cin >> A >> B;

        Divisor(A,vec1);

        reverse(vec1.begin(),vec1.end());//LINE 27

        for(int k=0;k<vec1.size();k++)>
        {
            if(B%vec1.at(k)==0)
            {
                cout << vec1.at(k) << endl;
                break;
            }
        }
    }
}
Posted
Updated 6-Jul-16 4:53am
v2
Comments
CPallini 6-Jul-16 6:43am    
What is 'reverse'?
Patrice T 6-Jul-16 7:38am    
Explain the error you get.

"there is some error" tells us nothing - anything could be happening and we have no idea what! Saying that is like your car breaking down in the middle of nowhere, then when you call the garage just saying "it broke!" and ending the call. How long do you think you will be waiting before they arrive with the right parts?

So use the debugger.
Put a breakpoint at the start of the function, run your app in the debugger, and step through each line in turn. Use the debugger to look at what is in what variable.
Before you execute each line, work out what you expect to happen first, then execute the line and see what did happen. Was it what you expected? If so, move on. If not ... why not? What was different? What happened that you didn't expect, or didn't happen that you did?

Debugging is a skill: you only develop it by using it. And it's a lot better to develop it on a trivial code sample like this than on a 100,000 line complete project!
Give it a try: see what information you can find out!
 
Share this answer
 
The way you calc the GCD is complicated and a waste of resources.
Collecting all divisors of a number takes a lot of time.

Bug: You forgot that n is divisor of itself !
C++
void Divisor(int n,vector<int>vec)
{
    for(int i=1; i<n>+1; i++)>
    {
        if(n%i==0)
        {
            vec.push_back(i);// It will be already sorted ha
        }
    }
}</n></int>

Greatest common divisor - Wikipedia, the free encyclopedia[^]

[UpDate]
The problem with your program is that the bigger the numbers, longer it takes to get the answer. It is O(n).
The right way to do it is by taking advantage of the mathematical relation between the 2 numbers and the GCD.
The 2 numbers are multiples of the GCD, it imply that the difference between the 2 numbers is also a multiple of the GCD.
This means that GCD(A,B) <=> GCD(B, A%B)
This
C++
int GCD(int A, int B)
{
    int C;
    C= A % B;
    if (C == 0)
        return B;
    return GCD(B, C);
}

is a recursive version.
 
Share this answer
 
v3
You are passing the vector by value to your Divisor function:
void Divisor(int n, vector<int> vec)

When calling the function, a new vector instance is created and filled with the data of the passed vector. But the content of the passed vector is not changed.

To change the content of the passed vector, pass it by pointer or reference:
void Divisor(int n, vector<int>& vec)
 
Share this answer
 
 
Share this answer
 
Try this one...
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void Divisor(int n,vector<int>&vec)
{
    for(int i=n;i>0;i--)
    {
        if(n%i==0)
        {
            vec.push_back(i);            
        }
    }   
}
 
int main()
{
    vector<int>vec1;
    int T;
    int A;
    int B;
    cin >> T;
    for(int j=0;j<t;++j)>
    {
        cin >> A >> B; 
       Divisor(A,vec1);
        for(int k=0;k<vec1.size();k++)>
        {
            if(B%vec1.at(k)==0)
            {
                cout << vec1.at(k) << endl;
                break;
            }
        }
    }
}
 
Share this answer
 
v3
Comments
Richard MacCutchan 6-Jul-16 10:03am    
If you are going to post code, then please format it properly.
Leo Chapiro 6-Jul-16 10:27am    
Is it not the same what has sudgested Jochen Arndt in his Solution 3?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900