Min/Max/counter/while loop from file


 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Min/Max/counter/while loop from file
# 1  
Old 10-12-2012
Min/Max/counter/while loop from file

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:
The program is supposed to read in text from a given file ( different samples provided in the homework but not relevant here), set the first number in the file as min and max number, then use a while loop to change the min and max number according to which number comes next in the file. Example: if the first number is 1, the min and max is now 1. The next number is 3, the min stays at 1 and the max is now 3 and this continues until end of file. The program is also supposed to count how many integers are in the file.


2. Relevant commands, code, scripts, algorithms:



3. The attempts at a solution (include all code and scripts):
Code:
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<string>
using namespace std;


int main()
{
  ifstream fin;                    // declare an input file stream
  string file_name;
  double x,min,max,count;
  
 

  cout << "Enter input  file name:";
  cin>>file_name;
  
  fin.open(file_name.c_str(),ios::in);       // open file file_name for input
  
  if (!fin.is_open())                      // check if file is open for input
    {
      cerr << "Unable to open input file stream  " << file_name << endl;
      exit(10);
    }
     
  fin>>x; // read txt from file
  x=min; // setting first number imput as min
  x=max; // setting first number imput as max
   
 
  count = 0; // initialize counter
  
  while(fin)
    {
      fin>>x;
      if(x<min)
    x=min;
      if(x>max)
    x=max;
      count++;
      fin>>x;
    }
  cout<<"total number"<<count<<endl;
  cout<<"Minimum: "<<min<<endl;
  cout<<"Maximum: "<<max<<endl;

  fin.close();        // close file stream fin
  return 0;  
}

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
The Ohio State University, Columbus Ohio, USA, Yi Wang, cse1222


Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by vbe; 10-12-2012 at 05:53 AM..
# 2  
Old 10-12-2012
And?

What is your issue...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find min and max time taken from a log file

You have a log file as attached in sample input with various operations and time taken by each of them. Write a script to find the min and max time taken for each operation. Sample output is attached. Sample Input is given as below: operation1,83621 operation2,72321 operation3,13288... (1 Reply)
Discussion started by: Chandan_Bose
1 Replies

2. Shell Programming and Scripting

Get min and max value in column

Gents, I have a big file file like this. 5100010002 5100010004 5100010006 5100010008 5100010010 5100010012 5102010002 5102010004 5102010006 5102010008 5102010010 5102010012 The file is sorted and I would like to find the min and max value, taking in the consideration key1... (3 Replies)
Discussion started by: jiam912
3 Replies

3. Shell Programming and Scripting

Get the min avg and max with awk

aaa: 3 ms aaa: 2 ms aaa: 5 ms aaa: 10 ms .......... to get the 3 2 5 10 ...'s min avg and max something like min: 2 ms avg: 5 ms max: 10 ms (2 Replies)
Discussion started by: yanglei_fage
2 Replies

4. Shell Programming and Scripting

Average, min and max in file with header, using awk

Hi, I have a file which looks like this: FID IID MISS_PHENO N_MISS N_GENO F_MISS 12AB43131 12AB43131 N 17774 906341 0.01961 65HJ87451 65HJ87451 N 10149 906341 0.0112 43JJ21345 43JJ21345 N 2826 906341 0.003118I would... (11 Replies)
Discussion started by: kayakj
11 Replies

5. Shell Programming and Scripting

File merging based on different counter loop

hello, File 1 main Group sub group MIT VAR_1D_DATA_TYPE 23-03-2012 MIT VAR_1D_DATA_TYPE 22-03-2012 MIT VAR_10D_DATA_TYPE 23-03-2012 MIT VAR_10D_DATA_TYPE 22-03-2012 MIT ... (0 Replies)
Discussion started by: manas_ranjan
0 Replies

6. Shell Programming and Scripting

To get max/min Date/Timestamp from a file

I want to get maximum/minimum date/timestamp from a data file ? Sample Input File ============= rec#,order_dt,ext_ts 1,2010-12-01,2010-12-01 17:55:23.222222 2,2011-11-05,2010-12-01 19:55:23.222222 3,2009-10-01,2010-12-01 18:55:23.222222 for above file Maximum Order_dt = 2011-11-05... (5 Replies)
Discussion started by: vikanna
5 Replies

7. Shell Programming and Scripting

How to get the counter value incremented after every 1 min?

I want to check the counter value for every 1 min until the particular counter value is reached and it should exit. Counter value: 15.( For Example) counter = 1 The start time is noted using Localtime. How can i do this in perl? Regards Archana (1 Reply)
Discussion started by: vanitham
1 Replies

8. Shell Programming and Scripting

get min, max and average value

hi! i have a file like the attachement. I'd like to get for each line the min, max and average values. (there is 255 values for each line) how can i get that ? i try this, is it right? BEGIN {FS = ","; OFS = ";";max=0;min=0;moy=0;total=0;freq=890} $0 !~ /Trace1:/ { ... (1 Reply)
Discussion started by: riderman
1 Replies

9. Shell Programming and Scripting

how to find min, max dates in a file

hello friends...:-) i need some help i have a file cantain like this Star1 ,NetWork,09/02/2008 Star1 ,NetWork,10/02/2008 Star1 ,NetWork,11/02/2008 Star2 ,NetWork,08/03/2008 Star2 ,NetWork,09/04/2008 Star2 ,NetWork,10/05/2008 i need to find out min, max dates the output look like... (6 Replies)
Discussion started by: gemini106
6 Replies

10. Shell Programming and Scripting

min and max value of process id

We are running a AIX 5.2 OS. Would anyone happen to know what the max value for a process id could be? Thanks jerardfjay :) (0 Replies)
Discussion started by: jerardfjay
0 Replies
Login or Register to Ask a Question