Why does the compiler say pow(n,k) ambiguous in this code? - c++

When compiling the following code under Xcode 4.5 (Mac), compilation fails with the following error message:
call to 'pow' is ambiguous
Why? Thanks!
#include <iostream>
#include <cmath>
#include <limits>
using namespace std;
int main()
{
cout << "\tSquare root calculator using an emulation the ENIAC's algorithm." << endl
<< endl;
long long m;
while ((cout << "Enter a positive integer:" << endl)
&& (!(cin >> m) || m < 0 || m > 9999999999)) //10-digit maxium
{
cout << "Out of range.";
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
again:
long long l = m;
//Find out how big the integer is and adjust k accordingly.
int order = -1;
long long temp = m;
do
{
temp /= 10;
order++;
} while (temp/10);
int k = order/2;
//Step 1
long long a = -1;
do
{
a+=2;
m -= a*pow(100,k);
} while (m >= 0);
while (k > 0)
{
k--;
//Step 2
if (m < 0)
{
a = 10*a+9;
for (;m < 0;a -= 2)
{
m += a*pow(100,k);
}
a += 2;
}
//Step 3
else
{
a = 10*a-9;
for(;m >= 0;a += 2)
{
m -= a*pow(100,k);
}
a -= 2;
}
}
//Step 4
cout << endl << "The square root of " << l << " is greater than or equal to "
<< (a-1)/2 << " and less than " << (a+1)/2 << "." << endl << endl;
while ((cout << "Enter a positive integer to calculate again, or zero to exit." << endl)
&& (!(cin >> m) || m < 0 || m > 9999999999))
{
cout << "Out of range.";
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
if (m > 0) goto again;
return 0;
}

As you can see here there is no pow(int,int);
pow
<cmath>
double pow ( double base, double exponent );
long double pow ( long double base, long double exponent );
float pow ( float base, float exponent );
double pow ( double base, int exponent );
long double pow ( long double base, int exponent );

This link looks like it answers your question:
http://bytes.com/topic/c/answers/727736-ambiguous-call-pow
It looks like the pow() function doesn't like integers.

Related

C++ if statement not printing desired output

Problem is with the if statment inside the while loop. It is not printing the desired output. The else if statement and the else statement seem to work fine
Any help is appreciated
#include <iostream>
using namespace std;
/*
Write a C++ program that asks the user for an integer.
The program finds and displays the first power of 3
larger than the input number using while
*/
int main() {
int input = 0;
int base = 3;
int exponent = 0;
int sum = 1;
cout << "Enter a number: ";
cin >> input;
while (sum < input) {
// This is the if statement giving me problems
if (input == 1) {
exponent += 1;
sum = 3;
}
// This else if statement seems to work fine
else if (input == 3) {
exponent += 2;
sum = 9;
}
else {
exponent++;
sum *= base;
}
}
// Print output
cout << "3 to the power of " << exponent << " is equal to " << sum;
cout << endl << "It is the first power of 3 larger than " << input;
return 0;
}
Your logic is wrong (and I have to say a bit bizarre).
If the input is 1 then while (sum < input) is not true and so you never reach your if (input == 1) statement.
REALIZED my mistake. i just moved the if and else if statement to outside the loop
#include <iostream>
using namespace std;
/*
Write a C++ program that asks the user for an integer.
The program finds and displays the first power of 3
larger than the input number using while
*/
int main() {
int input = 0;
int base = 3;
int exponent = 0;
int sum = 1;
cout << "Enter a number: ";
cin >> input;
if (input == 1) {
exponent += 1;
sum = 3;
}
else if (input == 3) {
exponent += 2;
sum = 9;
}
while (sum < input) {
exponent++;
sum *= base;
}
cout << "3 to the power of " << exponent << " is equal to " << sum;
cout << endl << "It is the first power of 3 larger than " << input;
return 0;
}
If I understood the objective right from the comments, if conditions are not required. Just replace the condition and simplify the while loop as follows:
while (sum <= input) {
exponent++;
sum *= base;
}
Write a C++ program that asks the user for an integer. The program
finds and displays the first power of 3 larger than the input number
using while
You should probably calculate the answer instead of looping.
#include <iostream>
#include <cmath>
int main() {
int input;
std::cout << "input: ";
std::cin >> input;
int x = 0;
/*
3^x == input
ln(3^x) == ln(input)
x*ln(3) == ln(input)
x == ln(input)/ln(3)
*/
// calculate x = ln(input)/ln(3), round down and add 1
if(input > 0) x = std::floor(std::log(input) / std::log(3.)) + 1.;
std::cout << "answer: 3^" << x << " == " << std::pow(3, x) << "\n";
}

Using floating numbers for functions

I ran this codes only my zybook class, it gives me the wrong answers and tells me to use floating numbers for the functions. How can I use floating point numbers for square function.
edit:
sorry for the confusion
#include <iostream>
#include <string>
using namespace std;
float squareRoot( float s) {
float xn;
if (s == 0) {
xn = 0;
}
else {
xn = s / 2.0;
int counter = 1;
while (counter <= 10) {
xn = (xn + (s/ xn)) / 2.0;
counter = counter + 1;
}
}
return xn;
} int square(int what) {
return what* what;
}
int main() {
float sideA, sideB, sideC;
string answer = "yes";
while (answer == "yes") {
cout <<" Enter side A: ";
cin >> sideA;
cout << sideA << endl;
cout <<" Enter side B: ";
cin >> sideB;
cout << sideB << endl;
sideC = squareRoot( square( sideA) + square( sideB));
cout <<"Side C is " << sideC << endl;
cout <<"Continue? ";
cin >> answer;
cout << answer << endl;
}
return 0;
}
Your function:
int square (int what){
return what * what;
}
should instead return a float. When you pass the floating point numbers to this function, it returns them as integers so instead of 3.3 and 4.4 getting squared, 3 and 4 do.
You should return a float and change what to a float as well.

I want the average of the two biggest variables among the three variables n1, n2, n3. Can someone help me.

I want the average of the two biggest variables among the three variables n1, n2, n3. Can someone help me. I ask the user to enter three notes will be stored in variables n1, n2, n3. then I want the program to return the average of the two biggest variables.
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
float ra[23], qte_alunos=0;
float n1[29],n2[33],n3[33],op1[22],op2[22], fina[22];
string nome[23], curso[23];
for (int i=0; i<3; i++){
cout << "digite RA: ";
cin >> ra[i];
cout << "digte nome: ";
cin >> nome[i];
cout << "digite curso: ";
cin >> curso[i];
cout << "digite N1: ";
cin >> n1[i];
cout << "digite N2: ";
cin >> n2[i];
cout << "digite N3: ";
cin >> n3[i];
if (n1[i] > n2[i] && n2[i] > n3[i]){
n1[i] = op1[i];
n2[i] = op2[i];
}
if (n2[i] > n3[i] && n3[i] > n1[i]){
n2[i] = op1[i];
n3[i] = op2[i];
}
if (n3[i] > n1[i] && n1[i] > n2[i]){
n3[i] = op1[i];
n1[i] = op2[i];
}
fina[i] = (op1[i]+op2[i])/2;
if (fina[i] > 6 ){
cout << "aprovado " << fina[i];
}
if (fina[i] > 4 && fina[i] < 5.9){
cout << "exame " << fina[i];
}
if (fina[i] < 4){
cout << "reprovado " << fina[i];
}
cout << "\n" << endl;
}
return 0;
}
If I get you right you want (sum(a, b, c) - min(a, b, c)) / 2:
#include <algorithm>
#include <iostream>
int main (int argc, const char **argv) {
double a = 1;
double b = 2;
double c = 3;
double min = std::min({a, b, c});
// double max = std::max({a, b, c});
double sum = a + b + c;
// double result = ((sum - min - max) + max) / 2;
// which is:
double result = (sum - min) / 2;
std::cout << result << '\n';
}

Classifying digits of an integer value

I spent a day on this code for count even and zero and odd numbers
From long datatype I used a function to send data. Here is the code
#include <iostream>
using namespace std;
void digitCount(long long int &num);
int main ()
{
long long int num;
cout <<"Enter any No. " <<endl;
cin >>num;
cout <<endl;
digitCount(num);
return 0;
}
void digitCount(long long int &num)
{
int e = 0, z = 0, o = 0, x = 0;
for (int i = 0; i <= num; i++)
{
x= num % 10;
if(x == 0)
{
++z;
num = num / 10;
}
else if(x%2==1)
{
++o;
num = num / 10;
}
else
{
++e;
num = num / 10;
}
}
cout << "No of zeros Digits = " << z<< endl;
cout << "No of odd Digits = " << o << endl;
cout << "No of Even Digits = " << e << endl;
}
the problem is when I count odd numbers there is a number missed
for example when i input : 12345
the result is
no of even : 2
no of odd : 2 (should be 3)
no of zero : 0
and here the question :
Write a function that takes as parameter an integer (as a long value) and returns the number of odd, even, and zero digits. Also write a program to test your function. Use pass by reference method.
Instead of the for loop you should use:
while (num > 0)
You're constantly changing num and when it gets to 1 (in your 12345 example), i is at 3. I also modified your digitcount to demonstrate some decent formatting for readable code.
void digitCount(long long int &num) {
int e(0), z(0), o(0), x(0);
while (num > 0) {
x = num % 10;
if (x == 0) {
z++;
}
else if (x % 2 == 1) {
o++;
}
else {
e++;
}
num /= 10;
}
cout << "No of zeros Digits = " << z << endl;
cout << "No of odd Digits = " << o << endl;
cout << "No of Even Digits = " << e << endl;
}
If you believe this solves your problem && is the best answer, please click the checkmark next to this answer. Thanks

Calculating mathematical constant e using while loop

I am currently doing a task in a book which asks me to calculate the mathematical constant e using the while loop. I managed that fairly easily, however I am having troubles calculating e^x, whereas the user inputs x and the degree of accuracy. The code I used for computing e is:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int degreeOfAccuracy, x = 1;
long double e = 1;
cout << "Enter degree of accuracy of mathimatical constant e: ";
cin >> degreeOfAccuracy;
while (x <= degreeOfAccuracy)
{
int conter = x;
int intial = x;
long double number = x;
int counter = 1;
while (conter > 1)
{
number = number*(intial-counter);
counter++;
conter--;
}
e += (1/number);
x++;
}
cout << endl << "The mathematical constantr e is: "
<< setprecision(degreeOfAccuracy) << fixed << e << endl;
system("pause");
return 0;
}
However, when I tried e^x the following code returned a completely wrong value:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int degreeOfAccuracy, x = 1, exponent;
long double e = 1;
cout << "Enter degree of accuracy of mathimatical constant e: ";
cin >> degreeOfAccuracy;
cout << "Enter the number of which you wish to raise to e: ";
cin >> exponent;
int temp = exponent;
while (x <= degreeOfAccuracy)
{
exponent = temp;
int conter = x;
int intial = x;
long double number = x;
int counter = 1;
while (conter > 1)
{
number = number*(intial-counter);
counter++;
conter--;
}
int counterr = 1;
while (counterr < x)
{
exponent *= exponent;
counterr++;
}
e += (exponent/number);
x++;
}
cout << endl << "The mathematical constantr e is: " << setprecision(degreeOfAccuracy) << fixed << e << endl;
system("pause");
return 0;
}
Any ideas where the calculations went wrong?
This line:
exponent *= exponent;
is wrong. It should be:
exponent *= temp;