-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Some Basic Programs in C++ (#53)
* Age Calculator * Program in C++ * Fixed Scattered C++ Programs Co-authored-by: Aman Ahmed Siddiqui <[email protected]>
- Loading branch information
1 parent
ad98a2d
commit c32f7b5
Showing
56 changed files
with
3,031 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//Code to check if 2 strings are anagrams of each other.. | ||
#include <iostream> | ||
#include<unordered_map> | ||
#include<string> | ||
using namespace std; | ||
bool checkAnagram(string str1,string str2,int l1,int l2){ | ||
unordered_map<char,int> mp; | ||
for(int i=0;i<l1;i++){ | ||
mp[str1[i]]++; | ||
} | ||
for(int i=0;i<l2;i++){ | ||
mp[str2[i]]--; | ||
} | ||
for(auto x:mp) if(x.second!=0) return false; | ||
return true; | ||
} | ||
int main()1 | ||
{ | ||
int t; | ||
cin >> t; | ||
while (t--) | ||
{ | ||
string str1,str2; | ||
cin>>str1>>str2; | ||
if(checkAnagram(str1,str2,str1.length(),str2.length())){ | ||
cout<<str1<<" and "<<str2<<" are anagrams...\n"; | ||
} | ||
else{ | ||
cout<<"Not anagrams"; | ||
} | ||
} | ||
} | ||
|
||
//By Rajat Dhull |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include<iostream> | ||
#include<vector> | ||
#include<algorithm> | ||
#include <string> | ||
#include<cstring> | ||
#include<map> | ||
#include<set> | ||
#include<unordered_set> | ||
#include<unordered_map> | ||
#include<cmath> | ||
#include<conio.h> | ||
#include<queue> | ||
#include <utility> | ||
typedef long long ll; | ||
using namespace std; | ||
int main() | ||
{ | ||
getch(); | ||
} | ||
class Solution { | ||
public: | ||
string breakPalindrome(string palindrome) { | ||
int n = palindrome.length(); | ||
if(n==1) | ||
return ""; | ||
for(int i=0;i<n;i++) | ||
{ | ||
if((palindrome[i]!='a'&&i!=n/2)||(palindrome[i]!='a'&&i==n/2&&n%2==0)) | ||
{ | ||
palindrome[i] = 'a'; | ||
return palindrome; | ||
} | ||
} | ||
palindrome[n-1]='b'; | ||
return palindrome; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
// Bucket sort in C++ | ||
|
||
#include <iomanip> | ||
#include <iostream> | ||
using namespace std; | ||
|
||
#define NARRAY 7 // Array size | ||
#define NBUCKET 6 // Number of buckets | ||
#define INTERVAL 10 // Each bucket capacity | ||
|
||
struct Node { | ||
int data; | ||
struct Node *next; | ||
}; | ||
|
||
void BucketSort(int arr[]); | ||
struct Node *InsertionSort(struct Node *list); | ||
void print(int arr[]); | ||
void printBuckets(struct Node *list); | ||
int getBucketIndex(int value); | ||
|
||
// Sorting function | ||
void BucketSort(int arr[]) { | ||
int i, j; | ||
struct Node **buckets; | ||
|
||
// Create buckets and allocate memory size | ||
buckets = (struct Node **)malloc(sizeof(struct Node *) * NBUCKET); | ||
|
||
// Initialize empty buckets | ||
for (i = 0; i < NBUCKET; ++i) { | ||
buckets[i] = NULL; | ||
} | ||
|
||
// Fill the buckets with respective elements | ||
for (i = 0; i < NARRAY; ++i) { | ||
struct Node *current; | ||
int pos = getBucketIndex(arr[i]); | ||
current = (struct Node *)malloc(sizeof(struct Node)); | ||
current->data = arr[i]; | ||
current->next = buckets[pos]; | ||
buckets[pos] = current; | ||
} | ||
|
||
// Print the buckets along with their elements | ||
for (i = 0; i < NBUCKET; i++) { | ||
cout << "Bucket[" << i << "] : "; | ||
printBuckets(buckets[i]); | ||
cout << endl; | ||
} | ||
|
||
// Sort the elements of each bucket | ||
for (i = 0; i < NBUCKET; ++i) { | ||
buckets[i] = InsertionSort(buckets[i]); | ||
} | ||
|
||
cout << "-------------" << endl; | ||
cout << "Bucktets after sorted" << endl; | ||
for (i = 0; i < NBUCKET; i++) { | ||
cout << "Bucket[" << i << "] : "; | ||
printBuckets(buckets[i]); | ||
cout << endl; | ||
} | ||
|
||
// Put sorted elements on arr | ||
for (j = 0, i = 0; i < NBUCKET; ++i) { | ||
struct Node *node; | ||
node = buckets[i]; | ||
while (node) { | ||
arr[j++] = node->data; | ||
node = node->next; | ||
} | ||
} | ||
|
||
for (i = 0; i < NBUCKET; ++i) { | ||
struct Node *node; | ||
node = buckets[i]; | ||
while (node) { | ||
struct Node *tmp; | ||
tmp = node; | ||
node = node->next; | ||
free(tmp); | ||
} | ||
} | ||
free(buckets); | ||
return; | ||
} | ||
|
||
// Function to sort the elements of each bucket | ||
struct Node *InsertionSort(struct Node *list) { | ||
struct Node *k, *nodeList; | ||
if (list == 0 || list->next == 0) { | ||
return list; | ||
} | ||
|
||
nodeList = list; | ||
k = list->next; | ||
nodeList->next = 0; | ||
while (k != 0) { | ||
struct Node *ptr; | ||
if (nodeList->data > k->data) { | ||
struct Node *tmp; | ||
tmp = k; | ||
k = k->next; | ||
tmp->next = nodeList; | ||
nodeList = tmp; | ||
continue; | ||
} | ||
|
||
for (ptr = nodeList; ptr->next != 0; ptr = ptr->next) { | ||
if (ptr->next->data > k->data) | ||
break; | ||
} | ||
|
||
if (ptr->next != 0) { | ||
struct Node *tmp; | ||
tmp = k; | ||
k = k->next; | ||
tmp->next = ptr->next; | ||
ptr->next = tmp; | ||
continue; | ||
} else { | ||
ptr->next = k; | ||
k = k->next; | ||
ptr->next->next = 0; | ||
continue; | ||
} | ||
} | ||
return nodeList; | ||
} | ||
|
||
int getBucketIndex(int value) { | ||
return value / INTERVAL; | ||
} | ||
|
||
// Print buckets | ||
void print(int ar[]) { | ||
int i; | ||
for (i = 0; i < NARRAY; ++i) { | ||
cout << setw(3) << ar[i]; | ||
} | ||
cout << endl; | ||
} | ||
|
||
void printBuckets(struct Node *list) { | ||
struct Node *cur = list; | ||
while (cur) { | ||
cout << setw(3) << cur->data; | ||
cur = cur->next; | ||
} | ||
} | ||
|
||
// Driver code | ||
int main(void) { | ||
int array[NARRAY] = {42, 32, 33, 52, 37, 47, 51}; | ||
|
||
cout << "Initial array: " << endl; | ||
print(array); | ||
cout << "-------------" << endl; | ||
|
||
BucketSort(array); | ||
cout << "-------------" << endl; | ||
cout << "Sorted array: " << endl; | ||
print(array); | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
// Function to swap two numbers in the array | ||
void swap(int arr[], int i, int j) | ||
{ | ||
int temp; | ||
temp = arr[i]; | ||
arr[i] = arr[j]; | ||
arr[j] = temp; | ||
} | ||
|
||
// We will maintain three pointers, low and mid on zero and high on last element | ||
// This sort only works when there are only 3 unique elements in the array | ||
void DNF_sort(int arr[], int n) | ||
{ | ||
int low = 0; | ||
int mid = 0; | ||
int high = n - 1; | ||
while (mid <= high) | ||
{ | ||
if (arr[mid] == 0) | ||
{ | ||
swap(arr, low, mid); | ||
low++; | ||
mid++; | ||
} | ||
else if (arr[mid] == 1) | ||
{ | ||
mid++; | ||
} | ||
else | ||
{ | ||
swap(arr, mid, high); | ||
high--; | ||
} | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
// Here the three numbers are 0,1 and 2 | ||
int arr[] = {0, 1, 1, 2, 1, 2, 0, 2}; | ||
|
||
// Calling the function | ||
DNF_sort(arr, 8); | ||
|
||
// Printing the sorted array | ||
for (int i = 0; i < 8; i++) | ||
{ | ||
cout << arr[i] << " "; | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#include <iostream> | ||
#include <string> | ||
using namespace std; | ||
class DayofYear | ||
{ | ||
private: | ||
int day; | ||
public: | ||
DayofYear(int d) | ||
{ | ||
day = d; | ||
|
||
} | ||
void Print() | ||
{ | ||
|
||
string months[] = {" ","January","Feburary","March","April","May","June","July","August","September","October","November","December" }; | ||
int DaysInEachMonth[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; | ||
if (day < 0 && day>365) | ||
{ | ||
cout << "ERROR......!" << endl; | ||
exit(0); | ||
} | ||
if (day > 0 && day <= 31) | ||
{ | ||
cout << months[1] << " " << day; | ||
} | ||
else if (day > 31 && day <= 59) | ||
{ | ||
day -= DaysInEachMonth[1]; | ||
cout << months[2] << " " << day; | ||
|
||
} | ||
else if (day > 59 && day <=90) | ||
{ | ||
day -= DaysInEachMonth[1] + DaysInEachMonth[2]; | ||
cout << months[3] << " " << day; | ||
} | ||
else if (day >90 && day <=120) | ||
{ | ||
day -= DaysInEachMonth[1] + DaysInEachMonth[2] + DaysInEachMonth[3]; | ||
cout << months[4] << " " << day; | ||
} | ||
else if (day > 120 && day <= 151) | ||
{ | ||
day -= DaysInEachMonth[1] + DaysInEachMonth[2] + DaysInEachMonth[3] + DaysInEachMonth[4]; | ||
cout << months[5] << " " << day; | ||
} | ||
else if (day >151 && day <= 181) | ||
|
||
{ | ||
day -= DaysInEachMonth[1] + DaysInEachMonth[2] + DaysInEachMonth[3] + DaysInEachMonth[4] + DaysInEachMonth[5]; | ||
cout << months[6] << " " << day; | ||
} | ||
else if (day > 181 && day <= 212) | ||
{ | ||
day -= DaysInEachMonth[1] + DaysInEachMonth[2] + DaysInEachMonth[3] + DaysInEachMonth[4] + DaysInEachMonth[5] + DaysInEachMonth[6]; | ||
cout << months[7] << " " << day; | ||
} | ||
else if (day > 212 && day <= 243) | ||
{ | ||
day -= DaysInEachMonth[1] + DaysInEachMonth[2] + DaysInEachMonth[3] + DaysInEachMonth[4] + DaysInEachMonth[5] + DaysInEachMonth[6] + DaysInEachMonth[7]; | ||
cout << months[8] << " " << day; | ||
} | ||
else if (day > 243 && day <= 273) | ||
{ | ||
day -= DaysInEachMonth[1] + DaysInEachMonth[2] + DaysInEachMonth[3] + DaysInEachMonth[4] + DaysInEachMonth[5] + DaysInEachMonth[6] + DaysInEachMonth[7] + DaysInEachMonth[8]; | ||
cout << months[9] << " " << day; | ||
} | ||
else if (day >273 && day <= 304) | ||
{ | ||
day -= DaysInEachMonth[1] + DaysInEachMonth[2] + DaysInEachMonth[3] + DaysInEachMonth[4] + DaysInEachMonth[5] + DaysInEachMonth[6] + DaysInEachMonth[7] + DaysInEachMonth[8] + DaysInEachMonth[9]; | ||
cout << months[10] << " " << day; | ||
} | ||
else if (day > 304 && day <= 334) | ||
{ | ||
day -= DaysInEachMonth[1] + DaysInEachMonth[2] + DaysInEachMonth[3] + DaysInEachMonth[4] + DaysInEachMonth[5] + DaysInEachMonth[6] + DaysInEachMonth[7] + DaysInEachMonth[8] + DaysInEachMonth[9] + DaysInEachMonth[10]; | ||
cout << months[11] << " " << day; | ||
} | ||
else if (day > 334 && day <= 365) | ||
{ | ||
day -= DaysInEachMonth[1] + DaysInEachMonth[2] + DaysInEachMonth[3] + DaysInEachMonth[4] + DaysInEachMonth[5] + DaysInEachMonth[6] + DaysInEachMonth[7] + DaysInEachMonth[8] + DaysInEachMonth[9] + DaysInEachMonth[10] + DaysInEachMonth[11]; | ||
cout << months[12] << " " << day; | ||
} | ||
|
||
} | ||
|
||
}; | ||
int main() | ||
{ | ||
DayofYear d[5] = {4,365,78,9,100}; | ||
for (int i = 0; i < 5; i++) | ||
{ | ||
d[i].Print(); | ||
cout << endl; | ||
} | ||
|
||
return 0; | ||
} |
Oops, something went wrong.