#include <stdio.h>
int josephus(int n, int k)
{
if (n == 1)
return 1;
else
/* The position returned by josephus(n - 1,
k) is adjusted because the
recursive call josephus(n
- 1, k) considers the original position
k%n + 1 as position 1
*/
return (josephus(n - 1, k) + k-1) % n +
1;
}
// Driver Program to test above function
int main()
{
int n = 14;
int k = 2;
printf("The chosen place is %d", josephus(n, k));
return 0;
}
this is a c source code to find the solution of the
"Josephus problem"
but the problem is that this code only gives me the final outcome
only,
what I want is to tweak this code and make it print all the
outcomes from the begining until the final answer
I want the answer with explaination please
Answer:
#include <stdio.h>
int josephus(int n,int k)
{
int a;
if(n==1)
return 1;
else
{
int a;
a=(josephus(n-1,k)+k-1)%n+1;
printf("%d\n",a);
return a;
}
}
int main()
{
int n=14;
int k=2;
printf("The chosen place is %d",josephus(n,k));
return 0;
}
Inorder to print the value at each recursive function call,we
need to store the value into a variable.
We are printing that.And we are returning the stored value after
each function call.
The final value is returned to the main function.
Thank you
#include <stdio.h> int josephus(int n, int k) { if (n == 1) return 1; else...
i need flowchart for the following c code #include <stdio.h> int main() { int n, i, sum = 0; printf("Enter an integer: "); scanf("%d",&n); i = 1; while ( i <=n ) { sum += i; ++i; } printf("Sum = %d",sum); return 0; }
Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int *); void replaceAt(int *, int, int); int isEmpty(int *, int); int isFull(int *, int); void removeAt(int *, int); void printList(int *, int); int main() { int *a; int arraySize=0,l=0,loc=0; int choice; while(1) { printf("\n Main Menu"); printf("\n 1.Create list\n 2.Insert element at particular position\n 3.Delete list.\n4. Remove an element at given position \n 5.Replace an element at given position\n 6. Check the size of...
#include <stdio.h> int main(int argc, char *argv[]) { int i; for (i = argc - 1; i > 0; i--) printf("%s ", argv[i]); printf("\n"); return 0; } can you explain this code in c and why use this function
#include<stdio.h> int functionl (int x, int y); int main() int ij=2,k; for(i=1;i<=5; i++) k = function1(ij); printf("k=%d\n",k); return 0; int function] (int x, int y) int z; z=x*2+y; return z;
Q4) what is the output after the code below is executed? #include <stdio.h> #1 nclude<string.h> int main) char abc[100]-"Good Luck in Your Programming Final" int i, ; while(abc[i]!= '\0'){ if(abc[i]' if(abc[i+1]-'&&abci+1] -'0' else printf"c", abcli-1]) printf("\n w = %d return 0; \n", w); Place you answer here 4 l Page
QUESTIONS What will be the output of the following C code? 1. #include <stdio.h> int maino int 1 - -3; int k - * % 2; printf(\n", k); Compile time error O-1 Implementation defined
#include<stdio.h> int main() { int data[10], i, j, temp; printf("Enter 10 random number to sort in ascending order:\n"); for(i = 0; i < 10; i++) scanf("%d", &data[i]); /* Sorting process start */ ****** INSERT YOUR CODE TO COMPLETE THE PROGRAM ****** printf("After sort\n"); for(i = 0; i < 10; i++) printf("%d\n",data[i]); return 0; } Looking for alternative solutions for the program code.
Do you have a flowgorithim flow chart for this code? #include <stdio.h> int main() { int num,i=0,sum=0; float average; int customerNumbers[num]; int customerSales[num]; printf("How many customers do you want to track?\n"); scanf("%d",&num); while(i<num) { printf("Enter the customer number. "); scanf("%d",&customerNumbers[i]); printf("Enter the sales for the customer "); scanf("%d",&customerSales[i]); i++; } printf("Sales for the Customer"); printf("\nCustomer Customer"); printf("\nNumber Sales"); for(i=0;i<num;i++) { printf("\n %d \t %d",customerNumbers[i], customerSales[i]); sum=sum+customerSales[i]; } average=(int)sum/num; printf("\n Total sales are $%d",sum); printf("\n Average sales are $%.2f",average); printf("\n --------------------------------------------");...
Can someone please complete the "allTheQueensAreSafe" function? #include <stdio.h> #include <stdlib.h> void printBoard(int *whichRow, int n) { int row, col; for (row = 0; row < n; row++) { for (col = 0; col < n; col++) { printf("%c", (whichRow[col] == row) ? 'Q' : '.'); } printf("\n"); } printf("\n"); } int allTheQueensAreSafe(int *whichRow, int n, int currentCol) { // TODO: Write a function that returns 1 if all the queens represented by // this array are safe (i.e., none...
#include<stdio.h> #include<stdio.h> int main(){ int i; //initialize array char array[10] = {“Smith”, “Owen”, “Kowalczyk”, “Glass”, “Bierling”, “Hanenburg”, “Rhoderick”, “Pearce”, “Raymond”, “Kamphuis”}; for(int i=0; i<8;i++){ for(int j=0; j<9; j++){ if(strcmp(array[j],array[j+1])>0){ char temp[20]; strcpy(temp,array[j]); strcpy(array[j],array[j+1]); strcpy(array[j+1],temp); } } } printf(“---------File Names---------\n”); for(inti=0; i<9; i++){ printf(“\t%s\n”,array[i]); } printf(-------5 Largest Files according to sorting----\n”); for(int i=0;i>=5;i--) { printf(“\t%s\n”,array[i]); } return0; } Consider the "sort" program (using with void* parameters in the bubblesort function) from the week 10 "sort void" lecture. Modify it as follows...