HELP -Gcf NON-RECURSIVE FUNCTION

Nov 8, 2013 at 6:37pm
closed account (ivDwAqkS)
I don't know if its right, but I am getting the answer I wanted but now I don't know if I am doing the problem right, can someone tell me please?

This is the exercise:
1
2
3
4
5
6
7
"For experts: Revise the gcf function so that it uses 
an iterative (loop- based) approach. 
Each cycle through the loop should stop if B is zero; 
otherwise, it should set new values for A and B and then continue. 
You’ll need a temporary variable—temp—
to hold the old value of B for a couple of lines: 
temp=b, b=a%b, and a=temp."


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <cstdlib>
using namespace std;

int Gcf(int a, int b);
void Print_gcf(int a, int b);

int main ()
{
    int a = 0, b = 0;

    cout << "Enter a: ";
    cin >> a;
    cout << "Enter b: ";
    cin >> b;

    Print_gcf(a, b);
    cout << "GCF is "<< Gcf(a, b) << endl;
    return 0;
}

 int Gcf(int a, int b)
 {
    int temp;
    temp=b;
    b=a%b;
    a=temp;
    if(temp==0){
        return a;
    }
    else{
        return a%b;
    }
 }

void Print_gcf(int a, int b)
 {
    int temp;
    cout << "GCF (" << a << ") " << "(" << b << ") => " << b <<endl;
    while(b>0){
        temp=b;
        b=a%b;
        a=temp;
        if(temp==0){
            cout << a;
        }
        cout << "GCF (" << a << ") " << "(" << b << ") => " << b <<endl;
    }
 }

Last edited on Nov 8, 2013 at 10:40pm
Nov 8, 2013 at 10:39pm
closed account (ivDwAqkS)
Please I need help D: someone tell me if I am doing it right or not.
Nov 8, 2013 at 10:46pm
The iterative version of Gcf will require a loop (as your instructions indicate.) And presumably, Print_gcf should be calling Gcf.
Last edited on Nov 8, 2013 at 10:48pm
Nov 8, 2013 at 10:53pm
closed account (ivDwAqkS)
but if Print_gcf calls Gcf fuction isn't it recursive? if not, how would the problem could be wrote?
Last edited on Nov 8, 2013 at 11:01pm
Nov 8, 2013 at 11:01pm
but if we call a fuction isn't it recursive?


No. If a function calls itself directly or indirectly, that is recursive. If funcA calls funcB which doesn't, in turn, call funcA either directly or indirectly, then that is not recursive.
Nov 8, 2013 at 11:04pm
closed account (ivDwAqkS)
how would the problem could be wrote then? could you help me out? because when I write Gcf inside it prints until:

GCF(500, 300) => 300
GCF(300, 200) => 200
GCF(200, 100) => 100
GCF(100, 0) => 0

then if says error the program stopped working and it doesn't print the gcf value
Nov 8, 2013 at 11:42pm
I may have mentioned something earlier about avoiding division by zero.
Nov 9, 2013 at 12:23am
closed account (ivDwAqkS)
yes but that was with recursive function if you notice. if I leave it like this is it correct too?
Last edited on Nov 9, 2013 at 12:24am
Nov 9, 2013 at 10:13am
Recursive or iterative, one must still avoid division by zero.
Topic archived. No new replies allowed.