| | |
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:
Solved Threads: 0
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
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.
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++):
Here's what to do (I leave it to you to translate that into C++):
C++ Syntax (Toggle Plain Text)
declare variable for smallest value declare variable for largest value set both to first value in the file while there's more data read new value if smaller than smallest set as smallest else when larger than largest set as largest wend close file
•
•
Join Date: Jan 2005
Posts: 5
Reputation:
Solved Threads: 0
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)
//******************************************************************************************** // // 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"); fin >> isFirst; min = isFirst; max = isFirst; while (fin >> val) { fin >> val; if (val < min) val = min; else val = max; } // -------------------------------- // ----- ENTER YOUR CODE HERE ----- // -------------------------------- // -------------------------------- // --------- END USER CODE -------- // -------------------------------- fin.close(); cout << "Largest value is " << max << ", smallest value is " << min << "\n"; system("PAUSE"); }
Last edited by alc6379; Jan 31st, 2005 at 9:16 pm.
![]() |
Similar Threads
- What code to find MAX and MIN? [Problem] (Java)
- Max and Min of more than 2 numbers (Java)
- extract max-min value from every column in array (C)
- Program to compute max and min values from input - HELP! (C)
- arrange values from row of database in php from max to min, from min to max and value (PHP)
Other Threads in the C++ Forum
- Previous Thread: Trying to learn C++
- Next Thread: compile header file
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






