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_10.c #10

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

Create F2_10.c #10

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
}
'''
@Tanujasontakke
Copy link

I am working on this.

@Tanujasontakke
Copy link

In the implementation steps for normalization, arithmetic operations and the formatting of the results is clear and well organized. The program even handles negative values. With functions the code is easier to understand and maintain. Overall, it is a good implementation.

For better readability and description can we add comments to the code stating the function task more clearly?

Comment on lines +9 to +12
if (numeratorSum < 0) {
result[pos++] = '-';
numeratorSum = -numeratorSum;
}

Choose a reason for hiding this comment

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

can we ensure that the length of the result array is checked before appending characters to avoid buffer overflow?
if (numeratorSum < 0) {
if (pos < len - 1) {
result[pos++] = '-';
} else {
return 0;
}
numeratorSum = -numeratorSum;
}

}

int subtract(int c1, int n1, int d1, int c2, int n2, int d2, char result[], int len) {
int commonDenominator = d1 * d2;

Choose a reason for hiding this comment

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

can we add validation to handle the cases where denominators d1 or d2 are 0? which will help to prevent division by zero error.
if (d1 == 0 || d2 == 0) {
return 0; // denominator cannot be zero
}
int commonDenominator = d1 * d2;

Comment on lines +69 to +74
} else {
int divisor = 1;
while (temp / divisor >= 10) {
divisor *= 10;
}
while (divisor > 0) {

Choose a reason for hiding this comment

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

can we calculate the digits first and then convert? which will help to minimize the number of operations.
int numDigits = 0, tempCopy = temp;
while (tempCopy > 0) {
numDigits++;
tempCopy /= 10;
}
if (pos + numDigits < len) {
//converting part
}

}
}

result[pos++] = '.';

Choose a reason for hiding this comment

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

can we check the length of the result array, to avoid overflow issue? can we check this throughout the program?
if (pos < len - 1) {
result[pos++] = '.';
} else {
return 0; // Not enough space for result
}

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.

2 participants