435,414 Members | 2,939 Online
Need help? Post your question and get tips & solutions from a community of 435,414 IT Pros & Developers. It's quick & easy.

# Why is the code coming out wrong

 P: 11 #include 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
11 Replies

 Expert Mod 5K+ P: 9,197 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 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. Oct 3 '18 #2

 Expert 100+ P: 2,400 What do expect these lines to do? In fact, they won’t do anything. Expand|Select|Wrap|Line Numbers (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. 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 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

 Expert Mod 5K+ P: 9,197 You don't have a Square function. You have this: Expand|Select|Wrap|Line Numbers 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. Oct 7 '18 #5

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

 Expert Mod 5K+ P: 9,197 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 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,400 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 #include  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.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. Line 1. You will need a function prototype for the Square function. 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?) 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. Line 15. Multiplying two ints has an int result. Overflow will occur if the input value is larger than sqrt(INT_MAX). 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 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

 Expert Mod 5K+ P: 9,197 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 #define _CRT_SECURE_NO_WARNINGS   #include  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 #12