what's wrong with the code below?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ravikiran7
    New Member
    • Mar 2013
    • 6

    #1

    what's wrong with the code below?

    the result i am getting is not correct.And i do not know where the mistake is.. please help me.
    Code:
              #include <stdio.h>
    		#include <ctype.h>
    		int fun(int *v,int *r);
    		main()
    		{
    		static int v[1][3]={{740,10,0}};
    		static int r[1][3]={{20000,10000,0}};
    		int result;
    		result=fun(&v[0][0],&r[0][1]);
    		printf("the result is %d",result);
    		return 0;
    		}
    		int fun(int *v,int *r)
    		{
    		int temp;
    		temp=*v * *r;
    		return (temp);
    		}
    the output of above is :-
    the result is -5568.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I'm using Visual Studio.NET 2008 and I get 7400000 from the above code which looks to be correct.

    What compiler are you using?

    Comment

    • ravikiran7
      New Member
      • Mar 2013
      • 6

      #3
      Thanks for such a quick reply.I am using Turbo c++

      Comment

      • divideby0
        New Member
        • May 2012
        • 131

        #4
        You're probably dealing with overflow; try using long instead of int; or if you're only dealing with positive values, maybe unsigned long. limits.h will allow you to see what sizes turbo uses; for example

        Code:
        #include <stdio.h>
        #include <limits.h>
        
        int main(void)
        {
           printf("int max = %d\nunsigned int = %u\n",
               INT_MAX, UINT_MAX);
           printf("long max = %ld\nunsigned long max = %lu", 
               LONG_MAX, ULONG_MAX);
           return 0;
        }

        Comment

        • ravikiran7
          New Member
          • Mar 2013
          • 6

          #5
          i changed the datatype to unsigned long int.but could not resolve it.I am putting the code here.help me to resolve it.
          Code:
          	#include <stdio.h>
          		#include <ctype.h>
                
          		unsigned long int fun(unsigned long int *v,unsigned long int *r);
          		main()
          		{
          		static unsigned  long int v[1][3]={{740,10,0}};
          		static unsigned long int r[1][3]={{20000,10000,0}};
          		 unsigned long int result;
          		result=fun(&v[0][0],&r[0][1]);
          		printf("the result is %d",result);
          		return 0;
          		}
          		unsigned long int fun(unsigned long int *v,unsigned long int *r)
          		{
          		unsigned long int temp;
          		temp=*v * *r;
          		return (temp);
          		}
          still getting the same output.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            So what is the sizeof(int) on your Turbo C++?

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              You have one more int to change to an unsigned long int. On line 11, change "%d" to "%uld".

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                My mistake, make that "%lu"

                Comment

                • divideby0
                  New Member
                  • May 2012
                  • 131

                  #9
                  @wfc, I think it's 2. On my machine, the same output can be replicated in VCE 2008 using shorts.

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    If it is 2 then overflow occurs at 32768. Way too small to hold 7400000 which would explain the odd output.

                    Comment

                    • ravikiran7
                      New Member
                      • Mar 2013
                      • 6

                      #11
                      thanks for such awesome support.I compiled and runned the same code on linux.it worked perfectly.
                      the problem is overflow.
                      my special thanks to everyone.

                      Comment

                      Working...