Problem One Consider the following code in Appendix A (page 2). Using this code,
ID: 3769303 • Letter: P
Question
Problem One
Consider the following code in Appendix A (page 2).
Using this code, write a flow chart that describes what this code does. Also, write another flow chart showing how the Euclidian Algorithm calculates the GCD. Then, write the program.
Problem Two
Write a program that counts the maximum arrangements of writing out the words: ‘box’, ‘book’, and ‘toss’. You must consider repetition.
Output shall include each arrangement, as well as the sequential number and Total.
For example “HE”
(1)EH
(2)HE
Total = ”2”
Explanation / Answer
Solution 1:
Algorithm for Euclidean to find GCD(A,B) is as follows:
If A = 0 then GCD(A,B) = B, Since the GCD(0,B)=B, and we will stop the process.
If B = 0 then GCD(A,B)=A, since the GCD(A,0)=A, and we will stop the process.
Write A in quotient remainder form (A = BQ + R)
Find GCD(B,R) using Euclidean Algorithm since GCD(A,B) = GCD(B,R).
C Program:
// C Program to Find GCD of two given numbers Using Recursive Euclid Algorithm
#include <stdio.h>
int gcd_algorithm(int x, int y)
{
if (y == 0) {
return x;
} else if (x >= y && y > 0) {
return gcd_algorithm(y, (x % y));
}
}
int main(void)
{
int num1, num2, gcd;
printf(" Enter two numbers to find gcd using Euclidean algorithm: ");
scanf("%d%d", &num1, &num2);
gcd = gcd_algorithm(num1, num2);
if (gcd)
printf(" The GCD of %d and %d is %d ", num1, num2, gcd);
else
printf(" Invalid input!!! ");
return 0;
}
Output:
Enter two numbers to find gcd using Euclidean algorithm: 12 20
The GCD of 12 and 20 is 4
------------------------------------------------------------------
Solution: 2
Program:
// C program to print all permutations with duplicates allowed
#include <stdio.h>
#include <string.h>
/* Function to swap values at two pointers */
void swap(char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
void permute(char *a, int l, int r)
{
int i;
if (l == r)
{
int count;
printf("(%d) %s ",count, a);
count++;
}
else
{
for (i = l; i <= r; i++)
{
swap((a+l), (a+i));
permute(a, l+1, r);
swap((a+l), (a+i)); //backtrack
}
}
}
/* Driver program to test above functions */
int main()
{
char str[] = "BOOK";
int n = strlen(str);
permute(str, 0, n-1);
return 0;
}
OUTPUT:
(1) BOOK
(2) BOKO
(3)BOOK
(4) BOKO
(5) BKOO
(6) BKOO
(7) OBOK
(8) OBKO
(9) OOBK
(10) OOKB
(11) OKOB
(12) OKBO
(13) OOBK
(14) OOKB
(15) OBOK
-------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.