| | |
Any better way of implementing this recusively?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2004
Posts: 3
Reputation:
Solved Threads: 0
I have a function that does intermediate calculations to a greatest common divisor deal. and here is how its implemented iteratively (see exteuc() function) .
i tried to implement it recursively too, (see exteuc2() function) . feel free to look at it and give me suggestions if i can improve my recursive implementation in any way. even though it works out. i ask because i'm doing c++ programming after a long long time now.
the function takes 2 numbers and 3 arrays of int size 3 each. copyarr just shifts the contents of arrays left by 1. its needed because the array is of size 3 and as i only need 3 values at one time to do calculations on.
edit : i thought i'ld add copyarr function.
here
void copyarr(int* p)
{
p[0] = p[1];
p[1] = p[2];
}
i tried to implement it recursively too, (see exteuc2() function) . feel free to look at it and give me suggestions if i can improve my recursive implementation in any way. even though it works out. i ask because i'm doing c++ programming after a long long time now.
the function takes 2 numbers and 3 arrays of int size 3 each. copyarr just shifts the contents of arrays left by 1. its needed because the array is of size 3 and as i only need 3 values at one time to do calculations on.
C++ Syntax (Toggle Plain Text)
int exteuc2(int num1, int num2, int* x, int* y, int* r) { int q, tmp; r[1] = (num1 * x[1]) + (num2 * y[1]); tmp = r[1]; if (tmp > 0) { q = r[0] / r[1]; x[2] = x[0] - (q * x[1]); y[2] = y[0] - (q * y[1]); copyarr(x); copyarr(y); copyarr(r); exteuc2(num1, num2, x, y, r); } return q; } int exteuc(int num1, int num2, int* x, int* y, int* r) { int q; while (( r[1] = (num1 * x[1]) + (num2 * y[1])) != 0) { q = r[0] / r[1]; x[2] = x[0] - (q * x[1]); y[2] = y[0] - (q * y[1]); copyarr(x); copyarr(y); copyarr(r); } return q; }
edit : i thought i'ld add copyarr function.
here
void copyarr(int* p)
{
p[0] = p[1];
p[1] = p[2];
}
![]() |
Similar Threads
- help implementing singly linked list (C++)
- need some help implementing a "continue or quit" prompt (Python)
- Implementing Active Directory (Computer Science)
- Implementing paging in pop window (C#)
- Implementing Random Images (Java)
- Source code implementing Latent Semantic Indexing (Java)
- URGENT: Implementing search with multiple dissimilar MySQL tables (MySQL)
- help required for implementing project using JAVA & XML (Java)
Other Threads in the C++ Forum
- Previous Thread: C++ Address Book
- Next Thread: Windows Api Tutorial
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





