CS 2734
Computer Organization II
If your SSN has last 2 digits in the range 0-33 then write Hanoi below
else if your SSN has last 2 digits in the range 34-66 then write Comb below
else if your SSN has last 2 digits in the range 67-99 then write Writebase below
/* Tower of Hanoi problem: given n discs of decreasing size
* on a peg numbered 1, show how to move them all to peg
* number 2, using peg number 3. The rules state that you
* must not place a disc on a smaller disc, though you can
* place a disc on an empty peg or on a larger disc.
*/
#include <stdio.h>
void hanoi(int n, int start, int finish, int extra);
void main(void)
{
int n;
scanf("%d", &n);
hanoi(n, 1, 2, 3);
}
void hanoi(int n, int start, int finish, int extra)
{
if (n != 0) {
hanoi(n-1, start, extra, finish);
printf("Move disc %d from %d to %d.\n", n, start, finish);
hanoi(n-1, extra, finish, start);
}
}
Along with sample output:
4
Move disc 1 from 1 to 3.
Move disc 2 from 1 to 2.
Move disc 1 from 3 to 2.
Move disc 3 from 1 to 3.
Move disc 1 from 2 to 1.
Move disc 2 from 2 to 3.
Move disc 1 from 1 to 3.
Move disc 4 from 1 to 2.
Move disc 1 from 3 to 2.
Move disc 2 from 3 to 1.
Move disc 1 from 2 to 1.
Move disc 3 from 3 to 2.
Move disc 1 from 1 to 3.
Move disc 2 from 1 to 2.
Move disc 1 from 3 to 2.
/* Number of combinations of n things taken k at a time.
* Use a recursive algorithm to calculate.
*/
#include <stdio.h>
void main()
{ int N, K, C;
printf("Enter values for N and K>>>");
scanf("%d %d", &N, &K);
C = Comb(N, K);
printf("C = Comb(%d, %d) is %d\n", N, K, C);
return;
}
int Comb(int N, int K)
{
printf("Call Comb with N and K = %d %d\n", N, K);
if (N == K || K == 0)
return 1;
else return Comb(N-1, K) + Comb(N-1, K-1);
}
Along with sample output:
Enter values for N and K>>>5
2
Call Comb with N and K = 5 2
Call Comb with N and K = 4 2
Call Comb with N and K = 3 2
Call Comb with N and K = 2 2
Call Comb with N and K = 2 1
Call Comb with N and K = 1 1
Call Comb with N and K = 1 0
Call Comb with N and K = 3 1
Call Comb with N and K = 2 1
Call Comb with N and K = 1 1
Call Comb with N and K = 1 0
Call Comb with N and K = 2 0
Call Comb with N and K = 4 1
Call Comb with N and K = 3 1
Call Comb with N and K = 2 1
Call Comb with N and K = 1 1
Call Comb with N and K = 1 0
Call Comb with N and K = 2 0
Call Comb with N and K = 3 0
C = Comb(5, 2) is 10
#include <stdio.h>
void writebase(int, int);
char *D[] =
{"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
void main()
{
int N, B;
printf("Enter N and B:");
scanf("%d %d", &N, &B);
writebase(N, B);
printf("\n");
}
void writebase(int N, int B)
{
printf("Call Base with N and B = %d %d\n", N, B);
if (N != 0) {
writebase(N/B, B);
printf("%s", D[N%B]);
}
}
Along with sample output:
Enter values for N and B:54790143
16
The values of N and B are: 54790143 16
Call Base with N and B = 54790143 16
Call Base with N and B = 3424383 16
Call Base with N and B = 214023 16
Call Base with N and B = 13376 16
Call Base with N and B = 836 16
Call Base with N and B = 52 16
Call Base with N and B = 3 16
Call Base with N and B = 0 16
34407ff
For checkoff: