- #1
jimmy007
- 8
- 0
please help me solving errors in this problem .
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <cstdlib>
using namespace std;
const size_t CAPACITY(8);
// struct needed for this program.
struct Points {
double x, y;
};
// function prototype to open the user file and make array.
void makeArray(Points[], size_t&);
// function prototype to calculate distance of points from origin.
void distance(Points[], const Points, size_t&);
double calculateAverage(Points[], double);
void outputFile(Points[], double, ofstream&);
int main()
{
Points array[CAPACITY];
Points dis;
size_t size(0);
ofstream outFile;
double average(0);
makeArray(array, size);
distance(array, dis, size);
average = calculateAverage(array, average);
outputFile(array, average, outFile);
}
// function to open the user choice of input file and fill in the array.
void makeArray(Points array[], size_t& size)
{
string fileName;
ifstream inFile;
// ask the user for input file.
cout << "Enter a filename:";
cin >> fileName;
// open the user input file.
inFile.open(fileName.c_str());
// check if the file exists or not.
if(inFile.fail())
{
cout << "Invalid filename.";
cout << "Please enter a filename again:";
cin >> fileName;
inFile.clear();
inFile.open(fileName.c_str());
}
Points temp;
// read the points from the file.
while(inFile >> temp.x >> temp.y )
{
array[size] = temp;
size++;
}
}
// function to calculate the distance of the points from the origin.
void distance(Points array[], const Points dis, size_t& size)
{
// for loop to calculate the distance of each point from origin.
for(int i = 0; i < size; i++)
{
array = sqrt(static_cast<double>(pow( dis.x,2)+pow(dis.y,2) )) ;
}
}
// function to calculate the average of all the distance of points.
double calculateAverage(Points array[], double average)
{
int howMany(0);
int total(0);
// for loop to calculate the sum.
for(int i = 1; i < howMany; i++)
{
total += array;
howMany++;
}
// calculate the average of distances.
average = total/howMany;
return average;
}
// function to output if average is greater than the points.
void outputFile(Points array[], double average, ofstream& outFile)
{
if(average > array)
{
outFile.open("Output.txt");
outFile << array<<endl;
}
}
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <cstdlib>
using namespace std;
const size_t CAPACITY(8);
// struct needed for this program.
struct Points {
double x, y;
};
// function prototype to open the user file and make array.
void makeArray(Points[], size_t&);
// function prototype to calculate distance of points from origin.
void distance(Points[], const Points, size_t&);
double calculateAverage(Points[], double);
void outputFile(Points[], double, ofstream&);
int main()
{
Points array[CAPACITY];
Points dis;
size_t size(0);
ofstream outFile;
double average(0);
makeArray(array, size);
distance(array, dis, size);
average = calculateAverage(array, average);
outputFile(array, average, outFile);
}
// function to open the user choice of input file and fill in the array.
void makeArray(Points array[], size_t& size)
{
string fileName;
ifstream inFile;
// ask the user for input file.
cout << "Enter a filename:";
cin >> fileName;
// open the user input file.
inFile.open(fileName.c_str());
// check if the file exists or not.
if(inFile.fail())
{
cout << "Invalid filename.";
cout << "Please enter a filename again:";
cin >> fileName;
inFile.clear();
inFile.open(fileName.c_str());
}
Points temp;
// read the points from the file.
while(inFile >> temp.x >> temp.y )
{
array[size] = temp;
size++;
}
}
// function to calculate the distance of the points from the origin.
void distance(Points array[], const Points dis, size_t& size)
{
// for loop to calculate the distance of each point from origin.
for(int i = 0; i < size; i++)
{
array = sqrt(static_cast<double>(pow( dis.x,2)+pow(dis.y,2) )) ;
}
}
// function to calculate the average of all the distance of points.
double calculateAverage(Points array[], double average)
{
int howMany(0);
int total(0);
// for loop to calculate the sum.
for(int i = 1; i < howMany; i++)
{
total += array;
howMany++;
}
// calculate the average of distances.
average = total/howMany;
return average;
}
// function to output if average is greater than the points.
void outputFile(Points array[], double average, ofstream& outFile)
{
if(average > array)
{
outFile.open("Output.txt");
outFile << array<<endl;
}
}