diff --git a/Datastructures/Linked List.cpp b/Datastructures/Linked List.cpp index 02a2523ee..5abebd584 100644 --- a/Datastructures/Linked List.cpp +++ b/Datastructures/Linked List.cpp @@ -101,7 +101,7 @@ int main() cout<<"\n2. Delete"; cout<<"\n3. Search"; cout<<"\n4. Print"; - cout<<"\n5. Exit"; + cout<<"\n0. Exit"; cout<<"\n\nEnter you choice : "; cin>>choice; switch (choice) @@ -115,8 +115,8 @@ int main() case 3 : cout<<"\nEnter the element to be searched : "; cin>>x; search(x); break; - case 4 : show(); break; - case 5 : exit(0); + case 4 : show(); + cout<<"\n"; break; } } while(choice!=0); diff --git a/Datastructures/Stack Using Array.cpp b/Datastructures/Stack Using Array.cpp index 944258ed4..c5ad146cb 100644 --- a/Datastructures/Stack Using Array.cpp +++ b/Datastructures/Stack Using Array.cpp @@ -1,12 +1,12 @@ #include using namespace std; -int stack[10]; -int top=0; +int *stack; +int top=0, size; void push(int x) { - if(top==10) + if(top==size) { cout<<"\nOverflow"; } @@ -42,6 +42,9 @@ void topmost() } int main() { + cout<<"\nEnter Size of stack : "; + cin>>size; + stack = new int[size]; int ch, x; do { diff --git a/Decimal To Binary.cpp b/Decimal To Binary.cpp index 3794d2a96..b8eaba1f1 100644 --- a/Decimal To Binary.cpp +++ b/Decimal To Binary.cpp @@ -1,20 +1,24 @@ -#include +// This function convert decimal to binary number +// +#include using namespace std; + int main() { int number; - cin>>number; - int remainder,binary=0,var=1; - -do{ - - remainder=number%2; - number=number/2; - binary=binary+(remainder*var); - var=var*10; + cout << "Enter a number:"; + cin >> number; + int remainder, binary = 0, var = 1; + do { + remainder = number % 2; + number = number / 2; + binary = binary + (remainder*var); + var = var * 10; -} -while(number>0); - cout<0); + cout << "the binary is :"; + cout << binary; + cout << endl; + return 0; } diff --git a/Decimal To Hexadecimal .cpp b/Decimal To Hexadecimal .cpp index fadddfd74..39333ca00 100644 --- a/Decimal To Hexadecimal .cpp +++ b/Decimal To Hexadecimal .cpp @@ -2,50 +2,25 @@ using namespace std; -void main(void) -{ +int 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 + char HexValues[] = "0123456789ABCDEF"; 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++; + while (valueToConvert > 15){ //Dec to Hex Algorithm + hexArray[i++] = valueToConvert % 16; //Gets remainder + valueToConvert /= 16; } + hexArray[i] = valueToConvert; //Gets last value + 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--; - } + while (i >= 0) + cout< using namespace std; + int main() { int n,k,s=0,d; @@ -23,4 +24,5 @@ int main() cout << n << " is a happy number" << endl; else cout << n << " is not a happy number" << endl; + return 0; } diff --git a/Others/Paranthesis Matching.cpp b/Others/Paranthesis Matching.cpp index 0347f38d0..25ee7287b 100644 --- a/Others/Paranthesis Matching.cpp +++ b/Others/Paranthesis Matching.cpp @@ -1,52 +1,63 @@ #include -#include #include -#include + using namespace std; -const int MAX = 100; +#define MAX 100 // -------------- stack -------------- char stack[MAX]; -int top=0; +int top = -1; -void push(char ch) -{ - stack[top++]=ch; +void push(char ch){ + stack[ ++top ] = ch; } -char pop() -{ - return stack[--top]; +char pop(){ + return stack[ top-- ]; } // -------------- end stack ----------- -int main() -{ +char opening(char ch){ + switch(ch){ + case '}': + return '{'; + case ']': + return '['; + case ')': + return '('; + case '>': + return '<'; + } +} + +int main(){ + string exp; + int valid = 1, i = 0; cout<<"Enter The Expression : "; cin >> exp; - for (int i = 0; i < exp.length(); i++) - { - if (exp[i]=='(' || exp[i]=='{' || exp[i]=='[' || exp[i]=='<') - { + + while (valid == 1 && i < exp.length()){ + if (exp[i] == '(' || exp[i] == '{' || exp[i] == '[' || exp[i] == '<'){ push(exp[i]); } - else if (exp[i]==')' || exp[i]=='}' || exp[i]==']' || exp[i]=='>') - { - pop(); + else if (top >= 0 && stack[top] == opening(exp[i])){ + pop(); } + else{ + valid = 0; + } + i++; } // makes sure the stack is empty after processsing (above) - if(top==0) - { - cout<<"Correct Expression"; + if (valid == 1 && top == -1){ + cout<<"\nCorrect Expression"; } - else - { + else{ cout<<"\nWrong Expression"; } diff --git a/Others/String Fibonacci.cpp b/Others/String Fibonacci.cpp index 44d5d5748..8027b9668 100644 --- a/Others/String Fibonacci.cpp +++ b/Others/String Fibonacci.cpp @@ -81,5 +81,5 @@ int main() cout << n << " th Fibonacci is \n"; fib_Accurate(n); - return 0; + return 0; } diff --git a/README.md b/README.md index e4ad41623..81367fcb4 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,8 @@ How you can contribute? See this small guide. * Use the directory structure of the repository. * Please describe your pull requests. -* Don't use **bits/stdc++.h** because this is quite Linux specific and slow down the compiler process. -* Put in comments in your code. -* Avoid **struct** uses instead the **class** keyword. -* Add some test cases in the main-function. \ No newline at end of file +* Don't use **bits/stdc++.h** because this is quite Linux specific and slows down the compiler process. +* Put comments in your code. +* Avoid **struct**. Instead use the **class** keyword. +* Add some test cases in the main-function. +* Can suggest any change in present algorithms(if needed). \ No newline at end of file diff --git a/Sorting/Bubble Sort.cpp b/Sorting/Bubble Sort.cpp index c94a45efc..fed01bfc5 100644 --- a/Sorting/Bubble Sort.cpp +++ b/Sorting/Bubble Sort.cpp @@ -1,39 +1,55 @@ //Bubble Sort #include +#include using namespace std; int main() { int n; + short swap_check=0; + cout << "Enter the amount of numbers to sort: "; cin >> n; - int Array[n]; - cout<<"\nEnter any 6 Numbers for Unsorted Array : "; + vector numbers; + cout << "Enter " << n << " numbers: "; + int num; - //Input + //Input for(int i=0; i>Array[i]; + cin >> num; + numbers.push_back(num); } //Bubble Sorting for(int i=0; iArray[j+1]) + if(numbers[j]>numbers[j+1]) { - int temp=Array[j]; - Array[j]=Array[j+1]; - Array[j+1]=temp; + swap_check=1; + swap(numbers[j], numbers[j+1]); } } + if(swap_check == 0) + { + break; + } } //Output cout<<"\nSorted Array : "; - for(int i=0; i +using namespace std; + +int Max(int Arr[], int N){ + int max = Arr[0]; + for(int i=1; i max) + max = Arr[i]; + return max; +} + +int Min(int Arr[], int N){ + int min = Arr[0]; + for(int i=1; i=0; i--){ + Sorted_Arr[Count[Arr[i]-min]-1] = Arr[i]; + Count[Arr[i]-min]--; + } + + return Sorted_Arr; +} + +int main(){ + + int Arr[] = {47, 65, 20, 66, 25, 53, 64, 69, 72, 22, 74, 25, 53, 15, 42, 36, 4, 69, 86, 19}, N = 20; + int *Sorted_Arr; + + cout<<"\n\tOrignal Array = ";Print(Arr, N); + Sorted_Arr = Counting_Sort(Arr, N); + cout<<"\n\t Sorted Array = ";Print(Sorted_Arr, N); + cout<>n; int Array[n]; - cout<<"\nEnter any 6 Numbers for Unsorted Array : "; + cout<<"\nEnter any "<