User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 426,022 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,654 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 3680 | Replies: 3 | Solved
Reply
Join Date: Jan 2005
Posts: 5
Reputation: yvonnemo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
yvonnemo yvonnemo is offline Offline
Newbie Poster

Max and Min values from a group of numbers

  #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 8:16 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 199
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Max and Min values from a group of numbers

  #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++):
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
Reply With Quote  
Join Date: Jan 2005
Posts: 5
Reputation: yvonnemo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
yvonnemo yvonnemo is offline Offline
Newbie Poster

Re: Max and Min values from a group of numbers

  #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

//********************************************************************************************
// 
// 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 8:16 pm.
Reply With Quote  
Join Date: Jan 2005
Posts: 5
Reputation: yvonnemo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
yvonnemo yvonnemo is offline Offline
Newbie Poster

Re: Max and Min values from a group of numbers

  #4  
Jan 29th, 2005
nm I got it working now. thanks for the help
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 1:39 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC