473,382 Members | 1,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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
Oct 3 '18 #1
11 1821
weaknessforcats
9,208 Expert Mod 8TB
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
donbock
2,426 Expert 2GB
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
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
9,208 Expert Mod 8TB
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
it still is not squaring the number
Oct 7 '18 #6
weaknessforcats
9,208 Expert Mod 8TB
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
#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
donbock
2,426 Expert 2GB
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
Could you show me the code how it should be please.
Oct 8 '18 #10
#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
9,208 Expert Mod 8TB
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

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

Similar topics

5
by: plugimi | last post by:
hi i've got kind of a strange problem i'm struggeling with here. this page: http://www.pohflepp.de/eavesdripping4.html won't be recognized as html/xml by firefox. i've tried everything that...
1
by: DS | last post by:
What in this code is stopping me from adding new records? Private Sub ListProd_Click() On Error GoTo GotError DoCmd.GoToControl "" Me!.Form.AllowAdditions = True DoCmd.GoToRecord , , acNewRec...
25
by: Alvin Bruney | last post by:
C# is great but it does have some short comings. Here, I examine one of them which I definitely think is a shortcoming. Coming from C++, there seems to be no equivalent in C# to separate code...
0
by: Miquel | last post by:
Hi all. I felt frustrated when developing an 'UserControl' derivated from textBox, because sequence event (and Validate event) seems to fail. I Always thought my code was wrong. But after...
0
by: GJH | last post by:
Hi, I have created a page that downloads a vCalendar to a visitor. If the person logs on from somewhere other than EST their times are incorrect. for example, someone in Colorado accesses the page...
5
by: Otto Wyss | last post by:
I've a simple if statement yet it executes even if condition is false. What can be wrong? var j = -1; for (var i in files) { var f = files.split('/', 1); alert (j+','+i+','+(j == -1) ||...
2
by: xianwei | last post by:
First, typedef struct pair { Node *parent; Node *child; } Pair; static Pair SeekItem(cosnt Item *pI, const Tree *pTree) { Pair look;
2
by: cosmos22 | last post by:
Hi. I'm designing something that uploads files to an FTP server of the users choice, so they quickly upload things and access them as a way of backing up files, almost. I would like to upload...
9
by: tonyadams5 | last post by:
I have the following script but the page does not load. It always loads the error page. <?php if ($c !="") { include ("content".$c.".php"); } else { ...
9
by: pejy69 | last post by:
Hi My name is PJ and i'm an adult student who requires a pointer in the right direction. Below is a snippett of the code that i believe i have something wrong in but cant work out what i have done...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.