Thread: Peculiar Problem with Printf

  1. #1
    Unregistered
    Guest

    Peculiar Problem with Printf

    I am trying to find numbers between 1 and 100000000 which are both perfect squares and perfect cubes.

    I have written the code successfully and the results are good. The problem is coming with printf.
    When I print the results seperately, they're right.. when I print them in the same line, it is showing 0....

    please check the code below:

    #include <stdio.h>


    void sqroot(long long int number, long long int *prevsqroot, long long int *sqexists)
    {
    long long int i;
    *sqexists = 0;

    for(i= *prevsqroot; i<=number; i++)
    {
    long long int var = i*i;

    if (var == number)
    {
    *prevsqroot = i;
    *sqexists = 1;
    break;
    }

    if (var > number)
    {
    break;
    }

    }
    }

    void curoot(long long int number, long long int prevsqroot, long long int *prevcuroot, long long int *cuexists)
    {
    long long int i;
    *cuexists = 0;


    for(i=*prevcuroot; i<=prevsqroot; i++)
    {
    long long int var = i*i*i;

    if (var == number)
    {
    *prevcuroot = i;
    *cuexists = 1;
    break;
    }

    if (var > number)
    {
    break;
    }

    }
    }


    main()
    {
    long long int i;
    long long int prevsqroot = 1;
    long long int prevcuroot = 1;
    long long int sqexists=0, cuexists=0;

    for (i=1; i<=1000000000; i++)
    {
    sqroot(i, &prevsqroot, &sqexists);

    if (sqexists ==1)
    {
    curoot(i, prevsqroot, &prevcuroot, &cuexists);

    if (cuexists ==1)
    {
    printf("Squareroot is %6d\n", prevsqroot);
    printf("Cuberoot is %6d\n", prevcuroot);

    printf("Number is %9d Sqroot is %6d Curoot is %6d\n", i, prevsqroot, prevcuroot);
    }
    }

    }


    printf("Successful!\n");

    getchar();
    }



    notice that there are three printfs under the if above... the first
    two printfs are working fine... but when i do the same thing in the thrid printf, it is not working..



    another problem is that when i run this code in TC++, I get one extra problem, it is showing i as a negative number ... however, the squareroots and cuberoots calculated are right ...


    Can anyone tell me what's gone wrong with this ?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    another problem is that when i run this code in TC++, I get one extra problem, it is showing i as a negative number ... however, the squareroots and cuberoots calculated are right ..
    Um... TurboC++ does not support 'long long', IIRC. TurboC++ has 16bit integers. I'm surprised your code even compiles. In the same vein, I doubt that implementation of printf knows what to do with them either.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Unregistered
    Guest

    Turbo C++ DOES support long long int

    TURBO C++ DOES support long long int (64 bit)
    I wrote many programs before using that.
    There's also long long float, FYI

    If it doesn't, it wouldn't have printed the values
    in the first two printfs.

  4. #4
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    Code:
    printf("Squareroot is %ld\n", prevsqroot);
    printf("Cuberoot is %ld\n", prevcuroot);
    %d is for integers, %ld is for long. Try the above, may help.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: Turbo C++ DOES support long long int

    Originally posted by Unregistered
    TURBO C++ DOES support long long int (64 bit)
    I wrote many programs before using that.
    There's also long long float, FYI

    If it doesn't, it wouldn't have printed the values
    in the first two printfs.
    Really? What version of TC++ are you running? IIRC, the last version that was called "Turbo C++" was version 3.1, which was a Windows version, and did not support long long.

    Assuming we're talking about the same product here. Turbo C++ by Borland? What version?

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Unregistered
    Guest
    What version ?
    Borland Turbo C++ 3.0 (DOS)


    It also works in Dev C++

    I think you should include a library in the later
    versions of Borland C++ (Turbo or whatever)
    to use long long float ... instead, they
    say something like __long64 , i am not sure..



    And about the other post....

    your %ld looks correct, but if you put them in the printf below,
    it still doesn't work...

    printf("Number is %9ld Sqroot is %6ld Curoot is %6ld\n", i, prevsqroot, prevcuroot);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Weird printf() problem
    By stanlvw in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2008, 02:29 AM
  3. Newb Help: Full Arrays and Functions
    By LycanGalen in forum C Programming
    Replies: 5
    Last Post: 01-31-2008, 08:35 PM
  4. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM
  5. Whats Wrong Whith This!?
    By SmokingMonkey in forum C++ Programming
    Replies: 8
    Last Post: 06-01-2003, 09:42 PM