1

On line 20 and 29, it says:

'av' was not declared in this scope

And on line 30, it says:

's' was not declared in this scope

What's the problem?

#include <iostream>
#include <cmath>
using namespace std;
       
const int SIZE= 6;
int nums[SIZE]= {1,5,6,43,7,9};
int average(double av){
    //av is to get the average of the array
    int sum=0;
    for (int i=0; i<SIZE; i++){
        sum+= nums[i];
        av= static_cast<double>(sum/SIZE);
    }
    return av;
}
        
        
int deviation( double s ){
        
    int function = nums[0]-average(av);
    int nominator = pow(function, 2);
    s = sqrt(nominator/SIZE);
    return s;
}
        
        
int main(){
    cout << "Numbers: "<< nums[SIZE]<< endl;
    cout << "Average: "<< average(av)<<endl;
    cout << " Standard Deviation: "<< deviation(s)<<endl;
        
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    We don’t have line numbers – Taekahn Jul 09 '22 at 01:22
  • You have a variable `av` (a parameter passed by value) in the function `average()` that exists only as long as the function is running. You attempt to use some variables named `av` in `main()` & `deviation()` - but no such variables exits or are visible to those functions. Time to go back to your book & review how variables work. Scope, visibility & lifetime. – Avi Berger Jul 09 '22 at 01:27
  • 2
    [C++ book guide](https://stackoverflow.com/q/388242/631266) – Avi Berger Jul 09 '22 at 01:29
  • Actually, there is no reason for `av` to be a parameter to `average()``. Consider making it a local variable. – Avi Berger Jul 09 '22 at 01:33
  • Also you declared `av` and `s` as `double` but the return type is `int`. If you want to return a `double`, change the return type. There may be a compiler warning about type conversion. – Matt Jul 09 '22 at 03:11

1 Answers1

1

Both variables are function parameters and they exist only inside in the functions they were declared.

Parameters, in the way they was declared there, are to pass data INTO the functions. You can remove the parameters from the function's signatures, so they gonna be like...

int average(){
 // Your code...
}

int deviation(){
 // Your code...
}

... and declare the variables inside the main function body and use them to store the values returned by the functions....

int main(){
    double av = average();
    double s = deviation();
    cout << "Numbers: "<< nums[SIZE]<< endl;
    cout << "Average: "<< average(av)<<endl;
    cout << " Standard Deviation: "<< deviation(s)<<endl;
    
    }

... OR you can make both global like you did with SIZE and nums.

S4NR-3000
  • 57
  • 5