Skip to content
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
80 changes: 80 additions & 0 deletions F2_4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
void intToString(int num, char result[], int* pos) {
if (num == 0) {
result[(*pos)++] = '0';
return;
}

int digits[10];
int count = 0;
if (num < 0) {
result[(*pos)++] = '-';
num = -num;
}
while (num > 0) {
digits[count++] = num % 10;
num /= 10;
}
for (int i = count - 1; i >= 0; i--) {
result[(*pos)++] = '0' + digits[i];
}
}

int 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;
int characteristicSum = c1 + c2 + numeratorSum / commonDenominator;
numeratorSum %= commonDenominator;

int pos = 0;
intToString(characteristicSum, result, &pos);

result[pos++] = '.';

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;
}
Copy link

Choose a reason for hiding this comment

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

Both the functions add and subtract always return true. Why don't we add a if condition like this:
if(pos < len){
result[pos] = '\0';
return 1;
}
return 0;


int 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;
int characteristicDiff = c1 - c2 + numeratorDiff / commonDenominator;
Copy link

Choose a reason for hiding this comment

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

What if in the worst case we get d1 and d2 as 0. If that's the case then we would be dealing with division by zero which will cause a runtime error. Can we add a try catch block in this part of code so that we can know when we run into such cases.

numeratorDiff %= commonDenominator;

int pos = 0;
intToString(characteristicDiff, result, &pos);

result[pos++] = '.';

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;
}
}
Copy link

Choose a reason for hiding this comment

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

In both the add and subtract functions the below part of the code is repeated.
result[pos++] = '.';

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;
    }
}

Why don't we create a new function with the same set of operations so that we can call the newly created function from within the add and subtract functions instead of repeating the code in both the functions.


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