Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My function does not modify its inputs

Tags:

c++

I'm trying to learn C++ and have this small beginner question:

why does the standardize function not modify its inputs?

To help with the answers, I have posted an executing code at Coliru here and the sources of my program below.

Referring to the code, the question would be: why isn't what's printed after outside the same as what's printed after inside?

#include <cstdlib>
#include <ctime>
#include <algorithm>    // std::copy    
#include <iostream>

using namespace std;

void standardize(const int n,const float x[],float ave,float sct){
    float acc=0.0f,sum=0.0f;
    sum=std::accumulate(x,x+n,0.0f);
    ave=sum/(float)n;
    std::for_each(x,x+n,[&](const float d){acc+=(d-ave)*(d-ave);});
    sct=std::sqrt(acc/(float)(n-1));
    std::cout << "inside" << std::endl;
    std::cout << ave << std::endl;
    std::cout << sct << std::endl;
    return;
}
int main(){
    const int n=1024;
    float a2[n];
    float part0=0.0f,part1=0.0f;
    std::srand(std::time(0)); 
    for(int i=0;i<n;i++)      a2[i]=std::rand()/(float)RAND_MAX;
    standardize(n,a2,part0,part1);
    std::cout << "outside" << std::endl;
    std::cout << part0 << std::endl;
    std::cout << part1 << std::endl;
}
like image 320
user189035 Avatar asked Jun 09 '26 10:06

user189035


1 Answers

You are passing ave and sct by values. Your standardize method modifies copies of those arguments, letting unchanged the original ones declared in main()

Consider passing them by reference:

void standardize(const int n,const float x[],float& ave,float& sct)
like image 70
quantdev Avatar answered Jun 12 '26 00:06

quantdev