How to print highest and lowest value from an input file

I have to create a program that reads and prints the highest and lowes

Page 1 of 1

7 Replies - 7223 Views - Last Post: 23 July 2008 - 10:05 PM Rate Topic: -----

#1 ozz_blade  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-July 08

How to print highest and lowest value from an input file

Post icon  Posted 23 July 2008 - 05:59 AM

// 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;
}


Is This A Good Question/Topic? 0
  • +

Replies To: How to print highest and lowest value from an input file

#2 polymath  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 670
  • Joined: 04-April 08

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?
Was This Post Helpful? 0
  • +
  • -

#3 ozz_blade  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-July 08

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.
Was This Post Helpful? 0
  • +
  • -

#4 ozz_blade  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-July 08

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.

#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;
}


Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

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.
Was This Post Helpful? 0
  • +
  • -

#6 ozz_blade  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-July 08

Re: How to print highest and lowest value from an input file

Posted 23 July 2008 - 12:53 PM

View PostNickDMax, on 23 Jul, 2008 - 12:50 PM, said:

Please do not make duplicate posts. Topics merged.




ok, sorry for that
Was This Post Helpful? 0
  • +
  • -

#7 penguin2  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 90
  • Joined: 22-July 08

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:
//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];
  }
}


Was This Post Helpful? 0
  • +
  • -

#8 ozz_blade  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-July 08

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)

  • Attached File  test.txt (302bytes)
    Number of downloads: 101

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1