Question

Can someone help me write a small clean "helper" function which accepts two arguements, finds the...

Can someone help me write a small clean "helper" function which accepts two arguements, finds the greatest common divisor, and returns it? DO NOT use recursion. This is to be written in C++. I would be greatful if you wrote a small driver main() to test it as well.

Thanks in advance.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

int gcd(int n1, int n2);
int helper(int n1, int n2);

int main()
{
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);

printf("GCD of %d and %d is %d.", n1, n2, gcd(n1,n2));
return 0;
}

int gcd(int n1, int n2)
{
if (n2 != 0)
return helper(n1,n2);
else
return n1;
}
int helper(int n1, int n2)
{
return gcd(n2, n1%n2);
}

Add a comment
Know the answer?
Add Answer to:
Can someone help me write a small clean "helper" function which accepts two arguements, finds the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help writing this program in C++. Thanks so much for your help in advance...

    I need help writing this program in C++. Thanks so much for your help in advance Function 1: Write a function that accepts an integer argument and returns the sum of all the integers from 1 up to the number passed as an argument. For example, if 50 is passed as an argument, the function will return the sum of 1, 2, 3, 4, ... 50. Use recursion to calculate the sum. Demonstrate the function in a program. A sample...

  • Please help me with this python3 assignment. Write an iterative function, count_consonants(), that accepts a string...

    Please help me with this python3 assignment. Write an iterative function, count_consonants(), that accepts a string (that can include upper and lowercase characters) and returns the number of consonants in that string. A consonant is defined as any letter in the English alphabet other than the vowels a, e, i, o, u. For example, if you call the function as follows: count_consonants('correct horse battery staple') the function should return 20.

  • can someone please help me with this. I need to use java. Recursion Write and run...

    can someone please help me with this. I need to use java. Recursion Write and run a program that reads in a set of numbers and stores them in a sequential array. It then calls a recursive function to sort the elements of the array in ascending order. When the sorting is done, the main function prints out the elements of the newly sorted array Hint: How do you sort an array recursively? Place the smallest element of the array...

  • can somebody help me with this exercise 5 Euclidean algorithm The largest common divisor (gcd) of...

    can somebody help me with this exercise 5 Euclidean algorithm The largest common divisor (gcd) of two positive integers p and q can be given by the Euclid's algorithm explained in the lecture will be determined. · Write a function gcdIterative that uses the largest common divisor of p and q Calculates loop structure and returns. Use the pseudocode given in the lecture as a starting point and implement it as directly as possible into a C ++ program. Use...

  • can someone help me solve this question? Thanks in advance!! Each week a retail outlet accepts...

    can someone help me solve this question? Thanks in advance!! Each week a retail outlet accepts delivery of a certain item from 3 different suppliers A, B and C. All the items received are put into an empty bin. A supplies 50% of these items, while B and C each supply 25%. From past experience, it is known that 2% of the items supplied by A are defective, 2% of the items supplied by B are defective and 4% of...

  • can someone help me fix this. The function that finds the minimum and maximum values of...

    can someone help me fix this. The function that finds the minimum and maximum values of the array and outputs the value and index doesnt find the maximum value of the array. #include <iostream> #include <fstream> #include<iomanip> using namespace std; void readArray(ifstream& inF, int arr[], int&ArrSize); void writeArray(ofstream & outF, int arr[], int & ArrSize); void MaxAndMin(ofstream & outF, int arr[], int & ArrSize, int &high, int &low); int main() {    int arr[100];    int i = 0;   ...

  • Can someone help me understand this step by step (C++) Array Shifter and Array Reversal Write...

    Can someone help me understand this step by step (C++) Array Shifter and Array Reversal Write a function that accepts an array of doubles and the array's size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array...

  • can someone please help me write an email for my teacher asking her to proofread my...

    can someone please help me write an email for my teacher asking her to proofread my essay. I was starting to write her an email but I feel like I don't make sense or sounds polite enough. English isn't my first language and I don't feel very confident about it. Thank you in advance for your help!

  • help me solving this both please in c++ language 6 Write function max(int al Lint n)...

    help me solving this both please in c++ language 6 Write function max(int al Lint n) that receives an array of integer and its length, it returns the maximum number in that array. Test your function in main. 7) Write functin reverse (char x[ .int n) that reieves an array of characters and reverse the order of its elements. Test this function in main.

  • Can someone please help me write an email to my teacher? On Wednesday she said that...

    Can someone please help me write an email to my teacher? On Wednesday she said that our essay was due on Friday at 1:00 pm. However, I think she made a mistake and she put a different date on Canvas. She put that our essay is due on Saturday at 1:00 pm. I want to email her to tell her about her miatake and that I'm just letting her know so she can let know my classmate and they submit...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT