diff --git a/Decimal To Hexadecimal .cpp b/Decimal To Hexadecimal .cpp new file mode 100644 index 000000000..fadddfd74 --- /dev/null +++ b/Decimal To Hexadecimal .cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; + +void main(void) +{ + int valueToConvert = 0; //Holds user input + int hexArray[8]; //Contains hex values backwards + int i = 0; //counter + int lValue = 0; //Last Value of Hex result + + cout << "Enter a Decimal Value" << endl; //Displays request to stdout + cin >> valueToConvert; //Stores value into valueToConvert via user input + + while (valueToConvert > 0) //Dec to Hex Algorithm + { + lValue = valueToConvert % 16; //Gets remainder + valueToConvert = valueToConvert / 16; + hexArray[i] = lValue; //Stores converted values into an array + i++; + } + cout << "Hex Value: "; + while (i > 0) + { + //Displays Hex Letters to stdout + switch (hexArray[i - 1]) { + case 10: + cout << "A"; + break; + case 11: + cout << "B"; + break; + case 12: + cout << "C"; + break; + case 13: + cout << "D"; + break; + case 14: + cout << "E"; + break; + case 15: + cout << "F"; + break; + default: + cout << hexArray[i - 1]; //if not an int 10 - 15, displays int value + } + i--; + } + cout << endl; +} diff --git a/Heap Sort .cpp b/Heap Sort .cpp new file mode 100644 index 000000000..f188a0ea9 --- /dev/null +++ b/Heap Sort .cpp @@ -0,0 +1,62 @@ +#include +#include +using namespace std; +void max_heapify(int *a, int i, int n) +{ + int j, temp; + temp = a[i]; + j = 2 * i; + while (j <= n) + { + if (j < n && a[j + 1] > a[j]) + j = j + 1; + if (temp > a[j]) + break; + else if (temp <= a[j]) + { + a[j / 2] = a[j]; + j = 2 * j; + } + } + a[j / 2] = temp; + return; +} +void heapsort(int *a, int n) +{ + int i, temp; + for (i = n; i >= 2; i--) + { + temp = a[i]; + a[i] = a[1]; + a[1] = temp; + max_heapify(a, 1, i - 1); + } +} +void build_maxheap(int *a, int n) +{ + int i; + for (i = n / 2; i >= 1; i--) + { + max_heapify(a, i, n); + } +} +int main() +{ + int n, i, x; + cout << "Enter number of elements of array\n"; + cin >> n; + int a[20]; + for (i = 1; i <= n; i++) + { + cout << "Enter Element " << (i) << endl; + cin >> a[i]; + } + build_maxheap(a, n); + heapsort(a, n); + cout << "Sorted Output\n"; + for (i = 1; i <= n; i++) + { + cout << a[i] << endl; + } + getch(); +} diff --git a/Paranthesis Matching.cpp b/Paranthesis Matching.cpp index 0e4555d4e..5d37a161e 100644 --- a/Paranthesis Matching.cpp +++ b/Paranthesis Matching.cpp @@ -19,7 +19,7 @@ char pop() bool check(char x, char y) { - if ( ( x=='(' && y==')' ) || ( x=='{' && y=='}' ) || ( x=='[' && y==']' ) || ( x=='<' && y=='>' ) ) + if ((x=='(' && y==')') || (x=='{' && y=='}') || (x=='[' && y==']') || (x=='<' && y=='>')) { return true; } @@ -38,12 +38,11 @@ int main() gets(exp); for (int i = 0; i < strlen(exp); i++) { - if (exp[i]=='(' || exp[i]=='{' || exp[i]=='[' || exp[i]=='<') + if (exp[i]=='(' || exp[i]=='{' || exp[i]=='[' || exp[i]=='<') { push(exp[i]); - //show(); } - else if (exp[i]==')' || exp[i]=='}' || exp[i]==']' || exp[i]=='>') + else if (exp[i]==')' || exp[i]=='}' || exp[i]==']' || exp[i]=='>') { if(!check(pop(), exp[i])) {