943,931 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 7254
  • C++ RSS
Jan 29th, 2005
0

Max and Min values from a group of numbers

Expand Post »
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
yvonnemo is offline Offline
5 posts
since Jan 2005
Jan 29th, 2005
0

Re: Max and Min values from a group of numbers

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++):
C++ Syntax (Toggle Plain Text)
  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.  
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Jan 29th, 2005
0

Re: Max and Min values from a group of numbers

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

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
yvonnemo is offline Offline
5 posts
since Jan 2005
Jan 29th, 2005
0

Re: Max and Min values from a group of numbers

nm I got it working now. thanks for the help
Reputation Points: 10
Solved Threads: 0
Newbie Poster
yvonnemo is offline Offline
5 posts
since Jan 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Trying to learn C++
Next Thread in C++ Forum Timeline: compile header file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC