Merge branch 'major-corrections-to-files' into document/others

* major-corrections-to-files: (28 commits)
  fixed decimal to roman
  fixed decimal to hex
  fixed CPPLINT + added method 2
  fix buzz number
  updating DIRECTORY.md
  Added documentation (#802)
  updating DIRECTORY.md
  Correction : Fixed Array Overflow
  Update doubly_linked_list.cpp
  added deleted files back - not really needed but there is a redundant github action that required it
  updating DIRECTORY.md
  cpplint issues fixed in sorting folder
  fix dynamic array issues in sorting folder
  fix dynamic array
  cpplint and msvc fixes for - sorting
  sorting fixes for MSVC and CPPLINT
  fix CPPLINT in sorting folder
  updating DIRECTORY.md
  MSVC does not know cstring-use string-for toString
  fixed dynamic array
  ...

# Conflicts:
#	math/fast_power.cpp
#	sorting/non_recursive_merge_sort.cpp
#	sorting/quick_sort.cpp
This commit is contained in:
Krishna Vedala 2020-05-27 18:41:01 -04:00
commit 7d2078a90c
No known key found for this signature in database
GPG Key ID: BA19ACF8FC8792F7
6 changed files with 220 additions and 133 deletions

View File

@ -61,6 +61,7 @@ void double_linked_list::remove(int x) {
t->prev->next = t->next;
t->next->prev = t->prev;
}
delete t;
}
void double_linked_list::search(int x) {

View File

@ -1,17 +1,20 @@
//A buzz number is a number that is either divisble by 7 or has last digit as 7.
/**
* @file
* @brief A buzz number is a number that is either divisible by 7 or has last
* digit as 7.
*/
#include <iostream>
using namespace std;
int main()
{
int n, t;
cin >> t;
while (t--)
{
cin >> n;
if ((n % 7 == 0) || (n % 10 == 7))
cout << n << " is a buzz number" << endl;
else
cout << n << " is not a buzz number" << endl;
}
return 0;
/** main function */
int main() {
int n, t;
std::cin >> t;
while (t--) {
std::cin >> n;
if ((n % 7 == 0) || (n % 10 == 7))
std::cout << n << " is a buzz number" << std::endl;
else
std::cout << n << " is not a buzz number" << std::endl;
}
return 0;
}

View File

@ -1,25 +1,55 @@
// This function convert decimal to binary number
//
/**
* @file
* @brief Function to convert decimal number to binary representation
*/
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter a number:";
cin >> number;
int remainder, binary = 0, var = 1;
/**
* This method converts the bit representation and stores it as a decimal
* number.
*/
void method1(int 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 << "the binary is :";
cout << binary;
cout << endl;
return 0;
do {
remainder = number % 2;
number = number / 2;
binary = binary + (remainder * var);
var = var * 10;
} while (number > 0);
std::cout << "Method 1 : " << binary << std::endl;
}
/**
* This method stores each bit value from LSB to MSB and then prints them back
* from MSB to LSB
*/
void method2(int number) {
int num_bits = 0;
char bit_string[50];
do {
bool bit = number & 0x01; // get last bit
if (bit)
bit_string[num_bits++] = '1';
else
bit_string[num_bits++] = '0';
number >>= 1; // right shift bit 1 bit
} while (number > 0);
std::cout << "Method 2 : ";
while (num_bits >= 0)
std::cout << bit_string[num_bits--]; // print from MSB to LSB
std::cout << std::endl;
}
int main() {
int number;
std::cout << "Enter a number:";
std::cin >> number;
method1(number);
method2(number);
return 0;
}

View File

@ -1,28 +1,34 @@
/**
* @file
* @brief Convert decimal number to hexadecimal representation
*/
#include <iostream>
using namespace std;
/**
* Main program
*/
int main(void) {
int valueToConvert = 0; // Holds user input
int hexArray[8]; // Contains hex values backwards
int i = 0; // counter
char HexValues[] = "0123456789ABCDEF";
int main(void)
{
int valueToConvert = 0; //Holds user input
int hexArray[8]; //Contains hex values backwards
int i = 0; //counter
char HexValues[] = "0123456789ABCDEF";
std::cout << "Enter a Decimal Value"
<< std::endl; // Displays request to stdout
std::cin >>
valueToConvert; // Stores value into valueToConvert via user input
cout << "Enter a Decimal Value" << endl; //Displays request to stdout
cin >> valueToConvert; //Stores value into valueToConvert via user input
while (valueToConvert > 15) { // Dec to Hex Algorithm
hexArray[i++] = valueToConvert % 16; // Gets remainder
valueToConvert /= 16;
// valueToConvert >>= 4; // This will divide by 2^4=16 and is faster
}
hexArray[i] = valueToConvert; // Gets last value
while (valueToConvert > 15)
{ //Dec to Hex Algorithm
hexArray[i++] = valueToConvert % 16; //Gets remainder
valueToConvert /= 16;
}
hexArray[i] = valueToConvert; //Gets last value
std::cout << "Hex Value: ";
while (i >= 0) std::cout << HexValues[hexArray[i--]];
cout << "Hex Value: ";
while (i >= 0)
cout << HexValues[hexArray[i--]];
cout << endl;
return 0;
std::cout << std::endl;
return 0;
}

View File

@ -1,97 +1,76 @@
//This Programme Converts a given decimal number in the range [0,4000)
//to both Lower case and Upper case Roman Numeral
/**
* @file
* @brief This Programme Converts a given decimal number in the range [0,4000)
* to both Lower case and Upper case Roman Numeral
*/
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
using namespace std;
//This functions fills a string with character c, n times and returns it
string fill(char c, int n)
{
string s = "";
while (n--)
s += c;
/** This functions fills a string with character c, n times and returns it
* @note This can probably be replace by `memcpy` function.
*/
std::string fill(char c, int n) {
std::string s = "";
while (n--) s += c;
return s;
}
//to convert to lowercase Roman Numeral
// the function works recursively
string tolowerRoman(int n)
{
if (n < 4)
return fill('i', n);
if (n < 6)
return fill('i', 5 - n) + "v";
if (n < 9)
return string("v") + fill('i', n - 5);
if (n < 11)
return fill('i', 10 - n) + "x";
if (n < 40)
return fill('x', n / 10) + tolowerRoman(n % 10);
if (n < 60)
return fill('x', 5 - n / 10) + 'l' + tolowerRoman(n % 10);
/** to convert to lowercase Roman Numeral
* the function works recursively
*/
std::string tolowerRoman(int n) {
if (n < 4) return fill('i', n);
if (n < 6) return fill('i', 5 - n) + "v";
if (n < 9) return std::string("v") + fill('i', n - 5);
if (n < 11) return fill('i', 10 - n) + "x";
if (n < 40) return fill('x', n / 10) + tolowerRoman(n % 10);
if (n < 60) return fill('x', 5 - n / 10) + 'l' + tolowerRoman(n % 10);
if (n < 90)
return string("l") + fill('x', n / 10 - 5) + tolowerRoman(n % 10);
if (n < 110)
return fill('x', 10 - n / 10) + "c" + tolowerRoman(n % 10);
if (n < 400)
return fill('c', n / 100) + tolowerRoman(n % 100);
if (n < 600)
return fill('c', 5 - n / 100) + 'd' + tolowerRoman(n % 100);
return std::string("l") + fill('x', n / 10 - 5) + tolowerRoman(n % 10);
if (n < 110) return fill('x', 10 - n / 10) + "c" + tolowerRoman(n % 10);
if (n < 400) return fill('c', n / 100) + tolowerRoman(n % 100);
if (n < 600) return fill('c', 5 - n / 100) + 'd' + tolowerRoman(n % 100);
if (n < 900)
return string("d") + fill('c', n / 100 - 5) + tolowerRoman(n % 100);
if (n < 1100)
return fill('c', 10 - n / 100) + "m" + tolowerRoman(n % 100);
if (n < 4000)
return fill('m', n / 1000) + tolowerRoman(n % 1000);
return std::string("d") + fill('c', n / 100 - 5) +
tolowerRoman(n % 100);
if (n < 1100) return fill('c', 10 - n / 100) + "m" + tolowerRoman(n % 100);
if (n < 4000) return fill('m', n / 1000) + tolowerRoman(n % 1000);
return "?";
}
//to convert to uppercase Roman Numeral
// the function works recursively
string toupperRoman(int n)
{
if (n < 4)
return fill('I', n);
if (n < 6)
return fill('I', 5 - n) + "V";
if (n < 9)
return string("V") + fill('I', n - 5);
if (n < 11)
return fill('I', 10 - n) + "X";
if (n < 40)
return fill('X', n / 10) + toupperRoman(n % 10);
if (n < 60)
return fill('X', 5 - n / 10) + 'L' + toupperRoman(n % 10);
/** to convert to uppercase Roman Numeral
* the function works recursively
*/
std::string toupperRoman(int n) {
if (n < 4) return fill('I', n);
if (n < 6) return fill('I', 5 - n) + "V";
if (n < 9) return std::string("V") + fill('I', n - 5);
if (n < 11) return fill('I', 10 - n) + "X";
if (n < 40) return fill('X', n / 10) + toupperRoman(n % 10);
if (n < 60) return fill('X', 5 - n / 10) + 'L' + toupperRoman(n % 10);
if (n < 90)
return string("L") + fill('X', n / 10 - 5) + toupperRoman(n % 10);
if (n < 110)
return fill('X', 10 - n / 10) + "C" + toupperRoman(n % 10);
if (n < 400)
return fill('C', n / 100) + toupperRoman(n % 100);
if (n < 600)
return fill('C', 5 - n / 100) + 'D' + toupperRoman(n % 100);
return std::string("L") + fill('X', n / 10 - 5) + toupperRoman(n % 10);
if (n < 110) return fill('X', 10 - n / 10) + "C" + toupperRoman(n % 10);
if (n < 400) return fill('C', n / 100) + toupperRoman(n % 100);
if (n < 600) return fill('C', 5 - n / 100) + 'D' + toupperRoman(n % 100);
if (n < 900)
return string("D") + fill('C', n / 100 - 5) + toupperRoman(n % 100);
if (n < 1100)
return fill('C', 10 - n / 100) + "M" + toupperRoman(n % 100);
if (n < 4000)
return fill('M', n / 1000) + toupperRoman(n % 1000);
return std::string("D") + fill('C', n / 100 - 5) +
toupperRoman(n % 100);
if (n < 1100) return fill('C', 10 - n / 100) + "M" + toupperRoman(n % 100);
if (n < 4000) return fill('M', n / 1000) + toupperRoman(n % 1000);
return "?";
}
//main function
int main()
{
/** main function */
int main() {
int n;
cout << "\t\tRoman numbers converter\n\n";
cout << "Type in decimal number between 0 up to 4000 (exclusive): ";
cin >> n;
cout << n << " in Upper Roman Numerals is " << toupperRoman(n) << "\n";
cout << n << " in Lower Roman Numerals is " << tolowerRoman(n) << "\n";
std::cout << "\t\tRoman numbers converter\n\n";
std::cout << "Type in decimal number between 0 up to 4000 (exclusive): ";
std::cin >> n;
std::cout << n << " in Upper Roman Numerals is " << toupperRoman(n) << "\n";
std::cout << n << " in Lower Roman Numerals is " << tolowerRoman(n) << "\n";
return 0;
}

View File

@ -1,15 +1,62 @@
<<<<<<< HEAD
/* C implementation QuickSort */
#include <iostream>
int partition(int arr[], int low, int high) {
int pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element
=======
/**
*
* copyright The Algorithms
* Author -
* Correction - ayaankhan98
*
* Implementation Details -
* Quick Sort is a divide and conquer algorithm. It picks and element as
* pivot and partition the given array around the picked pivot. There
* are many different versions of quickSort that pick pivot in different
* ways.
*
* 1. Always pick the first element as pivot
* 2. Always pick the last element as pivot (implemented below)
* 3. Pick a random element as pivot
* 4. Pick median as pivot
*
* The key process in quickSort is partition(). Target of partition is,
* given an array and an element x(say) of array as pivot, put x at it's
* correct position in sorted array and put all smaller elements (samller
* than x) before x, and put all greater elements (greater than x) after
* x. All this should be done in linear time
*
*/
#include <cstdlib>
#include <iostream>
/**
* This function takes last element as pivot, places
* the pivot element at its correct position in sorted
* array, and places all smaller (smaller than pivot)
* to left of pivot and all greater elements to right
* of pivot
*
*/
int partition(int arr[], int low, int high) {
int pivot = arr[high]; // taking the last element as pivot
int i = (low - 1); // Index of smaller element
>>>>>>> major-corrections-to-files
for (int j = low; j < high; j++) {
// If current element is smaller than or
// equal to pivot
if (arr[j] <= pivot) {
<<<<<<< HEAD
i++; // increment index of smaller element
=======
i++; // increment index of smaller element
>>>>>>> major-corrections-to-files
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
@ -21,17 +68,38 @@ int partition(int arr[], int low, int high) {
return (i + 1);
}
<<<<<<< HEAD
void quickSort(int arr[], int low, int high) {
if (low < high) {
int p = partition(arr, low, high);
=======
/**
* The main function that implements QuickSort
* arr[] --> Array to be sorted,
* low --> Starting index,
* high --> Ending index
*/
void quickSort(int arr[], int low, int high) {
if (low < high) {
int p = partition(arr, low, high);
>>>>>>> major-corrections-to-files
quickSort(arr, low, p - 1);
quickSort(arr, p + 1, high);
}
}
<<<<<<< HEAD
void show(int arr[], int size) {
for (int i = 0; i < size; i++) std::cout << arr[i] << "\n";
=======
// prints the array after sorting
void show(int arr[], int size) {
for (int i = 0; i < size; i++)
std::cout << arr[i] << " ";
std::cout << "\n";
>>>>>>> major-corrections-to-files
}
// Driver program to test above functions