Thread: HELP! Min Max values using array.

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    2

    HELP! Min Max values using array.

    Hi guys im currently doing an assignment where i am given a .txt file containing an unknown amount of float numbers (there could be thousands).

    I have then got to create functions to calculate the min, max, and average of all the float numbers that could be contained in the file.

    Any ideas?

    Im mainly stuck on the min and max, i have worked out how to do averages but im not so great at using arrays which i believe is needed to be able to work this all out.

    Thanks in advance.

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    an unknown amount of float numbers (there could be thousands).
    This is not a problem at all.. more than likely, just a scare tactic given to you by your teacher. All you have to do is continue to read in entries from your .txt file, until the file can no longer be read:

    Code:
    while(infile >> file_data)
    {
         //determine if the newly read file_data qualifies as a min
         //determine if the newly read file_data qualifies as a max
         // += accumulate the newly read file_data as a step in calculating the average
         //++bump a counter so you'll know how many entries were read from the .txt file.. also a step in calculating the average
    }



    using arrays to calculate a max.. populate an array of floats.. use a nested for loop as a method to compare one element vs. every other element in the array:

    Code:
    float max = 0.0f;
    
    for(int i=0; i<array_size; i++)
         for(int j=0; j<array_size; j++)
         {
              if(array[i] > array[j] && array[i] > max)
    
                   max = array[i];
         }

    Just do the opposite to calcuate a min.
    Last edited by The Brain; 11-28-2005 at 05:17 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    2

    Thanks.

    Cheers for the quick reply, still having difficulty working out what i am doing! Here is what i have done so far, the average works as far as i know, i just dont have a clue how to incorporate the array min and max into it all!

    Code:
    #include <iostream.h>
    #include <fstream.h>
    
    
    void average()
    {
    	ifstream fin;
    
    	float Num, Total;
    	int Count;
    	char ifname[100];
    
    	Count = 0;
    	Total = 0.0;
    
    
    	cout << "Enter input file name :\n";			
    	cin >> ifname;									
    
    	fin.open(ifname, ios::nocreate);
    
    	if (fin.fail())									
    	{
    		cout << "Unable to open input file\n";
    		cout << "PROGRAM TERMINATED\n\n";
    		return;
    	}
    
    	fin >> Num;
    	
    	while(!fin.fail())  
    	{
    		Total = Total + Num;
    		Count++;
    		fin >> Num;
    	}
    
    	if (Count > 0)
    		cout << endl << "Average is " << Total / Count << endl;
    	else
    		cout << endl << "No data in file" << endl;
    
    	
    	fin.close();
    }
    
    
    void main()
    {
    	
    	average();
    	maximum();
    
    }
    So the function for average works fine, the file imported to calculate the min max and average is the same one each time is there an easier way to open it once instead of 3 times (once for each function).

    My attempts at an array failed miserably.

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    In the loop where you add the numbers up, you need to compare each number

    - to the current maximum and adjust if the number from the file is higher

    - to the current minimum and adjust if the number from the file is lower

    You will need two variables in your function to store the current minimum and maximum.

    This way, you will only read the file once.


    You don't need an array at all by the way. If you see a test question stating something as an unspecified number, you will not use an array. It either works without storing every single value ( like this thread ) or you will need another storage container you will learn about later.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    void average()
    {
    	ifstream fin;
    
    	float Num, Total, Max,Min;
    	int Count;
    	char ifname[100];
    
    	Count = 0;
    	Total = 0.0;
    
    
    	cout << "Enter input file name :\n";			
    	cin >> ifname;									
    
    	fin.open(ifname);
    
    	if (fin.fail())									
    	{
    		cout << "Unable to open input file\n";
    		cout << "PROGRAM TERMINATED\n\n";
    		return;
    	}
    
    	fin >> Num;
    	Max = Num;
    	Min = Num;
    	while(!fin.eof())  
    	{
    		Total = Total + Num;
    		Count++;
    		fin >> Num;
    		if(Num > Max) Max = Num;
    		if(Num < Min) Min = Num;
    	}
    	
    
    	if (Count > 0)
    		cout << endl << "Average is " << Total / Count << endl;
    	else
    		cout << endl << "No data in file" << endl;
    	cout << "Maximum value is " << Max << " and the minimum is " << Min << endl;
    	
    	fin.close();
    }
    
    
    int main()
    {
    	
    	average();
    	return 0;
    
    }
    You should't really be using void main! always use int main and return 0 on successful completetion. See above code to find min and max values, it is pretty simple!

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Code:
    while(infile >> file_data)
    Brain: does that really work the way you think? >> operator always returns a reference to the fstream object, so the while test will always be true, making that an infinite loop.
    Code:
    while( !infile.eof() )
    I thik this is what you wanted.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    infile >> file_data would be prefered to !infile.eof(). The reason is that all istreams will go into a fail state by flipping a bit in a state variable when EOF is read. This will cause the conditional to be interpretrted as false, thus stopping the loop. eof() detects the failed state, not EOF, so it won't return false until after the failed state is set. Therefore, when using return value of eof() as the conditional of a loop, the loop body is evaluated one time too many with undefined behaviour resulting. In either case, the stream must be cleared from the failed state in order for it to be reused.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-08-2009, 09:26 PM
  2. Max Min Problem
    By smithc2005 in forum C Programming
    Replies: 7
    Last Post: 10-22-2008, 10:38 AM
  3. How to read in empty values into array from input file
    By wpr101 in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2002, 10:59 PM
  4. Duplicate values in Array
    By TONYMX3 in forum C++ Programming
    Replies: 2
    Last Post: 01-30-2002, 03:57 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM