#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int MAXSIZE = 5000;
double maximum(double nums[], int size);
double combineMax(double max1, double max2, int size1, int size2);
int main()
{
string inputFileName1, inputFileName2;
ifstream ifp1, ifp2;
ofstream ofp;
double *num1 = 0, num2[MAXSIZE], *tmpPtr;
int ctr1 = 0, ctr2 = 0;
// Dynamic allocation of num1
cout << num1 << endl;
tmpPtr = num2;
// Prompt for filename and open
cout << "Enter the 2 files to open.: ";
cin >> inputFileName1 >> inputFileName2;
ifp1.open(inputFileName1.c_str());
ifp2.open(inputFileName2.c_str());
if(ifp1.is_open() == true && ifp2.is_open() == true)
{
// Loop through the first file one to determine the size
double tmpDbl;
do
{
ifp1 >> tmpDbl;
if(ifp1.eof()!= true)
{
ctr1++;
}
} while(ifp1.eof() != true);
num1 = new double[ctr1];
ctr1 = 0;
ifp1.close();
ifp1.clear();
ifp1.open(inputFileName1.c_str());
do
{
ifp1 >> *(num1 + ctr1);
if(ifp1.eof()!= true)
{
ctr1++;
}
} while(ifp1.eof() != true);
ifp1.close();
do
{
ifp2 >> *tmpPtr;
cout << "tmpPtr address = " << tmpPtr <<
"tmpPtr value = " << *tmpPtr << endl;
tmpPtr++;
if(ifp2.eof()!= true)
{
ctr2++;
}
} while(ifp2.eof() != true);
ifp2.close();
// Take the average of each array
ofp.open("Averages.txt");
if(ofp.is_open() == true)
{
double max1 = maximum(num1, ctr1);
ofp << "The average for max1 = " << max1 << endl;
double max2 = maximum(num2, ctr2);
ofp << "The average of max2 = " << max2 << endl;
double totalMax = combineMax(max1, max2, ctr1, ctr2);
ofp << "The overall average is " << totalMax << endl;
ofp.close();
}
}
else
{
cout << "Invalid filename(s): " << inputFileName1 << ", "
<< inputFileName2 << endl;
}
return (0);
}
double maximum(double nums[], int size)
{
double max = 0.0;
if(size > 0)
{
for(int i = 0; i < size; i++)
{
max += *(nums + i);
}
max /= size;
}
return(max);
}
double combineMax(double max1, double max2, int size1, int size2)
{
double max = 0.0;
max = (max1 * size1 + max2 * size2) / (size1 + size2);
return(max);
}
Why am i getting ints as the output I want doubles??? :evil: