5. Write a function, ionah, that takes a single number as input and prints out t
ID: 3734535 • Letter: 5
Question
5. Write a function, ionah, that takes a single number as input and prints out the solution to the inverted disk problem for that many disks. This is the problem of moving a stack of k disks of increasing size from bottom to top, from the first peg to the third peg with another peg that may be used as well, subject to the condition that a smaller disk is never put on top of a larger one, and only one disk may be moved at a time. Here is a sample run: ?- ionah (3) move disk from peg 1 to peg 3 move disk from peg 1 to peg 2 move disk from peg 3 to peg 2 move disk from peg 1 to peg 3 move disk from peg 2 to peg 1 move disk from peg 2 to peg 3 move disk from peg 1 to peg 3 Yes 6. Write a function, sequence, that takes a single integer as input and prints out a list containing that many terms of the sequence defined by: 1ifn-2 Here is a sample execution: ?- sequence (7, X). X-(0,1,2,5,12,29,70 Yes 2-Explanation / Answer
// problem - 5
// ***** C code to solve inverted disk problem ******
#include <stdio.h>
// C recursive function to solve inverted disk problem
void inverted_disk(int n, char from_peg, char to_peg, char ex_peg)
{
if (n == 1)
{
printf(" move disk from peg %c to peg %c", from_peg, to_peg);
return;
}
inverted_disk(n-1, from_peg, ex_peg, to_peg);
printf(" move disk from peg %c to peg %c", from_peg, to_peg);
inverted_disk(n-1, ex_peg, to_peg, from_peg);
}
void ionah(int n){
inverted_disk(n, '1', '3', '2');
}
int main()
{
int n; // Number of disks
printf("Enter n value: ");
scanf("%d",&n);
ionah(n);
return 0;
}
// problem - 6
// ***** C code to solve sequence problem ******
#include <stdio.h>
void sequence(int n,int X[]){
int i;
for(i=1;i<=n;i++){
if(i==1 || i==2)
X[i-1]=i-1;
else
X[i-1]=2*X[i-2]+X[i-3];
}
printf("X = ["); //printing the sequence
for(i=0;i<n-1;i++)
printf("%d,",X[i]);
printf("%d] ",X[n-1]);
}
int main()
{
int n;
printf("Enter an integer: ");
scanf("%d",&n);
int arr[n];
sequence(n,arr);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.