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
Changes from all commits
Commits
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
101 changes: 101 additions & 0 deletions F2_10.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
int add(int c1, int n1, int d1, int c2, int n2, int d2, char result[], int len) {
int commonDenominator = d1 * d2;
int numerator1 = c1 * commonDenominator + n1 * d2;
int numerator2 = c2 * commonDenominator + n2 * d1;
int numeratorSum = numerator1 + numerator2;

int pos = 0;

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

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 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';
return 1;
}

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;

int numerator1 = c1 * commonDenominator + n1 * d2;
int numerator2 = c2 * commonDenominator + n2 * d1;
int numeratorDiff = numerator1 - numerator2;

int pos = 0;

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

int characteristicDiff = numeratorDiff / commonDenominator;
int temp = characteristicDiff;
if (temp == 0) {
result[pos++] = '0';
} else {
int divisor = 1;
while (temp / divisor >= 10) {
divisor *= 10;
}
while (divisor > 0) {
Comment on lines +69 to +74

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++] = '0' + temp / divisor;
temp %= divisor;
divisor /= 10;
}
}

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
}


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

result[pos] = '\0';
return 1;
}