Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat/fix/docs]: Improved on conversion code , length variable , asserts added #840

Open
wants to merge 45 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
67eeefe
Added asserts , number length check
lazy-dude Jul 8, 2021
0008c67
Remove extra empty lines
lazy-dude Jul 8, 2021
7185941
Auto indent
lazy-dude Jul 8, 2021
6e5e561
fix: corrected shown hex number.
lazy-dude Jul 8, 2021
905b80c
Changed to recommended typical structure
lazy-dude Jul 10, 2021
e771021
docs: Added some doxygen doc.
lazy-dude Jul 14, 2021
157f6d1
Update conversions/binary_to_decimal.c
lazy-dude Jul 17, 2021
bbb6024
Update conversions/binary_to_decimal.c
lazy-dude Jul 17, 2021
82e1f41
Update conversions/binary_to_decimal.c
lazy-dude Jul 17, 2021
1e4c396
Update conversions/binary_to_decimal.c
lazy-dude Jul 17, 2021
166dc0b
Update conversions/binary_to_decimal.c
lazy-dude Jul 17, 2021
57bcffa
Update conversions/binary_to_decimal.c
lazy-dude Jul 17, 2021
129f4b0
Update conversions/binary_to_decimal.c
lazy-dude Jul 17, 2021
8526600
Update conversions/binary_to_decimal.c
lazy-dude Jul 17, 2021
61bed00
Update conversions/binary_to_decimal.c
lazy-dude Jul 17, 2021
4d91c9d
Update conversions/binary_to_decimal.c
lazy-dude Jul 17, 2021
8defb93
Followed the two suggestions on code. Explain includes, intmax_t->uin…
lazy-dude Jul 17, 2021
6397733
Update conversions/binary_to_decimal.c
lazy-dude Jul 18, 2021
45e4eff
Update conversions/binary_to_decimal.c
lazy-dude Jul 18, 2021
6553640
Update conversions/binary_to_decimal.c
lazy-dude Jul 18, 2021
09df4a4
Update conversions/binary_to_decimal.c
lazy-dude Jul 18, 2021
63cf38c
Changes of some types.
lazy-dude Jul 18, 2021
e078df2
Update conversions/binary_to_decimal.c
lazy-dude Jul 21, 2021
0b199fb
Update conversions/binary_to_decimal.c
lazy-dude Jul 21, 2021
f66cd1b
Update conversions/binary_to_decimal.c
lazy-dude Jul 21, 2021
fd82d9e
Update conversions/binary_to_decimal.c
lazy-dude Jul 21, 2021
ebf83bb
Update conversions/binary_to_decimal.c
lazy-dude Jul 21, 2021
63a9a16
Update conversions/binary_to_decimal.c
lazy-dude Jul 21, 2021
a2ca660
Update conversions/binary_to_decimal.c
lazy-dude Jul 21, 2021
81ab47c
Considered some suggestions
lazy-dude Jul 21, 2021
0f96de5
Update conversions/binary_to_decimal.c
lazy-dude Jul 22, 2021
254cabf
Update conversions/binary_to_decimal.c
lazy-dude Jul 22, 2021
8c1b548
Update conversions/binary_to_decimal.c
lazy-dude Jul 22, 2021
8632490
Added link for explaination.
lazy-dude Jul 22, 2021
8fa92c5
Update conversions/binary_to_decimal.c
lazy-dude Jul 23, 2021
4ab5a25
Update conversions/binary_to_decimal.c
lazy-dude Jul 23, 2021
da73878
Update conversions/binary_to_decimal.c
lazy-dude Jul 23, 2021
71c17ee
return -> returns
lazy-dude Jul 23, 2021
c3433f7
Update conversions/binary_to_decimal.c
lazy-dude Jul 26, 2021
53e719b
Update conversions/binary_to_decimal.c
lazy-dude Jul 26, 2021
af4b346
Update conversions/binary_to_decimal.c
lazy-dude Jul 26, 2021
aa8e55b
Update conversions/binary_to_decimal.c
lazy-dude Jul 26, 2021
0d61a4a
Update conversions/binary_to_decimal.c
lazy-dude Jul 27, 2021
82fb3ae
Update conversions/binary_to_decimal.c
lazy-dude Jul 27, 2021
9612325
Added description
lazy-dude Jul 27, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 100 additions & 11 deletions conversions/binary_to_decimal.c
Original file line number Diff line number Diff line change
@@ -1,24 +1,113 @@
/**
* Modified 07/12/2017, Kyler Smith
*
/**
* @file binary_to_decimal.c
lazy-dude marked this conversation as resolved.
Show resolved Hide resolved
* @brief Converts a binary number to a decimal one.
Panquesito7 marked this conversation as resolved.
Show resolved Hide resolved
lazy-dude marked this conversation as resolved.
Show resolved Hide resolved
* @details
* A binary number is input , it is check to be binary
lazy-dude marked this conversation as resolved.
Show resolved Hide resolved
* then number is converted to a decimal number.
lazy-dude marked this conversation as resolved.
Show resolved Hide resolved
* Some tests are added too.
lazy-dude marked this conversation as resolved.
Show resolved Hide resolved
* Modified 07/12/2017
* Modified 2021
lazy-dude marked this conversation as resolved.
Show resolved Hide resolved
* @author Kyler Smith
lazy-dude marked this conversation as resolved.
Show resolved Hide resolved
* @author [lazy-dude] (https://github.com/lazy-dude)
lazy-dude marked this conversation as resolved.
Show resolved Hide resolved
*/

// includes
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a one-line description of what the library/header is for (see the example below).

#include <stdio.h>    /// for IO operations
#include <assert.h>  /// for assert


// function prototypes
bool is_binary(intmax_t num);
int num_len(intmax_t num);
intmax_t binary_decimal(intmax_t num);
void test(void);
lazy-dude marked this conversation as resolved.
Show resolved Hide resolved

int main()
/**
* @brief main function
* @param void
* @returns 0 on exit
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* @brief main function
* @param void
* @returns 0 on exit
*/
/**
* @brief Main function
* @returns 0 on exit
*/

int main(void)
lazy-dude marked this conversation as resolved.
Show resolved Hide resolved
{
int remainder, number = 0, decimal_number = 0, temp = 1;
printf("\n Enter any binary number= ");
scanf("%d", &number);
test();
lazy-dude marked this conversation as resolved.
Show resolved Hide resolved
printf("All tests passed.\n");
return 0;
}

/**
* @brief is_binary checks whether num is a binary one
lazy-dude marked this conversation as resolved.
Show resolved Hide resolved
* @param num to be checked if it has binary representation
lazy-dude marked this conversation as resolved.
Show resolved Hide resolved
* @return boolean true if num is binary false if not
lazy-dude marked this conversation as resolved.
Show resolved Hide resolved
*/
bool is_binary(intmax_t num)
{
int remainder = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using uint64_t for non-negative values (or their appropriate size: uint32_t, uint16_t, uint8_t) or int64_t for negative values. Requires adding the inttypes.h library. Check other parts of the code (reference). 🙂

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uintmax_t has the biggest length for machine running the code. I would prefer it over uint64_t which has a fixed size.


while (num > 0) {
remainder = num % 10;
if (remainder == 0 || remainder == 1) {
num /= 10;
continue;
} else
return false;
}
return true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a bit of documentation here of what this code does.

}

/**
* @brief num_len finds length of an intmax_t num
* @param num whose length to be computed
* @return i int length of num
lazy-dude marked this conversation as resolved.
Show resolved Hide resolved
*/
int num_len(intmax_t num)
{
int i;
lazy-dude marked this conversation as resolved.
Show resolved Hide resolved
for (i = 0; num > 0; i++) {
num /= 10;
}
return i;
}

/**
* @brief binary_decimal function does the actual job of conversion
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @brief binary_decimal function does the actual job of conversion
* @brief The `binary_decimal` function does the actual job of conversion

lazy-dude marked this conversation as resolved.
Show resolved Hide resolved
* @param number binary to be converted
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param number binary to be converted
* @param number the binary to be converted

lazy-dude marked this conversation as resolved.
Show resolved Hide resolved
* @return decimal_number decimal representation of binary number
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return decimal_number decimal representation of binary number
* @return decimal_number decimal representation of a binary number

lazy-dude marked this conversation as resolved.
Show resolved Hide resolved
*/
intmax_t binary_decimal(intmax_t number)
{
intmax_t remainder, decimal_number = 0, temp = 1;

int length = num_len(INTMAX_MAX) - 1;

assert(number>=0);
assert(num_len(number) <= length);
assert(is_binary(number));

// Iterate over the number until the end.
while (number > 0)
{
while (number > 0) {
remainder = number % 10;
number = number / 10;
decimal_number += remainder * temp;
temp = temp * 2; // used as power of 2
temp = temp * 2; // used as power of 2
}

printf("%d\n", decimal_number);
return decimal_number;
}

/**
* @brief some tests using assert
* @param void
* @return void
*/
lazy-dude marked this conversation as resolved.
Show resolved Hide resolved
void test(void)
lazy-dude marked this conversation as resolved.
Show resolved Hide resolved
{
assert(binary_decimal(0)==0);
assert(binary_decimal(1)==1);
assert(binary_decimal(1110001)==113);
assert(binary_decimal(11111111)==255);
assert(binary_decimal(10000000000)==1024);
assert(binary_decimal(1001110100000100)==40196);
lazy-dude marked this conversation as resolved.
Show resolved Hide resolved
}

Panquesito7 marked this conversation as resolved.
Show resolved Hide resolved
9 changes: 5 additions & 4 deletions conversions/decimal_to_hexa.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ int main()
* number****************/
void decimal2Hexadecimal(long num)
{
char hex_letters[]="abcdef";
long decimalnum = num;
long quotient, remainder;
int i, j = 0;
Expand All @@ -29,17 +30,17 @@ void decimal2Hexadecimal(long num)
{
remainder = quotient % 16;
if (remainder < 10)
hexadecimalnum[j++] = 48 + remainder;
hexadecimalnum[j++] = '0' + remainder;

else
hexadecimalnum[j++] = 55 + remainder;
hexadecimalnum[j++] = hex_letters[remainder-10];// 'A'

quotient = quotient / 16;
}

// print the hexadecimal number

for (i = j; i >= 0; i--)
printf("0x");
for (i = j-1; i >= 0; i--)
{
printf("%c", hexadecimalnum[i]);
}
Expand Down