0% found this document useful (0 votes)
17 views7 pages

HFT 3

The document contains a programming assignment with multiple questions focused on data structures and algorithms in C++. It includes implementations for tasks such as word counting, sorting algorithms, substring searching, anagram checking, and finding missing numbers in a sequence. Each question is accompanied by code snippets and explanations relevant to the respective problem.

Uploaded by

23bce089
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)
17 views7 pages

HFT 3

The document contains a programming assignment with multiple questions focused on data structures and algorithms in C++. It includes implementations for tasks such as word counting, sorting algorithms, substring searching, anagram checking, and finding missing numbers in a sequence. Each question is accompanied by code snippets and explanations relevant to the respective problem.

Uploaded by

23bce089
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/ 7

HFT ASSIGNMENT 3

Prepared by:
Harsh Shah
(23BCE089)

21st January, 2025

Computer Science Engineering


Institute of Technology
Nirma University
Ahmedabad 382481

January 2025
Question 1:
#include <bits/stdc++.h>
using namespace std;
class st
{
private:
vector<pair<string, int>> a;
int find(string str)
{
if (a.empty())
return -1;
int lo = 0, hi = a.size();
while (hi - lo > 1)
{
int m = lo + (hi - lo) / 2;
if (a[m].first <= str)
lo = m;
else
hi = m;
}
if (a[lo].first == str)
return lo;
return -1;
}
public:
void display()
{
for (auto it : a)
cout << it.first << ' ' << it.second << endl;
}
void insert(string str)
{
int x = find(str);
if (x == -1)
{
a.push_back({str, 1});
sort(a.begin(), a.end());
}
else
{
a[x].second++;
}
}
};
int main()
{
st ob;
string s;
getline(cin,s);
stringstream ss(s);
string word;
while (ss >> word)
{
// cout << word << endl;
ob.insert(word);
}
ob.display();
return 0;
}
Question 2:
#include <bits/stdc++.h>
using namespace std;
void merge(vector<int> &arr, int left, int mid, int right)
{
int n1 = mid - left + 1;
int n2 = right - mid;
vector<int> L(n1), R(n2);
for (int i = 0; i < n1; i++)
L[i] = arr[left + i];
for (int i = 0; i < n2; i++)
R[i] = arr[mid + 1 + i];
int i = 0, j = 0, k = left;
while (i < n1 && j < n2)
{
if (L[i] >= R[j])
arr[k++] = L[i++];
else
arr[k++] = R[j++];
}
while (i < n1)
arr[k++] = L[i++];
while (j < n2)
arr[k++] = R[j++];
}
void mergeSort(vector<int> &arr, int left, int right)
{
if (left >= right)
return;
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
void sortVector(vector<int> &vec)
{
if (!vec.empty())
{
mergeSort(vec, 0, vec.size() - 1);
}
}
int find(const vector<int> &a, int src)
{
if (a.empty())
return -1;
int lo = 0, hi = a.size();
while (hi - lo > 1)
{
int m = lo + (hi - lo) / 2;
if (a[m] >= src)
lo = m;
else
hi = m;
}
if (a[lo] == src)
return lo;
return -1;
}
int main()
{
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
sortVector(a);
cout << "Sorted : ";
for(int i = 0; i < n; i++) cout << a[i]<< ' ';
cout << endl;
while (true)
{
int sc;
cout << "Enter Score : ";
cin >> sc;
int x = find(a, sc);
if (x == -1)
cout << "Not found" << endl;
else
cout << "Rank : " << x + 1 << endl;
}
return 0;
}
Question 3:
#include <iostream>
using namespace std;
int findSubstring(const char *mainStr, const char *subStr)
{
int mainLen = 0, subLen = 0;
while (mainStr[mainLen] != '\0')
mainLen++;
while (subStr[subLen] != '\0')
subLen++;
for (int i = 0; i <= mainLen - subLen; i++)
{
int j = 0;
while (j < subLen && mainStr[i + j] == subStr[j])
{
j++;
}
if (j == subLen)
return i;
}
return -1;
}
int main()
{
char mainStr[100], subStr[50];
cout << "Enter the main string: ";
cin.getline(mainStr, 100);
cout << "Enter the substring to search: ";
cin.getline(subStr, 50);
int index = findSubstring(mainStr, subStr);
if (index != -1)
cout << "Substring found at index " << index << "." << endl;
else
cout << "Substring not found." << endl;
return 0;
}
Question 4:
#include <bits/stdc++.h>
using namespace std;
void merge(vector<char> &arr, int left, int mid, int right)
{
int n1 = mid - left + 1;
int n2 = right - mid;
vector<int> L(n1), R(n2);
for (int i = 0; i < n1; i++)
L[i] = arr[left + i];
for (int i = 0; i < n2; i++)
R[i] = arr[mid + 1 + i];
int i = 0, j = 0, k = left;
while (i < n1 && j < n2)
{
if (L[i] >= R[j])
arr[k++] = L[i++];
else
arr[k++] = R[j++];
}
while (i < n1)
arr[k++] = L[i++];
while (j < n2)
arr[k++] = R[j++];
}
void mergeSort(vector<char> &arr, int left, int right)
{
if (left >= right)
return;
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
void srt(vector<char> &vec)
{
if (!vec.empty())
{
mergeSort(vec, 0, vec.size() - 1);
}
}
bool cmp(vector<char> &a, vector<char> &b)
{
if (a.size() != b.size())
return false;
for (int i = 0; i < a.size(); i++)
if (a[i] != b[i])
return false;
return true;
}
int main()
{
string s1, s2;
cin >> s1 >> s2;
vector<char> a1, a2;
for (char c : s1)
a1.push_back(c);
for (char c : s2)
a2.push_back(c);
srt(a1);
srt(a2);
if(cmp(a1 , a2))
{
cout << "The strings are anagrams. " << endl;
}
else
{
cout << "The strings are not anagrams. " << endl;
}
return 0;
}
Question 5:
#include <bits/stdc++.h>
using namespace std;
void merge(vector<int> &arr, int left, int mid, int right)
{
int n1 = mid - left + 1;
int n2 = right - mid;
vector<int> L(n1), R(n2);
for (int i = 0; i < n1; i++)
L[i] = arr[left + i];
for (int i = 0; i < n2; i++)
R[i] = arr[mid + 1 + i];
int i = 0, j = 0, k = left;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
arr[k++] = L[i++];
else
arr[k++] = R[j++];
}
while (i < n1)
arr[k++] = L[i++];
while (j < n2)
arr[k++] = R[j++];
}
void mergeSort(vector<int> &arr, int left, int right)
{
if (left >= right)
return;
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
void sortVector(vector<int> &vec)
{
if (!vec.empty())
{
mergeSort(vec, 0, vec.size() - 1);
}
}
int find(const vector<int> &a, int src)
{
if (a.empty())
return -1;
int lo = 0, hi = a.size();
while (hi - lo > 1)
{
int m = lo + (hi - lo) / 2;
if (a[m] >= src)
lo = m;
else
hi = m;
}
if (a[lo] == src)
return lo;
return -1;
}
int find(vector<int>& a)
{
for (int i = 0; i < a.size(); i++)
{
if(a[i] != i+1)
return i + 1;
}
return a.size()+1;
}
int main()
{
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
sortVector(a);
cout << find(a) << endl;
return 0;
}

You might also like