-
Notifications
You must be signed in to change notification settings - Fork 1
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_14.c #14
base: main
Are you sure you want to change the base?
Create F2_14.c #14
Conversation
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 } '''
I am working on this pull request num F2_14. |
@@ -0,0 +1,82 @@ | |||
#include <stdbool.h> | |||
|
|||
void intToString(int num, char result[], int* pos) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We did a great job by adding a separate function to convert integers to strings, avoiding duplication of code in the add and subtract functions. We can reuse this helper function.
} | ||
|
||
bool subtract(int c1, int n1, int d1, int c2, int n2, int d2, char result[], int len) { | ||
int commonDenominator = d1 * d2; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see the variables in the code are very meaningful and understandable to the code reviewer and other developers, as single characters were not used. I appreciate that.
} | ||
|
||
bool add(int c1, int n1, int d1, int c2, int n2, int d2, char result[], int len) { | ||
int commonDenominator = d1 * d2; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see the variables in the code are very meaningful and understandable to the code reviewer and other developers, as single characters were not used. I appreciate that.
|
||
bool add(int c1, int n1, int d1, int c2, int n2, int d2, char result[], int len) { | ||
int commonDenominator = d1 * d2; | ||
int numeratorSum = n1 * d2 + n2 * d1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add a block of code to handle improper data/inputs such as commonDenominator not zero in add function. Adding this check points to make sure to avoid errors in the code.
|
||
bool subtract(int c1, int n1, int d1, int c2, int n2, int d2, char result[], int len) { | ||
int commonDenominator = d1 * d2; | ||
int numeratorDiff = n1 * d2 - n2 * d1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add a block of code to handle improper data/inputs such as commonDenominator not zero in subtract function. Adding this check points to make sure to avoid errors in the code.
return true; | ||
} | ||
|
||
bool subtract(int c1, int n1, int d1, int c2, int n2, int d2, char result[], int len) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see the len variable represents the maximum length of the result array, but it is not currently used in the code.This can lead to potential buffer overflow issues if the result exceeds the allocated space in the array. To Improve the code , we need to add block of code for subtract functions to ensure that the result fits within the provided length .
} | ||
} | ||
|
||
bool add(int c1, int n1, int d1, int c2, int n2, int d2, char result[], int len) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see the len variable represents the maximum length of the result array, but it is not currently used in the code.This can lead to potential buffer overflow issues if the result exceeds the allocated space in the array. To Improve the code , we need to add block of code for add functions to ensure that the result fits within the provided length .
@@ -0,0 +1,82 @@ | |||
#include <stdbool.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see we don't have comments for any line in the code, so can we add comments for the code for better understanding, readability, and explaining the purpose of that code blocks?.
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.