1

Even though I know turbo c is completely obsolete now, my instructor has put a condition to code in it. I am having an issue that when I am trying to pass a double value to a function it is not behaving properly. I am getting fluctuating output several of the time, sometimes even weird. Let's see my code first:

#include<stdio.h>
#include<conio.h>

double func(double input) {
    return input * input;
}

double simpson1By3(double initial, double final, double parts) {
    double sum = 0;
    double h = (final - initial) / parts;
    double oddSum = 0, evenSum = 0;
    int i;
    printf("%f %f %f\n", initial, final, parts);
    printf("%f\n", h);
    printf("%f %f\n", evenSum, oddSum);
    sum += func(initial) + func(final);
    printf("%f %f\n", evenSum, oddSum);
    for (i = 1; i < parts; i = i + 2) {
        oddSum += func(initial + (i * h));
    }
    for (i = 2; i < parts; i = i + 2) {
        evenSum += func(initial + (i * h));
    }
    oddSum *= 4;
    evenSum *= 2;
    printf("%f %f\n", evenSum, oddSum);
    sum += evenSum + oddSum;
    sum *= h / 3;
    return sum;
}

int main() {
    clrscr();
    printf ("%f", simpson1By3(0, 6, 6));
    getch();
    return 0;
}

sample output: enter image description here

What am I doing wrong there? Why are the arguments passed printed erroneously there along with other variables there and why is that -0 printing? Please help. I have tried finding something similar to it in forums but completely failed. Please help.

enter image description here

enter image description here

enter image description here

  • Seems to work on ideone (removing conio stuff): https://ideone.com/gB1QwK Does turbo-c have a separate floating point library? – 001 Sep 04 '18 at 17:24
  • its working fine everywhere else turbo c. for the knowledge purpose i just need to know whats wrong that turbo c is doing there. @johnny i really dont know if it uses a diff library – Praveen kumar Sep 04 '18 at 17:27
  • @johnny i had tried it but nothing changes except weird outputs – Praveen kumar Sep 04 '18 at 17:30
  • This answer says `"%lf"` is required - but it shouldn't be. You could also try using `float` instead of `double`. [c code doesnt work on turbo c++ with the float numbers](//stackoverflow.com/a/48270144) – 001 Sep 04 '18 at 17:39
  • @johnny i have initially used %lf but found no luck...actually when i compile and run it several times i got sometimes output correct but sometimes weird.. – Praveen kumar Sep 04 '18 at 17:53
  • How do you compile this with turbo-c. What command line options? What memory model are you using? What version of Turbo-C – Michael Petch Sep 04 '18 at 21:03
  • @MichaelPetch i am using turbo c 3.0 and i have compiled it using the ide supplied by turbo c using alt + f9 shortcut key and the memory model used is small this is all default i am not passing any compile time option on my own one more thing i would like to add is it is using something emulation under floating point which i believe is due to the fact that i am running turboc in a dosbox emulator...can you do me a some favor now – Praveen kumar Sep 05 '18 at 17:29
  • I have Turbo C 3.0 here running in DOSBox and this code works.I have run it many many times (using floating point emulation - which generates code that works in the absence of a hardware floating point processor). Are you sure the code you are showing in this post is **exactly** what you are using? – Michael Petch Sep 05 '18 at 17:31
  • @MichaelPetch yes 500% sure. sometime if i run it 40 to 50 times simultaneously it runs fine but if i come again fter some time and run it it gives me some time nan sometimes -0 i dont understand why.. – Praveen kumar Sep 05 '18 at 17:33
  • Are you running any special TSR or programs in your DOSBox at the same time? – Michael Petch Sep 05 '18 at 17:40
  • @MichaelPetch no i don't. by the way i have added some snap look at those. – Praveen kumar Sep 05 '18 at 17:42
  • @MichaelPetch i just have noted one thing when i run the executable from the dos shell its working fine again i don't know why – Praveen kumar Sep 05 '18 at 17:55

0 Answers0