1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <cstdlib>
using namespace std;
//Function Prototypes
void readfile(string, string );
float sqDiff(int[], int[], int);
float sum(int[], int);
float f(int [], int, int, int);
int main()
{
int size=0;
float setA[size];
float setB[size];
string filename1; //declare first file
string filename2;//declare second file
cout<<"Enter file 1 : ";
getline(cin,filename1); //input name for first file
cout<<"Enter file 2 : ";
getline(cin,filename2); //input name for second file
readfile(filename1,filename2); //read file 1 & 2 into array
return 0;
}
void readfile(string filenameA, string filenameB)
{
ifstream indata;
ifstream indata2;
indata.open(filenameA.c_str());
indata2.open(filenameB.c_str());
int num=0;
int num2=0;
int setA[num];
int setB[num2];
int sqdiff;
//PROBLEM START
while(!indata.eof())
{
indata>>setA[num];
num++;
}
indata.clear();
while(!indata2.eof())
{
indata2>>setB[num2];
num2++;
}
indata2.clear();
//PROBLEM END
if(num==num2)
{
sqdiff=sqDiff(setA, setB, num);
}
else
{
cout<<"** Both arrays are not of the same size. Please try again! **";
}
}
float sqDiff(int arr1[], int arr2[], int size)
{
int squarediff[size];
int addition1, addition2;
float Formula;
int diff[size];
for(int i=0; i<size; i++)
{
diff[i]=arr1[i]-arr2[i];
squarediff[i]=pow(diff[i],2);
}
addition1=sum(arr1,size);
addition2=sum(arr2,size);
Formula=f(squarediff, addition1, addition2, size);
cout<<endl<<endl<<"Result: f= "<<Formula;
}
float sum(int array[], int ArraySize)
{
int s=0;
for(int i=0; i<ArraySize; i++)
{
s+=array[i];
}
return s;
}
float f(int SQDIFF[], int SUM1, int SUM2, int SIZE)
{
int SumSQdiff=0;
float MeanX;
float MeanY;
float AbsoluteDiff;
float F;
for(int i=0; i<SIZE; i++)
{
SumSQdiff+=SQDIFF[i];
}
MeanX=static_cast<float>(SUM1)/SIZE;
MeanY=static_cast<float>(SUM2)/SIZE;
AbsoluteDiff=fabs(MeanX-MeanY);
F=(sqrt(SumSQdiff))/AbsoluteDiff;
return F;
}
| |