0% found this document useful (0 votes)
2 views10 pages

Automata Theory Questions and Answers

The document contains multiple C++ and C programs demonstrating different algorithms and functionalities. It includes implementations for Shell Sort, solving the shoemaker's problem, a WERTYU keyboard mapping program, and a check for whether a king in chess is under attack. Each program is accompanied by sample input and output to illustrate its operation.

Uploaded by

mahajangaurav497
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

Automata Theory Questions and Answers

The document contains multiple C++ and C programs demonstrating different algorithms and functionalities. It includes implementations for Shell Sort, solving the shoemaker's problem, a WERTYU keyboard mapping program, and a check for whether a king in chess is under attack. Each program is accompanied by sample input and output to illustrate its operation.

Uploaded by

mahajangaurav497
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Name :-Gaurav Ishwar Martand Rollno:91

Batch :T2 Div :-B

7]Program for Shell Sort Algorithm

// C++ implementation of Shell

Sort #include <iostream>

using namespace std;

/* function to sort arr using shellSort */

int shellSort(int arr[], int n)

// Start with a big gap, then reduce the gap

for (int gap = n/2; gap > 0; gap /= 2)

// Do a gapped insertion sort for this gap size.

// The first gap elements a[0..gap-1] are already in gapped order

// keep adding one more element until the entire array is

// gap sorted

for (int i = gap; i < n; i += 1)

// add a[i] to the elements that have been gap sorted

// save a[i] in temp and make a hole at position i

int temp = arr[i];

// shift earlier gap-sorted elements up until the correct

// location for a[i] is

found int j;
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)

arr[j] = arr[j - gap];

// put temp (the original a[i]) in its correct location

arr[j] = temp;

return 0;

void printArray(int arr[], int n)

for (int i=0; i<n; i++)

cout << arr[i] << " ";

int main()

int arr[] = {12, 34, 54, 2, 3}, i;

int n = sizeof(arr)/sizeof(arr[0]);

cout << "Array before sorting: \

n"; printArray(arr, n);

shellSort(arr, n);

cout << "\nArray after sorting: \

n"; printArray(arr, n);

return 0;

}
Output:-
Array before sorting:

30 11 12 40 20

Array after sorting:

11 12 20 30 40
Name :- Gaurav Ishwar Martand Roll no:-91

Batch :-T2 Div :-B

4]Program for shoemaker's Problem

#include <stdio.h>
#include <stdlib.h>

// Structure to represent a job


struct Job {
int id; // Job ID

int time; // Time to complete the job


int penalty; // Penalty if the job is late
};

// Function to compare jobs based on the penalty/time ratio


int compareJobs(const void* a, const void* b) {
const struct Job* job1 = (const struct Job*)a;
const struct Job* job2 = (const struct Job*)b;

double ratio1 = (double)job1->penalty / job1->time;


double ratio2 = (double)job2->penalty / job2->time;

if (ratio1 > ratio2)


return -1; // Higher ratio gets higher priority
else if (ratio1 < ratio2)
return 1;
else
return 0;
}
// Function to find the sequence of jobs that minimizes total penalty
void findOptimalSchedule(struct Job jobs[], int n) {
// Sort the jobs based on the scheduling rule
qsort(jobs, n, sizeof(struct Job), compareJobs);

// Print the job sequence


printf("\nOptimal job sequence to minimize total penalty:\n");
for (int i = 0; i < n; i++) {
printf("Job %d (Time: %d, Penalty: %d)\n", jobs[i].id, jobs[i].time, jobs[i].penalty);

}
}

int main()
{ int n;

// Input the number of jobs


printf("Enter the number of jobs: ");
scanf("%d", &n);

struct Job jobs[n];

// Input job details from the user


for (int i = 0; i < n; i++) {
jobs[i].id = i + 1;
printf("Enter time and penalty for job %d: ", i + 1);
scanf("%d %d", &jobs[i].time, &jobs[i].penalty);
}

// Find the optimal job


sequence
findOptimalSchedule(jobs, n);
return 0;
}

Output:-
Enter the number of jobs: 3

Enter time and penalty for job 1: 12 22

Enter time and penalty for job 2: 13 21

Enter time and penalty for job 3: 43 45

Optimal job sequence to minimize total penalty:

Job 1 (Time: 12, Penalty: 22)

Job 2 (Time: 13, Penalty: 21)

Job 3 (Time: 43, Penalty: 45)


Name : Gaurav Ishwar Martand Rollno:-91

Batch :-T2 Div :-B

6]Program for WERTYU

#include <vector>
#include <string>
#include <iostream>
using namespace std;

int main() {
string tec = "1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";
string men;
vector<string> v;

while (getline(cin, men))


{ v.push_back(men);
}

for (int j = 0; j < v.size(); j++)


{ men = v[j];
string ans = "";

for (int i = 0; i < men.size(); i++)


{ char ch = men[i];
size_t n = tec.find(ch);
if (n != string::npos && ch != 'Q' && ch != 'A' && ch != 'Z')
{ ans += tec[n - 1];
} else {
ans += ch;
}
}
cout << ans << endl;
}

return 0;
}
Output:-

Input given to program :- 7890-

Output: 6789=

Input given to program :- ;/.,'

Output: LKJHG
Name :- Gaurav Ishwar Martand Rollno:91

Batch :-T2 Div :-B

8] Program for check the check

#include <stdio.h>
#include <stdbool.h>

// Function to check if the king is under attack


bool isKingUnderAttack(int kingX, int kingY, int opponentX, int opponentY) {
// Check if the opponent piece is in the same row, column, or diagonal
if (kingX == opponentX || kingY == opponentY ||
kingX - kingY == opponentX - opponentY ||
kingX + kingY == opponentX + opponentY) {
return true; // King is under attack
} else {
return false; // King is not under attack
}
}

int main() {
int kingX, kingY; // King's coordinates
int opponentX, opponentY; // Opponent's piece coordinates

// Input king's coordinates


printf("Enter king's coordinates (x y): ");
scanf("%d %d", &kingX, &kingY);

// Input opponent's piece coordinates


printf("Enter opponent's piece coordinates (x y): ");
scanf("%d %d", &opponentX, &opponentY);
// Check if the king is under attack
if (isKingUnderAttack(kingX, kingY, opponentX, opponentY))
{ printf("King is under attack!\n");
} else {
printf("King is not under attack.\n");
}

return 0;
}
Output :-
Enter king's coordinates (x y): 8 8
Enter opponent's piece coordinates (x y): 1 1
King is under attack!
Enter king's coordinates (x y): 4 2
Enter opponent's piece coordinates (x y): 6 5
King is not under attack!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy