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

Create F2_15.c #15

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Create F2_15.c #15

wants to merge 1 commit into from

Conversation

igorsteinmacher
Copy link
Contributor

The application you will be writing for is intended to run on a small, inexpensive embedded device that does not have a floating point unit. This means that the processor can only do integer arithmetic. However, the device does have a display and needs to display the result of floating point arithmetic to the user. Because this is a small inexpensive processor there is no support for strings or any mathematical library functions. You must write all the code yourselves performing all the floating point math with integers only and no strings! Character arrays, or C strings, can't tell you their size but they do end with a '\0' character.

The requirements for the code are to write one of the functions presented below. All the functions return a boolean value indicating the success or failure of the operation. Improper data is the most likely cause for a function to fail. You may add your own additional helper functions (I had at least a half dozen) but you must include them for the review. Do not change the interface to these functions.

These two functions take as parameters two sets of mantissa and characteristic and a char array to hold the result of the arithmetic operations. The result of the add or subtract should be converted into char's and placed in the result array. The array must end with a '\0'. The 'len' parameter tells how many characters can be placed on the result array. For these functions to return true you must at a minimum store the characteristic of the result. If the result is a non-integer place as many of the digits as will fit in the result after a decimal point.

bool add(int c1, int n1, int d1, int c2, int n2, int d2, char result[], int len);
bool subtract(int c1, int n1, int d1, int c2, int n2, int d2, char result[], int len); 
...
...
char answer[100];
int c1, n1, d1;
int c2, n2, d2;
 
c1 = 1;
n1 = 1;
d1 = 2;
 
c2 = 2;
n2 = 2;
d2 = 3; 
 
//if the C string could hold at least the characteristic if(add(c1, n1, d1, c2, n2, d2, answer, 100))
{
    //display string with answer 4.166666666...
}
else
{
    //display error message
}
'''

The application you will be writing for is intended to run on a small, inexpensive embedded device that does not have a floating point unit. This means that the processor can only do integer arithmetic. However, the device does have a display and needs to display the result of floating point arithmetic to the user.  Because this is a small inexpensive processor there is no support for strings or any mathematical library functions. You must write all the code yourselves performing all the floating point math with integers only and no strings! Character arrays, or C strings, can't tell you their size but they do end with a '\0' character.

The requirements for the code are to write one of the functions presented below. All the functions return a boolean value indicating the success or failure of the operation. Improper data is the most likely cause for a function to fail. You may add your own additional helper functions (I had at least a half dozen) but you must include them for the review. Do not change the interface to these functions.


These two functions take as parameters two sets of mantissa and characteristic and a char array to hold the result of the arithmetic operations. The result of the add or subtract should be converted into char's and placed in the result array. The array must end with a '\0'. The 'len' parameter tells how many characters can be placed on the result array. For these functions to return true you must at a minimum store the characteristic of the result. If the result is a non-integer place as many of the digits as will fit in the result after a decimal point.

```c
bool add(int c1, int n1, int d1, int c2, int n2, int d2, char result[], int len);
bool subtract(int c1, int n1, int d1, int c2, int n2, int d2, char result[], int len); 
...
...
char answer[100];
int c1, n1, d1;
int c2, n2, d2;
 
c1 = 1;
n1 = 1;
d1 = 2;
 
c2 = 2;
n2 = 2;
d2 = 3; 
 
//if the C string could hold at least the characteristic if(add(c1, n1, d1, c2, n2, d2, answer, 100))
{
    //display string with answer 4.166666666...
}
else
{
    //display error message
}
'''
@ih295
Copy link

ih295 commented Jun 19, 2024

I am working on this

@ih295
Copy link

ih295 commented Jun 19, 2024

Things to improve

Looking at the code, there are a few areas of improvement. Some of these include:
(1) Lack of context
(2) Lack of helper functions
(3) Lack of buffer overflow preventive measures
(4) Improper division handling

I have a few recommendations for these problems:
(1) Lack of context
There is a lack of context necessary for understanding what the code does and what it is being used for. Can we get some more comments for context, to improve understanding?

(2) Lack of helper functions
Both the add and subtract functions utilize obvious integer to string conversion. Can we get a function that does that conversion, and replace those lines with code that calls it?
Outside of a difference in one variable's name, this block of code exists in both add and subtract function. Can we get a helper function for this?

    int pos = 0;

    if (numeratorSum < 0) {
        result[pos++] = '-';
        numeratorSum = -numeratorSum;
    }

    int characteristicSum = numeratorSum / commonDenominator;
    int temp = characteristicSum;
    if (temp == 0) {
        result[pos++] = '0';
    } else {
        int divisor = 1;
        while (temp / divisor >= 10) {
            divisor *= 10;
        }
        while (divisor > 0) {
            result[pos++] = '0' + temp / divisor;
            temp %= divisor;
            divisor /= 10;
        }
    }

    result[pos++] = '.';

    numeratorSum %= commonDenominator;
    if (numeratorSum == 0) {
        result[pos++] = '0';
    } else {
        int divisor = commonDenominator / 10;
        while (divisor > 0 && numeratorSum / divisor == 0) {
            result[pos++] = '0';
            divisor /= 10;
        }
        while (divisor > 0) {
            result[pos++] = '0' + numeratorSum / divisor;
            numeratorSum %= divisor;
            divisor /= 10;
        }
    }

    result[pos] = '\0';

(3) Lack of buffer overflow preventive measures
Both the add and subtract functions contain a length variable, presumably was meant to be used to prevent buffer overflow. Can we get code that makes sure internal position variables are less than the length variables?
I recommend adding a length check at several lines in the add and subtract functions.
In the add function, I recommend changing if(numeratorSum <0) to if((numeratorSum < 0) && (pos++ < len)) in the add function. In that same function, I recommend also changing if(temp == 0) to if((temp == 0) && (pos++ < len)). I also recommend changing while(divisor > 0 to while((divisor > 0) && (pos++ <len)). The add function has other areas to change, such as result[pos++] = '.', if(numeratorSum == 0), while(divisor > 0 && numeratorSum / divisor == 0),
while(divisor > 0), and result[pos] = '\0'. I recommend changing these to if(pos++ < len) { result[pos++] = '.' }, if((numeratorSum == 0) && (pos++ < len)), while((divisor > 0 && numeratorSum / divisor == 0) && (pos++ < len)), while((divisor > 0) && (pos++ < len)), and if(pos < len) { result[pos] = '\0'; } respectively.

Looking at the subtract function, these same changes need to be made, with variables such as numeratorSum being renamed to numeratorDiff, so I recommend making these changes in that function too.

I also recommend implementing measures to handle buffer underflow, so I recommend including checks that ensure positional variables are greater than or equal to 0.
Repeating what I stated earlier:
In the add function, I recommend changing if(numeratorSum <0) to if((numeratorSum < 0) && (pos++ < len) &&(pos++ >= 0)) in the add function. In that same function, I recommend also changing if(temp == 0) to if((temp == 0) && (pos++ < len) &&(pos++ >= 0)). I also recommend changing while(divisor > 0 to while((divisor > 0) && (pos++ <len) &&(pos++ >= 0)). The add function has other areas to change, such as result[pos++] = '.', if(numeratorSum == 0), while(divisor > 0 && numeratorSum / divisor == 0),
while(divisor > 0), and result[pos] = '\0'. I recommend changing these to if(pos++ < len) { result[pos++] = '.' }, if((numeratorSum == 0) && (pos++ < len) &&(pos++ >= 0)), while((divisor > 0 && numeratorSum / divisor == 0) && (pos++ < len) &&(pos++ >= 0)), while((divisor > 0) && (pos++ < len) &&(pos++ >= 0)), and if(pos < len && pos >= 0) { result[pos] = '\0'; } respectively.

(4) Improper division handling
There are several lines of code that use variables as denominators for division. Can we get code that ensures those denominator variables are not 0, to ensure the results are not undefined?
Potential fixes include:

int add(int c1, int n1, int d1, int c2, int n2, int d2, char result[], int len) {
    int commonDenominator = d1 * d2;
    if(commonDenominator != 0)
    {
        int numerator1 = c1 * commonDenominator + n1 * d2;
        int numerator2 = c2 * commonDenominator + n2 * d1;
        //Include rest of add function code here...
int subtract(int c1, int n1, int d1, int c2, int n2, int d2, char result[], int len) {
   int commonDenominator = d1 * d2;
   if(commonDenominator != 0)
   {
       int numerator1 = c1 * commonDenominator + n1 * d2;
       int numerator2 = c2 * commonDenominator + n2 * d1;
       //Include rest of subtract function here...

Pros and Cons

Pros

There are some pros in this code. Some of these include proper variable naming, which makes it easier to understand the code.

Cons

The only cons that come to mind are those listed in Things to improve.

Thank you for reading this, feel free to contact me if you have any questions, comments, or concerns.

Copy link

@Tharunya668 Tharunya668 left a comment

Choose a reason for hiding this comment

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

Few suggestions that can improve the code:

  • Create a helper function to format the result as a string to reduce duplication.
  • Ensure proper handling of edge cases such as zero denominators and buffer overflow.
  • Use more descriptive variable names to improve clarity.

Refactored version of the code -

#include <stdio.h>
#include <stdlib.h>

void formatResult(int numerator, int denominator, char result[], int len) {
    int pos = 0;
    int characteristic = numerator / denominator;

    // Handle negative results
    if (numerator < 0) {
        result[pos++] = '-';
        numerator = -numerator;
    }

    // Handle characteristic part
    int temp = characteristic;
    if (temp == 0) {
        result[pos++] = '0';
    } else {
        int divisor = 1;
        while (temp / divisor >= 10) {
            divisor *= 10;
        }
        while (divisor > 0) {
            result[pos++] = '0' + temp / divisor;
            temp %= divisor;
            divisor /= 10;
        }
    }

    // Handle fractional part
    result[pos++] = '.';
    numerator %= denominator;
    if (numerator == 0) {
        result[pos++] = '0';
    } else {
        int divisor = denominator / 10;
        while (divisor > 0 && numerator / divisor == 0) {
            result[pos++] = '0';
            divisor /= 10;
        }
        while (divisor > 0) {
            result[pos++] = '0' + numerator / divisor;
            numerator %= divisor;
            divisor /= 10;
        }
    }

    result[pos] = '\0';
}

int add(int c1, int n1, int d1, int c2, int n2, int d2, char result[], int len) {
    // Check for zero denominators
    if (d1 == 0 || d2 == 0) {
        snprintf(result, len, "Error: Zero denominator");
        return -1;
    }

    // Calculate common denominator and numerators
    int commonDenominator = d1 * d2;
    int numerator1 = c1 * commonDenominator + n1 * d2;
    int numerator2 = c2 * commonDenominator + n2 * d1;
    int numeratorSum = numerator1 + numerator2;

    // Format the result
    formatResult(numeratorSum, commonDenominator, result, len);
    return 1;
}

int subtract(int c1, int n1, int d1, int c2, int n2, int d2, char result[], int len) {
    // Check for zero denominators
    if (d1 == 0 || d2 == 0) {
        snprintf(result, len, "Error: Zero denominator");
        return -1;
    }

    // Calculate common denominator and numerators
    int commonDenominator = d1 * d2;
    int numerator1 = c1 * commonDenominator + n1 * d2;
    int numerator2 = c2 * commonDenominator + n2 * d1;
    int numeratorDiff = numerator1 - numerator2;

    // Format the result
    formatResult(numeratorDiff, commonDenominator, result, len);
    return 1;
}

@Tharunya668
Copy link

I am working on this

@ih295
Copy link

ih295 commented Jun 19, 2024

I am working on this

I already worked on this. Were there none left?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants