Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution for December of Algorithms 2024 #63

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
64 changes: 64 additions & 0 deletions December 01/Vanishing Number.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <stdio.h>
#include<stdlib.h>

int arraySize()
{
int n;
printf("Enter the value of n: ");
scanf("%d",&n);
return n;
}

int* createArray(int n)
{
int ctrl1;
int* array = (int*)malloc(n * sizeof(int));

for( ctrl1 = 0; ctrl1 < n; ctrl1++)
{
printf("Enter element-%d: ",ctrl1+1);
scanf("%d",&array[ctrl1]);
}
return array;
}

int vanishingNumber(int array[], int n)
{
int ctrl2 = 0, ctrl3 = 1, flag = 0;

while (ctrl2 < n && ctrl3 <= n)
{
if (array[ctrl2] != ctrl3)
{
return ctrl3;
flag = 1;
break;
}
else
{
flag = 0;
ctrl2++;
ctrl3++;
}
}
if(flag == 0)
{
return 0;
}
}

int main()
{
int size = arraySize();
int* result = createArray(size);
int missingNumber = vanishingNumber(result, size);
if (missingNumber != 0)
{
printf("The missing number is: %d", missingNumber);
}
else
{
printf("The numbers are in order.");
}
}

78 changes: 78 additions & 0 deletions December 02/WaveSort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include<stdio.h>
#include<stdlib.h>

int arraySize()
{
int n;
printf("Enter the number of elements for array: ");
scanf("%d", &n);
return n;
}

int* createArray(int size)
{
int ctrl;
int* array = (int*)malloc(size * sizeof(int));
for(ctrl = 0; ctrl < size; ctrl++)
{
printf("Enter element-%d: ",ctrl+1);
scanf("%d", &array[ctrl]);
}
return array;
}

int* arraySort(int array[], int size)
{
int ctrl1, ctrl2;
int end = size - 1;
for(ctrl1 = 0; ctrl1 < end; ctrl1++)
{
for(ctrl2 = ctrl1 + 1; ctrl2 < size; ctrl2++)
{
if(array[ctrl1] > array[ctrl2])
{
int temp = array[ctrl1];
array[ctrl1] = array[ctrl2];
array[ctrl2] = temp;
}
}
}
return array;
}

void display(int array[], int size)
{
int ctrl;
for(ctrl = 0; ctrl < size; ctrl++)
{
printf("%d\t",array[ctrl]);
}
}

int* waveSort(int array[], int result[], int size)
{
int end = size - 1;
int ctrl = 0, ctrl1 = 0;
int ctrl2 = end;
while(ctrl1 < ctrl2)
{
result[ctrl] = array[ctrl2];
ctrl++;
result[ctrl] = array[ctrl1];
ctrl++;
ctrl1++;
ctrl2--;
}
return result;
}

int main()
{
int size = arraySize();
int result[size];
int* input = createArray(size);
int* sortedInput = arraySort(input, size);
int* output = waveSort(sortedInput, result, size);
display(output, size);
return 0;
}
113 changes: 113 additions & 0 deletions December 03/Alternate Square.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include<stdio.h>

int number(char colour[])
{
int n;
printf("Enter the number of %s squares: ", colour);
scanf("%d",&n);
return n;
}

void display(char array[], int size)
{
int ctrl;
for(ctrl = 0; ctrl < size; ctrl++)
{
printf("%c\t",array[ctrl]);
}
}

void redSequence(int numberOfRed, int numberOfBlue)
{
int ctrl, idx = 0;
int size = numberOfRed + numberOfBlue;
char array[size];
for(ctrl = 1; ctrl <= numberOfRed; ctrl++)
{
array[idx] = 'R';
idx+=2;
}
idx = 1;
for(ctrl = 1; ctrl <= numberOfBlue; ctrl++)
{
array[idx] = 'B';
idx+=2;
}
display(array, size);
}
void blueSequence(int numberOfRed, int numberOfBlue)
{
int ctrl, idx = 0;
int size = numberOfRed + numberOfBlue;
char array[size];
for(ctrl = 1; ctrl <= numberOfRed; ctrl++)
{
array[idx] = 'B';
idx+=2;
}
idx = 1;
for(ctrl = 1; ctrl <= numberOfBlue; ctrl++)
{
array[idx] = 'R';
idx+=2;
}
display(array, size);
}

void possibleArrangement(int numberOfRed, int numberOfBlue, int boolean)
{
int twoRemaining = numberOfBlue - numberOfRed;
int oneRemaining = numberOfRed - numberOfBlue;

if(boolean == 1 && oneRemaining == 1)
{
printf("It is possible to form a sequence in which no two adjacent squares are of the same color\n");
redSequence(numberOfRed, numberOfBlue);
}

else if(boolean == 2 && twoRemaining == 1)
{
printf("It is possible to form a sequence in which no two adjacent squares are of the same color\n");
blueSequence(numberOfRed, numberOfBlue);
}
else if(boolean == 0 && oneRemaining == 0)
{
printf("It is possible to form a sequence in which no two adjacent squares are of the same color\n");
printf("Sequence-1: \n");
redSequence(numberOfRed, numberOfBlue);

printf("\nSequence-2: \n");
blueSequence(numberOfRed, numberOfBlue);
}

else
{
printf("Not possible");
}
}

void sizeCheck(int numberOfRed, int numberOfBlue)
{
if(numberOfRed > numberOfBlue)
{
possibleArrangement(numberOfRed, numberOfBlue, 1);
}

else if(numberOfRed < numberOfBlue)
{
possibleArrangement(numberOfRed, numberOfBlue, 2);
}

else
{
possibleArrangement(numberOfRed, numberOfBlue, 0);
}
}

int main()
{
int red = number("red");
int blue = number("blue");
sizeCheck(red, blue);
return 0;
}
60 changes: 60 additions & 0 deletions December 04/Plant Growth Tracker.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include<stdio.h>

int input()
{
int numberOfMonths;
printf("\nEnter the number of months: ");
scanf("%d",&numberOfMonths);
return numberOfMonths;
}

int predict(int numberOfMonths)
{
int start = 0, next = 1, ctrl;
int sum = start + next;
for(ctrl = 2; ctrl < numberOfMonths; ctrl++)
{
start = next;
next = sum;
sum = start + next;
}
return sum;
}

int errorHandling(int numberOfMonths)
{
if(numberOfMonths == 1)
{
return 1;
}
else if(numberOfMonths > 1)
{
return 0;
}
else
{
return -1;
}
}

int main()
{
int numberOfMonths = input();
int error = errorHandling(numberOfMonths);
if (error == 1)
{
printf("The plants take two months to grow before increasing in numbers.");
}

else if(numberOfMonths > 1)
{
int prediction = predict(numberOfMonths);
printf("\nAfter %d months, there will be %d plants", numberOfMonths, prediction);
}

else
{
printf("Please enter the number of months as postive integers");
}
return 0;
}
Loading