473,887 Members | 2,275 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

multiplying two numbers and giving the square root

Hello, I have newbie question.
I'm stuck on this current assignment.

Write a program that prompts the user for two integer values,
passes the values to a function where they are multiplied
together and the square root of the product is returned and
displayed for the user. The function should return a double.
Hint: If you multiply an integer by 1.0 the result will be a
double. For example:

Enter an integer: 7
Enter another integer: 6
The square root of 7 times 6 = 6.48074

--
ok, so what I did was created a program that would get the
sqaure root of a number, and then created another one that multipled
2 numbers. Here is the source for the square root:

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
double intOne; // use double for real numbers

cout << "Enter an integer:\n";
cin >> intOne;
double side; // create another variable
side = sqrt( intOne ); // call function, assign return value
cout << "The square root is " << side;
cout << " \n";

return 0;
}

here is the source for multiplying the two integers:

#include <iostream>
using namespace std;
int main()
{
int number1,number2 , product;
cout << "Enter a number: ";
cin >> number1;
cout << "Enter another number: ";
cin >> number2;
product = number1 * number2;
cout << "The product of " << number1 <<" and "<< number2 << " is " << product <<"\n";
return 0;
}

I tried to combine the two together using this code:

#include <iostream>
using namespace std;
int main()
{
int number1,number2 , product;
cout << "Enter a number: ";
cin >> number1;
cout << "Enter another number: ";
cin >> number2;
product = number1 * number2;
cout << "The product of " << number1 <<" and "<< number2 << " is " << product <<"\n";
root = sqrt( product );
cout << "The square root is " << root <<"\n";

return 0;
}

here is the error I'm getting:

combine.cc: In function `int main()':
combine.cc:16: error: `root' undeclared (first use this function)
combine.cc:16: error: (Each undeclared identifier is reported only once for
each function it appears in.)
combine.cc:16: error: call of overloaded `sqrt(int&)' is ambiguous
/usr/include/bits/mathcalls.h:157 : error: candidates are: double sqrt(double)
/usr/include/g++/cmath:550: error: long double std::sqrt(long
double)
/usr/include/g++/cmath:546: error: float std::sqrt(float )

I'm new to this language and any help would be greatly
appreciated. It would be great if someone could point
me in the right direction.
Thanks a bunch,

--Cameron
Jul 22 '05 #1
4 8704
cplusplus <cp*******@linu x.local> wrote in
news:pa******** *************** *****@linux.loc al:
int number1,number2 , product;


should be
int number1,number2 , product, root = 0;
Jul 22 '05 #2
On Wed, 14 Jun 2006 12:57:24 -0700, cplusplus wrote:
Hello, I have newbie question.
[snip]
here is the error I'm getting: combine.cc: In function `int main()':
combine.cc:16: error: `root' undeclared (first use this function)
It's quite simple, really. Compiler error messages are not always clear,
but this one is. If you become a programmer, you will be spending a lot of
time deciphering what the error message means in terms of your code, so
it's best to start right away! The problem here is that you have not
declared your root variable. In your first example program, you have
double intOne; // use double for real numbers


You have no such declaration for root in the combined program.

John's answer was close, but would give you an integral root, which would
not produce the desired answer (it would give 6 as the root of 6 * 7).

--
Greg Schmidt gr***@trawna.co m
Trawna Publications http://www.trawna.com/
Jul 22 '05 #3
you don't include cmath in the third program and you don't declare the
variable root:

#include <cmath> //should go near the beginning of the prog

int root = sqrt( product ); //inserted int before root

cplusplus wrote:
Hello, I have newbie question.
I'm stuck on this current assignment.

Write a program that prompts the user for two integer values,
passes the values to a function where they are multiplied
together and the square root of the product is returned and
displayed for the user. The function should return a double.
Hint: If you multiply an integer by 1.0 the result will be a
double. For example:

Enter an integer: 7
Enter another integer: 6
The square root of 7 times 6 = 6.48074

--
ok, so what I did was created a program that would get the
sqaure root of a number, and then created another one that multipled
2 numbers. Here is the source for the square root:

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
double intOne; // use double for real numbers

cout << "Enter an integer:\n";
cin >> intOne;
double side; // create another variable
side = sqrt( intOne ); // call function, assign return value
cout << "The square root is " << side;
cout << " \n";

return 0;
}

here is the source for multiplying the two integers:

#include <iostream>
using namespace std;
int main()
{
int number1,number2 , product;
cout << "Enter a number: ";
cin >> number1;
cout << "Enter another number: ";
cin >> number2;
product = number1 * number2;
cout << "The product of " << number1 <<" and "<< number2 << " is " << product <<"\n";
return 0;
}

I tried to combine the two together using this code:

#include <iostream>
using namespace std;
int main()
{
int number1,number2 , product;
cout << "Enter a number: ";
cin >> number1;
cout << "Enter another number: ";
cin >> number2;
product = number1 * number2;
cout << "The product of " << number1 <<" and "<< number2 << " is " << product <<"\n";
root = sqrt( product );
cout << "The square root is " << root <<"\n";

return 0;
}

here is the error I'm getting:

combine.cc: In function `int main()':
combine.cc:16: error: `root' undeclared (first use this function)
combine.cc:16: error: (Each undeclared identifier is reported only once for
each function it appears in.)
combine.cc:16: error: call of overloaded `sqrt(int&)' is ambiguous
/usr/include/bits/mathcalls.h:157 : error: candidates are: double sqrt(double)
/usr/include/g++/cmath:550: error: long double std::sqrt(long
double)
/usr/include/g++/cmath:546: error: float std::sqrt(float )

I'm new to this language and any help would be greatly
appreciated. It would be great if someone could point
me in the right direction.
Thanks a bunch,

--Cameron

Jul 23 '05 #4
In message <C7************ ********@adelph ia.com>, Squid Seven
<te*********@sq uidseven.com> top-posted

[please don't do that]
you don't include cmath in the third program and you don't declare the
variable root:

#include <cmath> //should go near the beginning of the prog

int root = sqrt( product ); //inserted int before root
Since when did std::sqrt return an int?
cplusplus wrote:
Hello, I have newbie question.
I'm stuck on this current assignment. Write a program that prompts
the user for two integer values,
passes the values to a function where they are multiplied
together and the square root of the product is returned and
displayed for the user. The function should return a double. ^^^^^^ Hint: If you multiply an integer by 1.0 the result will be a
double.
That's a really bad hint. There are better ways to convert integer to
double.
For example: Enter an integer: 7
Enter another integer: 6
The square root of 7 times 6 = 6.48074
--
ok, so what I did was created a program that would get the sqaure
root of a number, and then created another one that multipled
2 numbers. Here is the source for the square root:
#include <iostream>
#include <cmath> using namespace std;
int main() {
double intOne; // use double for real numbers
cout << "Enter an integer:\n";
cin >> intOne;
double side; // create another variable
side = sqrt( intOne ); // call function, assign return value
cout << "The square root is " << side;
cout << " \n";
return 0;
}
here is the source for multiplying the two integers:
#include <iostream> using namespace std;
int main() {
int number1,number2 , product; cout << "Enter a number: ";
cin >> number1; cout << "Enter another number: ";
cin >> number2;
product = number1 * number2;
cout << "The product of " << number1 <<" and "<< number2 << " is
" << product <<"\n";
return 0;
}
I tried to combine the two together using this code:
#include <iostream> using namespace std;
int main() {
int number1,number2 , product; cout << "Enter a number: ";
cin >> number1; cout << "Enter another number: ";
cin >> number2;
product = number1 * number2;
cout << "The product of " << number1 <<" and "<< number2 << " is
" << product <<"\n";
root = sqrt( product );
double root = sqrt(double(pro duct));
cout << "The square root is " << root <<"\n";
return 0;
}
here is the error I'm getting:
combine.cc: In function `int main()':
combine.cc:16: error: `root' undeclared (first use this function)
combine.cc:16: error: (Each undeclared identifier is reported only once for
each function it appears in.)
combine.cc:16: error: call of overloaded `sqrt(int&)' is ambiguous
/usr/include/bits/mathcalls.h:157 : error: candidates are: double sqrt(double)
/usr/include/g++/cmath:550: error: long double std::sqrt(long
double)
/usr/include/g++/cmath:546: error: float std::sqrt(float )
I'm new to this language and any help would be greatly
appreciated. It would be great if someone could point
me in the right direction.
Thanks a bunch,
--Cameron


--
Richard Herring
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
746
by: cplusplus | last post by:
Hello, I have newbie question. I'm stuck on this current assignment. Write a program that prompts the user for two integer values, passes the values to a function where they are multiplied together and the square root of the product is returned and displayed for the user. The function should return a double. Hint: If you multiply an integer by 1.0 the result will be a double. For example:
24
5923
by: Stavros Christoforou | last post by:
Hello everyone, I was wondering if someone could help me with an issue I have in C++. I want to select random points within the volume of a sphere. I know how to get random numbers using srand() and rand(), but have no idea how to do that within a more complicated geometry. Any help would be greatly appreciated.. Regards Stavros
7
7410
by: brian.digipimp | last post by:
Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is 2. An odd integer is prime if it is not divisible by an odd integer less than or equal to the square root of the number.) I was given this assignment in my c++ class and I'm having trouble with it. I've looked at the professors lab solutions but I just...
2
2538
by: k | last post by:
I have aproblem when multiplying 2 float 638.9 * 382.8 should = 244570.92 results giving 244570.922 both numbers are float variables , tried using double to much the same effect ..????? any assistance much appreciated
17
1488
by: Mike Meyer | last post by:
In the discussion of equality, the issue that decimal('3.0') == 3.0 is False came up as a reason for changing the behavior of ==. The problem with this is that the proposed change doesn't really fix anything, it just gives different wrong behavior. The correct fix would seem to be fixing python's handling of numbers. It's not clear from the PEP why test for equality always fails. While the PEP says that interactions with floats are...
32
5019
by: priyam.trivedi | last post by:
Hi! Could anyone tell me how to find the square root of a number without using the sqrt function. I did it by using Newton's Formula. How can it be done by using the Binomial Theorem/Taylor Series? Is there any other way of doing it rather than using series? Thank you, Priyam
12
2772
by: vj | last post by:
Hi! I have a piece of code (shown below) involving complex numbers. The code is not running and giving error ("Invalid floating point operation" and "SQRT:Domain error"). I would be very thankful if someone can tell me where is the problem. I am aware that my code is far from being efficient and organized, and also there are many extra #include statements not really required for the code. I am a novice programmer, as you can see ! At...
25
3908
by: johnmsimon | last post by:
i need to develop a code that finds a prime right number between 2 and 100000. and print one line of text that indicates if the int. is right prime. i am in beginning programing so complex is complicated. we are on a functions chapter. i am usung the square root to find the prime. so far i have accomplished this but i cant fgure how to divide this by ten so i can find the right prime. i am using c. #include <stdio.h>
2
4217
by: shihaoran | last post by:
I really need help with one my program; it is about arraylist; I do not get it. Can someone please help me with it? Here's the instruction: 1. Your instructor will provide you with a text file, (numbers.txt), containing a large (N <= 1000) number of integers. The integers range in value from 0 to 100. The text file has been created with one value on each line. Due to the potential for the sum of the numbers to be very large, you...
4
8102
by: krishnai888 | last post by:
I had already asked this question long back but no one has replied to me..I hope someone replies to me because its very important for me as I am doing my internship. I am currently writing a code involving lot of matrices. At one point I need to calculate the square root of a matrix e.g. A which contains non-zero off-diagonal elements. I searched for a lot of info on net but no algorithm worked. My best bet for finding square root was to find...
0
9957
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9799
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11173
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10770
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10875
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9593
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6011
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4632
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4238
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.