// Headers
#include <iostream>
#include <algorithm>
#include <iterator>
#include <fstream>
#include <cstddef>
#include <cstdlib>
using namespace std;
// Define variables
typedef float value_type;
int main ()
{
// Does the file exist?
ifstream infile;
infile.open("test.txt");
ofstream outfile;
outfile.open("outdata.txt");
value_type data[11*11];
copy(istream_iterator<value_type>(infile),istream_iterator<value_type>(),data);
// Initialize Program
int n=0,m=0;
for (n=0; n < 11; n++)
{
for (m=0; m < 11; m++)
{
outfile << data[(n*11+m)]<< endl;
}
std::clog << std::endl;
}
infile.close();
outfile.close();
return 0;
}
How to print highest and lowest value from an input fileI have to create a program that reads and prints the highest and lowes
Page 1 of 1
7 Replies - 5818 Views - Last Post: 23 July 2008 - 10:05 PM
#1
How to print highest and lowest value from an input file
Posted 23 July 2008 - 05:59 AM
Replies To: How to print highest and lowest value from an input file
#2
Re: How to print highest and lowest value from an input file
Posted 23 July 2008 - 07:58 AM
So what would be your question? What problems are you having? Is this meant to be some kind of code snippet?
#3
Re: How to print highest and lowest value from an input file
Posted 23 July 2008 - 08:32 AM
Oh sorry if i was not that clear. Well, I have to create a program that reads the highest value and lowest value from a data(ASCII data) which it looks like a matrix (rows and columns). In this case the input file is called ("test.txt"), and it contains the ASCII data (11 rows x 11 columns) with numbers on it. The program needs to run the data and creates an output file which I named ("outdata.txt") (I have gotten all that sofar). However, I do not know how to come out with a function to read the highest and lowest value from the input file to the output screen. In other words, the program runs fine and creates the output file that reads all the data from the inputfile, but it does not determine what the highest/lowest values are into the screen.
#4
Re: How to print highest and lowest value from an input file
Posted 23 July 2008 - 12:40 PM
I have this program that runs and reads the input file data ("test.txt") , and it creates the output file "outdata.txt" (this runs ok);however, the task now is to determine what the highest and lowest values are from the data (test.txt) and show the results in the output screen. The data is presented as a matrix with rows and columns in this case the input data (test.txt) has 11 rows x 11 columns (nxm), and from the matrix the program needs to calculate what the highest and lowest value are.
using namespace std;
// Define variables
typedef float value_type;
#include <iostream> #include <algorithm> #include <iterator> #include <fstream> #include <cstddef> #include <cstdlib>
using namespace std;
// Define variables
typedef float value_type;
int main ()
{
ifstream infile;
infile.open("test.txt");
ofstream outfile;
outfile.open("outdata.txt");
value_type data[11*11];
float highest,lowest;
copy(istream_iterator<value_type>(infile),istream_iterator<value_type>(),data);
cout << " The highest value is " << highest << endl;
cout << " The lowest value is " << lowest << endl;
int n=0,m=0;
for (n=0; n < 11; n++)
{
for (m=0; m < 11; m++)
{
outfile << data[(n*11+m)];
outfile << "\t";
}
outfile << "\n";
std::clog << std::endl;
}
infile.close();
outfile.close();
return 0;
}
#5
Re: How to print highest and lowest value from an input file
Posted 23 July 2008 - 12:50 PM
Please do not make duplicate posts. Topics merged.
#6
Re: How to print highest and lowest value from an input file
Posted 23 July 2008 - 12:53 PM
#7
Re: How to print highest and lowest value from an input file
Posted 23 July 2008 - 01:07 PM
I am not sure if I understand exactly what you are doing, attaching the test.txt file would help.
I would do something kind of like this:
I would do something kind of like this:
//We will just say we have int array [i]values[/i] with 10 elements.
int highest = 0;
for(int n=0; n<11; n++) {
if(values[n] > highest) {
highest = values[n];
}
}
#8
Re: How to print highest and lowest value from an input file
Posted 23 July 2008 - 10:05 PM
I just attached the file "test.xt" just to make my problem easier to understand. I need to read the highest and lowest value from this file (test.txt) into output screen. I hope I can get a hint to solve this problem. Well, I will continue working on it today. Thanks for the replies!
Attached File(s)
-
test.txt (302bytes)
Number of downloads: 67
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|