Skip to content

Commit

Permalink
Merge pull request TheAlgorithms#146 from ashwek/master
Browse files Browse the repository at this point in the history
Reduced Lines of code
  • Loading branch information
ashwek authored Feb 9, 2019
2 parents a378f47 + 1f638c9 commit 9461beb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 39 deletions.
9 changes: 6 additions & 3 deletions Datastructures/Stack Using Array.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include<iostream>
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";
}
Expand Down Expand Up @@ -42,6 +42,9 @@ void topmost()
}
int main()
{
cout<<"\nEnter Size of stack : ";
cin>>size;
stack = new int[size];
int ch, x;
do
{
Expand Down
47 changes: 11 additions & 36 deletions Decimal To Hexadecimal .cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<<HexValues[hexArray[i--]];

cout << endl;
return 0;
}

0 comments on commit 9461beb

Please sign in to comment.