423,851 Members | 1,356 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,851 IT Pros & Developers. It's quick & easy.

Why is the code coming out wrong

P: 11
#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
Oct 3 '18 #1
Share this Question
Share on Google+
11 Replies


weaknessforcats
Expert Mod 5K+
P: 9,196
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:


Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. int a;
  4.  
  5.      ....some processing...
  6. a = Square(a);
  7.  
  8. }
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.
Oct 3 '18 #2

Expert 100+
P: 2,385
What do expect these lines to do? In fact, they won’t do anything.
Expand|Select|Wrap|Line Numbers
  1. (a > 0);
  2. ...
  3. (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.
Oct 4 '18 #3

P: 11
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;
}
Oct 6 '18 #4

weaknessforcats
Expert Mod 5K+
P: 9,196
You don't have a Square function. You have this:

Expand|Select|Wrap|Line Numbers
  1. int Square(int a);
  2.  
  3.  
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.
Oct 7 '18 #5

P: 11
it still is not squaring the number
Oct 7 '18 #6

weaknessforcats
Expert Mod 5K+
P: 9,196
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?
Oct 7 '18 #7

P: 11
#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
Oct 8 '18 #8

Expert 100+
P: 2,385
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.
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. int main ()
  3. {
  4. /* variable definition: */ 
  5. int a;
  6. printf ("Enter a positive Integer\n: ");
  7. scanf("%d", &a);
  8. {
  9. printf ("This code will calculate square \n: ");
  10. scanf(a*a);
  11.  
  12. // Call the Square Function
  13. printf("Square of %d \n",a);
  14. return a * a;
  15. return 0;
  16. }
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?
Oct 8 '18 #9

P: 11
Could you show me the code how it should be please.
Oct 8 '18 #10

P: 11
#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
Oct 8 '18 #11

weaknessforcats
Expert Mod 5K+
P: 9,196
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:

Expand|Select|Wrap|Line Numbers
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <stdio.h>
  4. int Square(int value);
  5. int main()
  6. {
  7.     /* variable definition: */
  8.     int intValue, Results;
  9.     intValue = 1;
  10.     // While a positive number
  11.     while (intValue > 0)
  12.     {
  13.         printf("Enter a positive Integer\n: ");
  14.         scanf("%d", &intValue);
  15.         if (intValue > 0)
  16.         {
  17.             // Call the Square Function
  18.             Results = Square(intValue);
  19.             printf("Square of %d is %d\n", intValue, Results);
  20.         }
  21.         return 0;
  22.     }
  23. }
  24.     /* function returning the Square of a number */
  25.     int Square(int value)
  26.     {
  27.         return value*value;
  28.     }
  29. ///    This is the new code im trying and im still getting errors
  30. //        prog.c: In function ‘main’ :
  31. //    prog.c : 25 : 1 : error : expected declaration or statement at end of input
  32.  
  33. //^
  34. //At top level :
  35. //prog.c : 22 : 5 : warning : ‘Square’ defined but not used[-Wunused - function]
  36. //int Square(int value)
  37. //and no output
Oct 8 '18 #12

Post your reply

Sign in to post your reply or Sign up for a free account.