Skip to content

New algorithm #458

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions algorithms/ciphers/vigenere_cipher.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
=======================================================
Algorithm: Vigenere Cipher
=======================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

void encryption ();
void decryption ();

int main() {

int choice;

retry:
// Taking users choice
printf("Choose an option: \n");
printf(" 1. Encryption\n 2. Decryption\n");
scanf("%d", &choice);
while (getchar() != '\n');

if (choice == 1) {
encryption();
} else if (choice == 2) {
decryption();
} else goto retry;

return 0;
}

// envryption function
void encryption () {

char chain[100];
char key[100];
char text[100];
char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
int poschain, poskey, postext;
int lenchain, lenkey;
int textIndex = 0;

// Taking Plaintext
system("cls || clear");
printf("Enter plaintext : ");
scanf("%[^\n]", chain);
while (getchar() != '\n');

// Taking key
printf("Enter encryption key : ");
scanf("%[^\n]", key);
while (getchar() != '\n');
system("cls || clear");

for (int i = 0; chain[i]; i++) {
chain[i] = tolower(chain[i]);
}
for (int i = 0; key[i]; i++) {
key[i] = tolower(key[i]);
}

lenchain = strlen(chain);
lenkey = strlen(key);
// Encryption
for (int i = 0; i < lenchain; i++) {
poskey = i % lenkey;

poschain = chain[i] - 'a';
poskey = key[poskey] - 'a';

//Text concatenation
postext = (poschain + poskey) % 26;
text[textIndex++] = alphabet[postext];
}

// terminate the string with a '\0'
text[textIndex] = '\0';

// print result
system("cls || clear");
printf("-======[ Vigenere cipher ]============-\n\n");
printf("\t- Plaintext: %s\n", chain);
printf("\t- Key : %s\n", key);
printf("\t- Vigenere ciphertext : %s\n", text);
getchar ();
}

void decryption () {

char chain[100];
char key[100];
char text[100];
char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
int poschain, poskey, postext;
int lenchain, lenkey, choice;
int textIndex = 0;

system("cls || clear");
printf("Enter ciphertext : ");
scanf("%[^\n]", chain);
while (getchar() != '\n');

printf("Enter encryption key : ");
scanf("%[^\n]", key);
while (getchar() != '\n');

// converting chain and key to lowercase to facilitate string management
for (int i = 0; chain[i]; i++) {
chain[i] = tolower(chain[i]);
}
for (int i = 0; key[i]; i++) {
key[i] = tolower(key[i]);
}

lenchain = strlen(chain);
lenkey = strlen(key);

for (int i = 0; i < lenchain; i++) {
poskey = i % lenkey;

poschain = chain[i] - 'a';
poskey = key[poskey] - 'a';

//Text concatenation
postext = (poschain - poskey + 26) % 26;
text[textIndex++] = alphabet[postext];
}

text[textIndex] = '\0';

system("cls || clear");
printf("-======[ Vigenere cipher ]============-\n\n");
printf("\t- Vigenere ciphertext: %s\n", chain);
printf("\t- Key : %s\n", key);
printf("\t- Plaintext : %s\n", text);
}
25 changes: 15 additions & 10 deletions algorithms/roundrobin.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@ int main()
int i, limit, total = 0, x, counter = 0, time_quantum;
int wait_time = 0, turnaround_time = 0, arrival_time[10], burst_time[10], temp[10];
float average_wait_time, average_turnaround_time;
printf("nEnter Total Number of Processes:t");
printf("\nEnter Total Number of Processes: ");
scanf("%d", &limit);
x = limit;
for(i = 0; i < limit; i++)
{
printf("nEnter Details of Process[%d]n", i + 1);
printf("\nEnter Details of Process[%d]\n", i + 1);

printf("Arrival Time:t");
printf("Arrival Time: ");

scanf("%d", &arrival_time[i]);

printf("Burst Time:t");
printf("Burst Time: ");

scanf("%d", &burst_time[i]);

temp[i] = burst_time[i];
}

printf("nEnter Time Quantum:t");
printf("\nEnter Time Quantum: ");
scanf("%d", &time_quantum);
printf("nProcess IDttBurst Timet Turnaround Timet Waiting Timen");
printf("\nProcess ID\t\tBurst Time\t Turnaround Time\t Waiting Time\n");
for(total = 0, i = 0; x != 0;)
{
{
if(temp[i] <= time_quantum && temp[i] > 0)
{
total = total + temp[i];
Expand All @@ -42,7 +42,11 @@ int main()
if(temp[i] == 0 && counter == 1)
{
x--;
printf("nProcess[%d]tt%dtt %dttt %d", i + 1, burst_time[i], total - arrival_time[i], total - arrival_time[i] - burst_time[i]);
printf("\nProcess [%d]\t\t%d\t\t %d\t\t\t %d", i + 1,
burst_time[i],
total - arrival_time[i],
total - arrival_time[i] - burst_time[i]);

wait_time = wait_time + total - arrival_time[i] - burst_time[i];
turnaround_time = turnaround_time + total - arrival_time[i];
counter = 0;
Expand All @@ -63,7 +67,8 @@ int main()

average_wait_time = wait_time * 1.0 / limit;
average_turnaround_time = turnaround_time * 1.0 / limit;
printf("nnAverage Waiting Time:t%f", average_wait_time);
printf("nAvg Turnaround Time:t%fn", average_turnaround_time);
printf("\n\nAverage Waiting Time: %.2f", average_wait_time);
printf("\nAvg Turnaround Time: %.2f\n", average_turnaround_time);
getchar();
return 0;
}