|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming | |
Help with Minimum Program in C++
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) |
|
New to Overclock.net
|
I need help writing a program to output a minimum of a set of data.
my input file is 99 77 33 55 66 33.3 34 6.0 7.2 3.4 6 7.2 3.4 6.0 7.2 my ouput before i commented out looked as follows: X Y Z Minimum 99.0 77.0 33.0 55.0 66.0 33.3 34.0 6.0 7.2 3.4 6.0 7.2 3.4 6.0 7.2 Right now as it is written, my code will output my minimum as follows: 33 33.3 6 3.4 3.4 All of that is right, I just need to combine the 2. I am trying to create an output data file so I can then read that data back into the output file I need. Can anyone offer any suggestions? Note, the code part that is commented out produces an error of redefinition of `int main()' Code:
#include <fstream>
#include <iomanip>
#include <iostream>
using namespace std;
ofstream outfile("min.dat""out.txt");
int main();
int min(int x, int y, int z)
//99, 77, 33
{
if (x>y)
if (y>z) return z;
else return y;
else
if (x>z) return z;
else return x;
}
double min(int x, int y, double z)
//55, 66, 33.3
{
if (x>y)
if (y>z) return z;
else return (double)y;
else
if (x>z) return z;
else return (double)x;
}
double min(int x, double y, double z)
//34, 6.0, 7.2
{
if (x>y)
if (y>z) return z;
else return y;
else
if (x>z) return z;
else return (double)x;
}
double min(double x, int y, double z)
//3.4, 6, 7.2
{
if (x>y)
if (y>z) return z;
else return (double)y;
else
if (x>z) return z;
else return x;
}
double min(double x, double y, double z)
//3.4, 6.0, 7.2
{
if (x>y)
if (y>z) return z;
else return y;
else
if (x>z) return z;
else return x;
}
int main()
{
outfile << min(99, 77, 33) << endl;
outfile << min(55, 66, 33.3) << endl;
outfile << min(34, 6.0, 7.2) << endl;
outfile << min(3.4, 6, 7.2) << endl;
outfile << min(3.4, 6.0, 7.2)<< endl;
return 0;
}
/*
ifstream infile("in.dat");
struct record{
double x;
double y;
double z;
};
int main(){
record rec;
float min;
if(!infile)
cerr<<"Error:could not open input file\n";
else if(!outfile)
cerr<<"Error:could not open output file\n";
outfile<<setw(9)<<"X"<<setw(10)<<"Y"<<setw(10)<<"Z"<<setw(17)<<"Minimum\n\n";
while(infile >> rec.x >> rec.y >> rec.z){
outfile<<setiosflags(ios::showpoint | ios::fixed)
<<setprecision(1)<<setw(10)<<rec.x<<setw(10)<<rec.y<<setw(10)<<rec.z<<setw(12)<<endl;
}
return 0;
}
*/
|
|
|
|
|
|
#2 (permalink) | |||||||||||||
|
AMD Overclocker
![]() |
Some sample code for your main and min(). Remember that int's fall within the scope of float & double so you don't need to override your functions. A single function signature will work in this case.
Code:
#include <iostream>
using namespace std;
float m(float a, float b, float c)
{
float temp = (a < b ? a : b);
float answer = (temp < c ? temp : c);
return answer;
}
int main()
{
cout << m(80,1.9,3.33333) << endl;
return 0;
}
|
|||||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|