Max and Min values from a group of numbers

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2005
Posts: 5
Reputation: yvonnemo is an unknown quantity at this point 
Solved Threads: 0
yvonnemo yvonnemo is offline Offline
Newbie Poster

Max and Min values from a group of numbers

 
0
  #1
Jan 29th, 2005
Hi, I been trying this problem for 2 days and monday 31st is the due date
I tried to use a while loop but the numbers I get are not even close. What I did on the Enter your code is the codes below the question

//********************************************************************************************
// 
// MaxMinIntegers.cpp          
//
// Program that reads int values from a text file named "numbers01.txt"
// and writes out the largest and smallest values found. Numbers in the
// file may be separated by blanks or line breaks.
//
//********************************************************************************************

#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;


int main() {
    // Declare input stream
    ifstream fin;
    int min, max, val;
    int isFirst = true;
    fin.open("numbers01.txt");



	// --------------------------------
	// ----- ENTER YOUR CODE HERE -----
	// --------------------------------


	// --------------------------------
	// --------- END USER CODE --------
	// --------------------------------


    fin.close();

    cout << "Largest value is " << max << ", smallest value is " << min << "\n";
}




Here is what I did
              
fin >> isFirst;
while (fin >> val)
fin >> val;
if (isFirst > val)
min = val
max = isFirst
else
max = val
min = isFirst
Last edited by alc6379; Jan 31st, 2005 at 9:16 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Max and Min values from a group of numbers

 
0
  #2
Jan 29th, 2005
you're going about it wrong but you probably knew that already.

Here's what to do (I leave it to you to translate that into C++):
  1. declare variable for smallest value
  2. declare variable for largest value
  3. set both to first value in the file
  4. while there's more data
  5. read new value
  6. if smaller than smallest set as smallest
  7. else when larger than largest set as largest
  8. wend
  9. close file
  10.  
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 5
Reputation: yvonnemo is an unknown quantity at this point 
Solved Threads: 0
yvonnemo yvonnemo is offline Offline
Newbie Poster

Re: Max and Min values from a group of numbers

 
0
  #3
Jan 29th, 2005
Hi, thanks for helping. Here is what I tried to convert it into but it didn't quite actully work. It always make the first number it reads the max and min.. I donno why

  1. //********************************************************************************************
  2. //
  3. // MaxMinIntegers.cpp
  4. //
  5. // Program that reads int values from a text file named "numbers01.txt"
  6. // and writes out the largest and smallest values found. Numbers in the
  7. // file may be separated by blanks or line breaks.
  8. //
  9. //********************************************************************************************
  10.  
  11. #include <fstream>
  12. #include <iostream>
  13. #include <cstdlib>
  14. using namespace std;
  15.  
  16.  
  17. int main() {
  18. // Declare input stream
  19. ifstream fin;
  20. int min, max, val;
  21. int isFirst = true;
  22. fin.open("numbers01.txt");
  23.  
  24. fin >> isFirst;
  25. min = isFirst;
  26. max = isFirst;
  27.  
  28. while (fin >> val)
  29. {
  30.  
  31. fin >> val;
  32. if (val < min)
  33. val = min;
  34. else
  35. val = max;
  36. }
  37. // --------------------------------
  38. // ----- ENTER YOUR CODE HERE -----
  39. // --------------------------------
  40.  
  41.  
  42. // --------------------------------
  43. // --------- END USER CODE --------
  44. // --------------------------------
  45.  
  46.  
  47. fin.close();
  48.  
  49. cout << "Largest value is " << max << ", smallest value is " << min << "\n";
  50.  
  51. system("PAUSE");
  52. }
Last edited by alc6379; Jan 31st, 2005 at 9:16 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 5
Reputation: yvonnemo is an unknown quantity at this point 
Solved Threads: 0
yvonnemo yvonnemo is offline Offline
Newbie Poster

Re: Max and Min values from a group of numbers

 
0
  #4
Jan 29th, 2005
nm I got it working now. thanks for the help
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC