From 73fa353b0ab208e8287648d0c5ff6290a854a390 Mon Sep 17 00:00:00 2001 From: Ashwek Swamy <39827514+ashwek@users.noreply.github.com> Date: Tue, 23 Oct 2018 17:47:43 +0530 Subject: [PATCH 1/4] Reduced Lines of code Using a "HexValues" string to store the possible hex values(0-9 A-F). Changed "void main" to "int main" --- Decimal To Hexadecimal .cpp | 47 +++++++++---------------------------- 1 file changed, 11 insertions(+), 36 deletions(-) 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< Date: Tue, 23 Oct 2018 18:01:24 +0530 Subject: [PATCH 2/4] Added Proper Indentation Added proper indentation and using shorthand. --- Decimal To Binary.cpp | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/Decimal To Binary.cpp b/Decimal To Binary.cpp index 3794d2a96..4ed1302c0 100644 --- a/Decimal To Binary.cpp +++ b/Decimal To Binary.cpp @@ -1,20 +1,17 @@ #include using namespace std; -int main() -{ +int main(){ int number; + cout <<"Enter number to find its binary : "; 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); + do{ + remainder = number%2; + number /= 2; + binary += (remainder*var); + var *= 10; + }while(number>0); cout< Date: Tue, 23 Oct 2018 18:42:36 +0530 Subject: [PATCH 3/4] Update Bubble Sort.cpp reduced the number of iteration done be inner for loop used for sorting to improve efficiency. --- Sorting/Bubble Sort.cpp | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/Sorting/Bubble Sort.cpp b/Sorting/Bubble Sort.cpp index c94a45efc..7d79f78e7 100644 --- a/Sorting/Bubble Sort.cpp +++ b/Sorting/Bubble Sort.cpp @@ -3,27 +3,24 @@ #include using namespace std; -int main() -{ - int n; +int main(){ + int n, temp; + cout<<"Enter size of Array = "; cin >> n; - int Array[n]; - cout<<"\nEnter any 6 Numbers for Unsorted Array : "; + int *Array = new int[n]; + cout<<"\nEnter any " < Date: Sat, 27 Oct 2018 21:18:51 +0530 Subject: [PATCH 4/4] Update Stack Using Array.cpp dynamically allocating memory to `stack` according to size given by user as input --- Datastructures/Stack Using Array.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) 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 {