-1

Im fairly new to coding. I am doing a project with function calls, and my POW() and SQRT() functions say they're not declared in this scope. I'm not particularly sure how to solve this compiler error and appreciate any feedback

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

double average(double a,double b, double c, double d);
void stdev(double a, double b, double c, double d);
int main(){
    double num1,num2,num3,num4;
    char repeat;
    do{
    cout << "Enter four decimal numbers: ";
    cin >> num1 >> num2 >> num3 >> num4;
    average(num1, num2, num3, num4);
    stdev(num1,num2,num3,num4);
    
    cout << "Please enter y to continue: ";
    cin >> repeat;
    cout << endl;
    }
    while (repeat == 'y');
    return 0;
}
double average(double a, double b, double c, double d){
    double ave;
    ave = (a+b+c+d)/4;
    return ave;
}
void stdev(double a, double b, double c, double d, double average){
    double std, ave;
    std = SQRT(POW(a-ave,2)+POW(b-ave,2)+POW(c-ave,2)+POW(d-ave,2));
    cout << "The average = "<< ave << endl;
    cout << "The standard deviation of these numbers = "<<std<<endl;
    return;
    }

Fiona
  • 1
  • 1
  • 1
    The functions in "cmath" are all spelled with lower-case letters – UnholySheep Oct 16 '20 at 16:26
  • 4
    `SQRT` and `POW` are used without being declared. C++ identifiers are case-sensitive and the standard library only have `sqrt` and `pow` (lowercase letters). You said "**my** POW() and SQRT() function", so it seems you want to implement them. Then implement them and write their definition before the line in which they are used. – MikeCAT Oct 16 '20 at 16:27
  • FYI, `X * X` is more efficient than `pow(x, 2)`. The `pow` function uses floating point and there may be inaccuracies when converting to integer. Calling the `pow` function requires overhead and may cause the instruction cache to reload (wasting time). Some compilers may recognize the pattern and make the change, at higher optimization levels. – Thomas Matthews Oct 16 '20 at 16:31
  • BTW, your `stdev` function has a major flaw. The `ave` variable is never initialized. Maybe you meant to use the `average` parameter instead? – Thomas Matthews Oct 16 '20 at 16:33

1 Answers1

0

sqrt and pow must be in lowercase and you also missed average on stdev function definition. https://www.cplusplus.com/reference/cmath/

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

double average(double a, double b, double c, double d);
void stdev(double a, double b, double c, double d, double average);

int main() {
    double num1, num2, num3, num4;
    char repeat;
    do {
        cout << "Enter four decimal numbers: ";
        cin >> num1 >> num2 >> num3 >> num4;
        double avg = average(num1, num2, num3, num4);
        stdev(num1, num2, num3, num4, avg);

        cout << "Please enter y to continue: ";
        cin >> repeat;
        cout << endl;
    } while (repeat == 'y');
    return 0;
}

double average(double a, double b, double c, double d) {
    double ave;
    ave = (a + b + c + d) / 4;
    return ave;
}
void stdev(double a, double b, double c, double d, double average) {
    double std = sqrt(pow(a - average, 2) + pow(b - average, 2) + pow(c - average, 2) + pow(d - average, 2));
    cout << "The average = " << average << endl;
    cout << "The standard deviation of these numbers = " << std << endl;
    return;
}
w0lf
  • 353
  • 3
  • 10