C + + weighted mean calculation method with Feponac number as weights WMA

Source: Internet
Author: User

#pragmaOnce#include<iostream>using namespacestd;template<typename t>Double*wma (T &tarray,intIwmalen)//Application Template Data Type TypeName T refers to an external array tarray{    intIarraylen =sizeof(Tarray)/sizeof(tarray[0]);//Calculate incoming Array length = Total Group byte size/first element byte sizecout <<"Length of import array is:"<< Iarraylen <<Endl; cout<<"Import Array is:";  for(inti =0; i < Iarraylen; i++)    {        if(i = =0) cout<<"["<< Tarray[i] <<" "; Else if(i = = Iarraylen-1) cout<< Tarray[i] <<"]"<<Endl; Elsecout<< Tarray[i] <<" "; }    if(Iarraylen >= iwmalen && iwmalen >0)//calculate the premise that the array length cannot be less than the number of days the mean value is calculated    {        int*pwmaw =New int[Iwmalen];//weigh the number of Feponac series.        intISUMW =0;//the arithmetic and the Feponac sequence        Switch(Iwmalen) { Case 1: {cout<<"Creating Fabonacci Numbers:"<<Endl; pwmaw[0] =1; cout<<"[0] ="<< pwmaw[0] <<Endl; ISUMW= pwmaw[0];  Break; }             Case 2: {cout<<"Creating Fabonacci Numbers:"<<Endl; pwmaw[0] =1; pwmaw[1] =1; cout<<"[0] ="<< pwmaw[0] <<Endl; cout<<"[1] ="<< pwmaw[1] <<Endl; ISUMW= pwmaw[0] + pwmaw[1];  Break; }            default: {cout<<"Creating Fabonacci Numbers:"<<Endl; pwmaw[0] =1; pwmaw[1] =1; cout<<"[0] ="<< pwmaw[0] <<Endl; cout<<"[1] ="<< pwmaw[1] <<Endl; ISUMW= pwmaw[0] + pwmaw[1];  for(inti =2; i < Iwmalen; i++) {Pwmaw[i]= Pwmaw[i-1] + pwmaw[i-2];//Feponac formula Algorithm for general termcout <<"["<< I <<"] = "<< Pwmaw[i] <<Endl; ISUMW+=Pwmaw[i]; }                 Break; }} cout<<"The sum of those Fabonacci numbers is:"<< ISUMW <<Endl; intn = iwmalen;//The fixed mean calculates the number of days, because the Iwmalen in the back will gradually increase the change        Double*pwmav =New Double[Iarraylen];//used to save mean valuescout <<"The WMA values are:"<<Endl;  while(Iwmalen <=Iarraylen) {            DoubleISUMV =0;  for(inti = iwmalen-n, j =0; i < Iwmalen; i++, J + +) {ISUMV+ = tarray[i] *Pwmaw[j]; } Pwmav[iwmalen-1] = ISUMV/ISUMW;//algorithm WMA = (array of values * Feponac number of value weights) the first n and/Feponac series Plus andcout <<"wma["<< Iwmalen-1<<"] = "<< Pwmav[iwmalen-1] <<Endl; Iwmalen++; }        Delete(PWMAW);//The pointer ends here, releasing the pointercout <<"Remeber to delete the return pointer of WMA (...)! \ n"<< Endl;//function returns a pointer to a new pointer in the heap after the prompt is deleted        returnPwmav; }    Else{cout<<"error!\n"<<Endl; returnNULL; }}intMain () {intArray[] = {1,2,5,3,4,6, -,7}; Double*p = WMA (Array,5); Delete(P); Cin.Get(); return 0;}

C + + weighted mean calculation method with Feponac number as weights WMA

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.