Why is the code coming out wrong

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mpalmer1995
    New Member
    • Oct 2018
    • 11

    Why is the code coming out wrong

    #include <stdio.h>
    int Square(int a);
    int main ()
    {
    /* variable definition: */
    int a;
    {
    printf ("Enter a positive Integer\n: ");
    scanf("%d", &a);
    (a > 0);
    {
    printf ("This code will calculate square \n: ");

    // Call the Square Function
    (a * a);
    printf("Square of %d is %d\n",a);
    return a * a;
    }
    }
    return 0;
    }

    Enter a positive Integer
    : This code will calculate square
    : Square of 12 is -181033120
    please help me refine this so i can get the correct answer
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I agree that (a * a) is the square of a. However, you need to store this result into some variable. As it is, int a is never changed.

    Then there's the Square function, which does not exist. I see the function prototype but not the code for the function. A call might look like:


    Code:
    int main()
    {
    int a;
    
         ....some processing...
    a = Square(a);
    
    }
    but I don't see a call.

    Also, the is an (a<0) which doesn't get used. It looks like the "if" part of the code is missing.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      What do expect these lines to do? In fact, they won’t do anything.
      Code:
      (a > 0);
      ...
      (a * a);
      Your call to printf has two “%d” specifiers but you only pass one value to print. That’s why the second printed value is so crazy.

      Comment

      • mpalmer1995
        New Member
        • Oct 2018
        • 11

        #4
        I'm getting run time error now when I run the code I have changed it to reflect this code but I need it to stay a function
        #include <stdio.h>
        int Square(int a);
        int main ()
        {
        /* variable definition: */
        int a;
        {
        printf ("Enter a positive Integer\n: ");
        scanf("%d", &a);
        {
        printf ("This code will calculate square \n: ");

        // Call the Square Function
        printf("Square of %d \n",a);
        return a * a;
        }
        }
        return 0;
        }

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          You don't have a Square function. You have this:

          Code:
          int Square(int a);
          but you never use it.

          What you have is a calculation that returns a * a. But you are still in the main() function so the a * a is returned to the operating system as the exit code of your program. The operating system s gagging on the a * a as invalid for the operating system.


          Please look at the code in my post #2. Your main() should have a call like that.


          Please post again if this doesn't help and I will write the skeleton code for you to get you started.

          Comment

          • mpalmer1995
            New Member
            • Oct 2018
            • 11

            #6
            it still is not squaring the number

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Then code you posted doesn't display the square. Nowhere do you display a* a and nowhere have you moved a* a to another variable and displayed that other variable.

              Or do you mean you have looked at a * a in the debugger and seen that it is incorrect?

              Comment

              • mpalmer1995
                New Member
                • Oct 2018
                • 11

                #8
                #include <stdio.h>
                int main ()
                {
                /* variable definition: */
                int a;
                {
                printf ("Enter a positive Integer\n: ");
                scanf("%d", &a);
                {
                printf ("This code will calculate square \n: ");
                scanf(a*a);

                // Call the Square Function
                printf("Square of %d \n",a);
                return a * a;
                }
                }
                return 0;
                }

                Runtime error #stdin #stdout 0s 9424KB
                comments (0)
                stdin
                copy
                10
                10
                stdout
                copy
                Standard output is empty
                This is the code I'm using now, still getting error

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  The following code snippet is copied from the preceding post. I did this in order to use CODE tags so there are line numbers. You should always use CODE tags so that the responses can be precise.
                  Code:
                  #include <stdio.h>
                  int main ()
                  {
                  /* variable definition: */ 
                  int a;
                  { 
                  printf ("Enter a positive Integer\n: ");
                  scanf("%d", &a);
                  {
                  printf ("This code will calculate square \n: ");
                  scanf(a*a);
                  
                  // Call the Square Function
                  printf("Square of %d \n",a);
                  return a * a;
                  } 
                  } 
                  return 0;
                  }
                  Some of these issues have already been pointed out.
                  1. Lines 9-16. I think you want this to be the Square function but you put it in the middle of the main function. You need to define Square separately from main.
                  2. Line 1. You will need a function prototype for the Square function.
                  3. Lines 7-8. You tell the user to provide a positive Integer. It behooves you to validate the entered value and print an error message if it is out of range. (On the other hand, is there really any reason to disallow zero or negative inputs?)
                  4. Line 11. The preceding printed text says the code calculates the Square, but you call scanf - a function that gets user input. That won’t calculate anything. Not only that, but you violate the scanf function prototype, causing a compiler error and/or a runtime error.
                  5. Line 15. Multiplying two ints has an int result. Overflow will occur if the input value is larger than sqrt(INT_MAX).
                  6. Lines 6 and 17. What do want these braces to accomplish?

                  Comment

                  • mpalmer1995
                    New Member
                    • Oct 2018
                    • 11

                    #10
                    Could you show me the code how it should be please.

                    Comment

                    • mpalmer1995
                      New Member
                      • Oct 2018
                      • 11

                      #11
                      #include <stdio.h>
                      int Square(int value);
                      int main ()
                      {
                      /* variable definition: */
                      int intValue,Result s;
                      intValue = 1;
                      // While a positive number
                      while (intValue > 0)
                      {
                      printf ("Enter a positive Integer\n: ");
                      scanf("%d", &intValue);
                      if (intValue > 0)
                      {
                      // Call the Square Function
                      Results = Square(intValue );
                      printf("Square of %d is %d\n",intValue, Results);
                      }
                      return 0;
                      }
                      /* function returning the Square of a number */
                      int Square(int value)
                      {
                      return value*value;
                      }
                      This is the new code im trying and im still getting errors
                      prog.c: In function ‘main’:
                      prog.c:25:1: error: expected declaration or statement at end of input
                      }
                      ^
                      At top level:
                      prog.c:22:5: warning: ‘Square’ defined but not used [-Wunused-function]
                      int Square(int value)
                      and no output

                      Comment

                      • weaknessforcats
                        Recognized Expert Expert
                        • Mar 2007
                        • 9214

                        #12
                        Your code compiles BUT I had to move the Square function to be outside of main().


                        The closing brace of main() was after the closing brace of Square(). I just moved it before the Square() function.


                        This is what I compiled:

                        Code:
                        #define _CRT_SECURE_NO_WARNINGS
                        
                        #include <stdio.h>
                        int Square(int value);
                        int main()
                        {
                        	/* variable definition: */
                        	int intValue, Results;
                        	intValue = 1;
                        	// While a positive number
                        	while (intValue > 0)
                        	{
                        		printf("Enter a positive Integer\n: ");
                        		scanf("%d", &intValue);
                        		if (intValue > 0)
                        		{
                        			// Call the Square Function
                        			Results = Square(intValue);
                        			printf("Square of %d is %d\n", intValue, Results);
                        		}
                        		return 0;
                        	}
                        }
                        	/* function returning the Square of a number */
                        	int Square(int value)
                        	{
                        		return value*value;
                        	}
                        ///	This is the new code im trying and im still getting errors
                        //		prog.c: In function ‘main’ :
                        //	prog.c : 25 : 1 : error : expected declaration or statement at end of input
                        
                        //^
                        //At top level :
                        //prog.c : 22 : 5 : warning : ‘Square’ defined but not used[-Wunused - function]
                        //int Square(int value)
                        //and no output

                        Comment

                        Working...