Here is what i have to do
Create Class CSalary with private member variables
string m_strProfession
double m_dSalary
Constructors:Default,Copy,Explicit
Functions:
1/string GetProf(void),double GetSalary(void)
2/SetProf(const string val),SetSalary(const double val)
3/Output(ostream&) and Input(istram&)
4/Operators:<<,>>,=,==,<,double operator*(const CSalary&obj)
Create Class CCalcCorr with private member variables:
vector<CSalary>m_vCity1//data for city 1
vector<CSalary>m_vCity2//data for city 2
double m_dCorr//correlation coefficient
Constructors:
Explicit by data filenames string fNmae1,string fName2
CCalcCorr(const string& strFileName1,const string& strFileName2)

<profession> <salary>
Web Developer 5000

Functions:/I want help for these functions/
Colculate correlation coefficient btw 2 vectors in m_dCorr and return it in dCorr.Return false if can't calculate

Formulas:
Corr=SUMxy/sqrt(SUMxxSUMyy) where :
SUMxx = SUM(City1City1)-SUM(City1)SUM(City1)/n
SUMxy = SUM(City1City2)-SUM(City1)SUM(City2)/n
SUMyy = SUM(City2'City2)-SUM(City2)SUM(City2)/n where:
City1 - current value m_dSalary from m_vCityl
City2 - current value m_dSalary from m_vCity2
n- size of m_vCityl and m_vCity2

-return correlation
-change data for profession/write dCity1,dCity2/
-read data/return dCity1,dCity2/
-void WriteTo()-write stream data
<Prof,Salary1(m_vCity1),Salary2(m_vCity2)>
-input/output operators
Main function-read data from input files,create CCalcCorr,testing functions

Here is my code

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <iterator>

using namespace std;
#define FILE_ERROR -1
#define DATA_ERROR 1
class CSalary {
  string m_strProfession; double m_dSalary;

public:
  CSalary(void) {
    m_strProfession="";
     m_dSalary=0.; 
                 }
 ~CSalary(void) {}

 CSalary( const string xarg, const double& yarg)
{ 
          m_strProfession=xarg; 
          m_dSalary=yarg; 
}

 CSalary(const CSalary& xy)
{ 
m_strProfession=xy.m_strProfession;
m_dSalary=xy.m_dSalary; 
}
 string GetProf(void) const
 { 
        return m_strProfession;
 }
 double GetSalary(void) const 
 { 
        return m_dSalary;
 }
 void SetProf( const string val)
{ 
      m_strProfession=val; 
}
 void SetSalary( const double val)
{
       m_dSalary=val;
}
 const CSalary& operator =(const CSalary& xy) 
 {
m_strProfession = xy.m_strProfession;
m_dSalary = xy.m_dSalary; 
return *this;
 }
 bool operator ==( const CSalary& obj) const {
 return (m_strProfession == obj.m_strProfession);
 }
 bool operator < (const CSalary& obj) const {
 return (m_strProfession < obj.m_strProfession);
 }
 double operator *( const CSalary& obj) const {
 return m_dSalary * obj.m_dSalary;
 }
 void Output( ostream& toStream ) const {
 toStream << "Profesiq: " << GetProf() << " Zaplata: "
<< GetSalary();
 }
 void Input( istream& fromStream ) {
 fromStream >> m_strProfession >> m_dSalary;
 }
 friend ostream& operator<<(ostream& toStream, const
CSalary& obj) {
 obj.Output(toStream);
 return toStream;
 }
 friend istream& operator>>(istream& fromStream,
CSalary& obj) {
 obj.Input(fromStream);
 return fromStream;

 }
};
class CSqrt{
static double m_Res;
static double m_ResSqrt;
public:
 CSqrt() { m_ResSqrt=m_Res = 0.; }
 bool operator ()(CSalary& obj) {
     m_ResSqrt += obj.GetSalary()*obj.GetSalary();
     m_Res += obj.GetSalary();


 }
     double GetRes() { return (double)m_Res; }
     double GetResSqrt() { return (double)m_ResSqrt; }

};
double CSqrt::m_Res=0;
double CSqrt::m_ResSqrt=0;

class CCalcCorr {
 vector<CSalary> m_vCity1; 
 vector<CSalary> m_vCity2; 
 double m_dCorr; 
public:
 CCalcCorr(const char* strFileName1, const char*
strFileName2) {
 ifstream iStream1(strFileName1);
 ifstream iStream2(strFileName2);
 if(iStream1.good() && iStream2.good()) {
 copy (istream_iterator<CSalary>(iStream1),
istream_iterator<CSalary> (),
 back_inserter(m_vCity1) );
 copy (istream_iterator<CSalary>(iStream2),
istream_iterator<CSalary> (),
 back_inserter(m_vCity2) );
 if( m_vCity1 != m_vCity2 ) {
 sort( m_vCity1.begin(), m_vCity1.end() );
 sort( m_vCity2.begin(), m_vCity2.end() );
 if( m_vCity1 != m_vCity2 ) throw DATA_ERROR;
 }

 } else throw FILE_ERROR;
 }
 double getCorr() { return m_dCorr; }
 void writeTo(){
 for( int i=0; i<m_vCity1.size(); ++i) {
 cout<<m_vCity1[i].GetProf() << " " <<
m_vCity1[i].GetSalary();
 cout<< " " << m_vCity2[i].GetSalary() << endl;
 }
 }
 void getSalaryByProfession( const string& strProf,
 double& dCity1, double& dCity2) {
 CSalary toFind1( strProf, dCity1);
 CSalary toFind2( strProf, dCity2);
 vector<CSalary>::iterator
it1=find( m_vCity1.begin(),m_vCity1.end(), toFind1 );
 if( it1 != m_vCity1.end())
dCity1=(*it1).GetSalary();
 vector<CSalary>::iterator
it2=find( m_vCity2.begin(),m_vCity2.end(), toFind2 );
 if( it2 != m_vCity2.end())
dCity2=(*it2).GetSalary();
 }
 void setSalaryByProfession( const string& strProf,
 const double& dCity1, const double& dCity2) {
 CSalary toFind1( strProf, dCity1);
 CSalary toFind2( strProf, dCity2);
 vector<CSalary>::iterator
it1=find( m_vCity1.begin(),m_vCity1.end(), toFind1 );
 if( it1 != m_vCity1.end()) *it1=toFind1;
 vector<CSalary>::iterator
it2=find( m_vCity2.begin(),m_vCity2.end(), toFind2 );
 if( it2 != m_vCity2.end()) *it2=toFind2;
 }
 bool calcCorr( double& dCorr ) {
 int n1=m_vCity1.size();
 if( !n1 ) return false;
 CSqrt oF1;
 for_each(m_vCity1.begin(), m_vCity1.end(),oF1);

 double SUMx = oF1.GetRes();
 double SUMxx = oF1.GetResSqrt()-SUMx*SUMx/n1;
 CSqrt oF2;
 for_each(m_vCity2.begin(), m_vCity2.end(),oF2);
 double SUMy = oF2.GetRes();
 double SUMyy = oF2.GetResSqrt()-SUMy*SUMy/n1;
 double SUMxy =
 inner_product(m_vCity1.begin(), m_vCity1.end(),m_vCity2.begin(),0)-SUMx*SUMy/n1;
 m_dCorr=dCorr=SUMxy /sqrt(SUMxx*SUMyy);
 return true;
 }
};
int main() {
 try {
 CCalcCorr vectColl("file1.txt", "file2.txt");
 vectColl.writeTo();
 double dRes=0.;
 vectColl.calcCorr( dRes );
 cout<<"*********\n";
 cout<<"Correlation = "<<vectColl.getCorr()<<endl;
 vectColl.setSalaryByProfession( "p01", 9.83,
8.64);
 double dCity1,dCity2;
 vectColl.getSalaryByProfession( "p01", dCity1,
dCity2);
 cout<<"Sal : " << dCity1 << " "<< dCity2 << endl;
 vectColl.writeTo();
 }
 catch (int errType) {
 switch( errType ) {
 case FILE_ERROR: cout<<"File Error\n"; break;
 case DATA_ERROR: cout<<"Data Error\n"; break;
 }
 }
return 0;}

When i build it i got these errors?


Error   3   error C4716: 'CSqrt::operator()' : must return a value 94 main.cpp
  here is code


    bool operator ()(CSalary& obj) {
         m_ResSqrt += obj.GetSalary()*obj.GetSalary();
         m_Res += obj.GetSalary();
         }




    Warning 1   warning C4018: '<' : signed/unsigned mismatch        128  main.cpp


` for( int i=0; i<m_vCity1.size(); ++i)....`


    Warning 2   warning C4244: '=' : conversion from 'double' to 'int', possible loss of data   53  numeric


`       _Val = _Func1(_Val, _Func2(*_Firs

    t1, *_First2));`

    I have and two text files file 1 and file 2 and i have to see something like this in output




    file1:
     <prof> <sal>
    p01 8.64
    p02 6.17
    p03 6.04
    p04 6.23
    p05 7.3
    p06 10.26
    p07 13.96
    p08 12.72
    p09 13.29
    p10 10.88
    p11 9.91
    p12 5.89

     file2:
     <prof> <sal>
    p01 9.83
    p02 7.77
    p03 5.6
    p04 7.26
    p05 8.95
    p06 10.7
    p07 13.55
    p08 12.47
    p09 11.59
    p10 9.9
    p11 10.04
    p12 6.04

    p01 8.64 9.83
    p02 6.17 7.77
    p03 6.04 5.6
    p04 6.23 7.26
    p05 7.3 8.95
    p06 10.26 10.7
    p07 13.96 13.55
    p08 12.72 12.47
    p09 13.29 11.59
    112
    p10 10.88 9.9
    p11 9.91 10.04
    p12 5.89 6.04
    *********
    Correlation = 0.865338
    Sal : 9.83 8.64

This function:

bool operator ()(CSalary& obj) {
     m_ResSqrt += obj.GetSalary()*obj.GetSalary();
     m_Res += obj.GetSalary();
     }

Doesn't appear to be complete. It doesn't use the parameter and there's no return statement

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.