C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-27-2009, 12:13 PM   #1
Registered User
 
Join Date: Jan 2009
Posts: 4
Squaring numbers in c++

hey people!
I am trying to make a calculator in C++.
I am stuck on how to square a number.
Would anybody know how to do this?
Sanka792 is offline   Reply With Quote
Old 01-27-2009, 12:17 PM   #2
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,372
You er... multiply the number by itself.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 01-27-2009, 12:20 PM   #3
Registered User
 
Join Date: Jan 2009
Posts: 4
Quote:
Originally Posted by laserlight View Post
You er... multiply the number by itself.
oke and how do you square root a number?
Sanka792 is offline   Reply With Quote
Old 01-27-2009, 12:23 PM   #4
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,372
Quote:
Originally Posted by Sanka792
oke and how do you square root a number?
That depends on how you want to do it, but one way is to use the std::sqrt function from <cmath>. That said, it would be wiser to start by implementing say, addition, and only implement other functionality after you have a working prototype.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way

Last edited by laserlight; 01-27-2009 at 12:31 PM. Reason: oafter -> after
laserlight is online now   Reply With Quote
Old 01-27-2009, 12:29 PM   #5
Algorithm Dissector
 
iMalc's Avatar
 
Join Date: Dec 2005
Location: New Zealand
Posts: 2,744
Google is your friend
__________________
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
iMalc is offline   Reply With Quote
Old 01-27-2009, 12:31 PM   #6
Registered User
 
Join Date: Jan 2009
Posts: 4
Quote:
Originally Posted by laserlight View Post
You er... multiply the number by itself.
Quote:
Originally Posted by laserlight View Post
That depends on how you want to do it, but one way is to use the std::sqrt function from <cmath>. That said, it would be wiser to start by implementing say, addition, and only implement other functionality oafter you have a working prototype.
coudl you give an example of using this function. For example how to find the square root of 16?
Sanka792 is offline   Reply With Quote
Old 01-27-2009, 12:33 PM   #7
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,372
Quote:
Originally Posted by Sanka792
coudl you give an example of using this function. For example how to find the square root of 16?
You could read say, cppreference.com's entry on sqrt, and/or you could search the Web as iMalc suggested.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 01-27-2009, 12:34 PM   #8
The larch
 
Join Date: May 2006
Posts: 3,222
Um, it takes a value (16) and returns the square root of the value (4.0). There's nothing more to it. Do you have an idea how to call any function?
__________________
I might be wrong.

Quote:
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Quoted more than 1000 times (I hope).
anon is offline   Reply With Quote
Old 01-27-2009, 12:34 PM   #9
Registered User
 
Join Date: Jun 2006
Posts: 83
Code:
#include <iostream>
#include <cmath>

int main(){
  int n = 16; // number to square root
  std::cout << std::sqrt(n) << std::endl;
}
This was not compiled and just getting the answer doesn't do you any good.
Kudose is offline   Reply With Quote
Old 01-27-2009, 12:35 PM   #10
Student
 
legit's Avatar
 
Join Date: Aug 2008
Location: UK -> Newcastle
Posts: 156
Quote:
Originally Posted by Sanka792 View Post
coudl you give an example of using this function. For example how to find the square root of 16?
Well from where i'm sitting, it sounds like a big function to me. I havn't made a calc persae, but i'll probably make one now just for the fun of it. The way I would do it would be trial and error. Think about the behaviour of numbers. An odd number multiplied by an odd number is obviously going to be an odd number. Start from there, create a method of finding out if the input number was even or odd, then take it from there.
legit is offline   Reply With Quote
Old 01-27-2009, 12:40 PM   #11
Registered User
 
Join Date: Jan 2009
Posts: 4
Thnks guys so much for the help.
I now know what to do.
I already have a prototype that can add, subtract, multiply and divide.
But i was thinking of adding in formulas like the -b formula.

But i goit another question.
Is it possible to create an interface for c++ so that it looks like a program?
I mean a console that a user can use that covers up command prompt?
Sanka792 is offline   Reply With Quote
Old 01-27-2009, 12:43 PM   #12
Student
 
legit's Avatar
 
Join Date: Aug 2008
Location: UK -> Newcastle
Posts: 156
Quote:
Originally Posted by Sanka792 View Post
Thnks guys so much for the help.
I now know what to do.
I already have a prototype that can add, subtract, multiply and divide.
But i was thinking of adding in formulas like the -b formula.

But i goit another question.
Is it possible to create an interface for c++ so that it looks like a program?
I mean a console that a user can use that covers up command prompt?
You mean like a Window? This is called a GUI(Graphical User Interface). Yes this is possible, but hard work. I myself cannot get the hang of it, although poeple on the boards will gladly help you.
legit is offline   Reply With Quote
Old 01-27-2009, 12:46 PM   #13
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,372
Quote:
Originally Posted by Sanka792
Is it possible to create an interface for c++ so that it looks like a program?
I mean a console that a user can use that covers up command prompt?
As in a graphical user interface that say, resembles a hand held calculator? Yes, but since you appear relatively new to C++... not yet.

At the moment I would suggest a "classical" problem: allow the user to enter an arithmetic expression, from the console window, in reverse Polish notation (search the Web if you must). When you are satisfied with what you have done, allow the user to enter an arithmetic expression in the "normal" infix notation. You could always reuse this later on when you do write a GUI calculator.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 01-27-2009, 02:03 PM   #14
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,698
Just a note: if you wanted to raise a number to an arbitrary power, you can use the pow() function, also in <cmath>.
__________________
dwk

Seek and ye shall find. quaere et invenies.

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell


Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net

My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
dwks is offline   Reply With Quote
Reply

Tags
c++, calculator

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about random numbers Kempelen C Programming 2 07-02-2008 06:28 AM
Help with Rational Numbers (C++) cloudjc C++ Programming 3 04-28-2008 04:03 PM
Logical errors with seach function Taka C Programming 4 09-18-2006 05:20 AM
the definition of a mathematical "average" or "mean" DavidP A Brief History of Cprogramming.com 7 12-03-2002 11:15 AM
A (complex) question on numbers Unregistered C++ Programming 8 02-03-2002 06:38 PM


All times are GMT -6. The time now is 10:09 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22