findmax from imported file

This is a discussion on findmax from imported file within the C++ Programming forums, part of the General Programming Boards category; can someone show me some code for a findmax function? I am going to be importing a file that could ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    17

    findmax from imported file

    can someone show me some code for a findmax function?

    I am going to be importing a file that could have up to 10 rows in it. we'll say rate is one of the columns in that file and i want to find the max number in that column.

    I am using a rate[i] array.

  2. #2
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    If you're using an array and you want to know the max #, why not step through with a for loop?

    Code:
    int y = 0;
    
    	for (int x = 1; x < MAX_I_VAL; x++)
    	{
    	    if ((rate[x] > rate[x-1]) && (rate[x] > y))
    	    {
    	        y = rate[x];
    	    }
    	
    	    else if ((rate[x-1] > rate[x]) && (rate[x-1] > y))
    	    {
    	        y = rate[x-1];
    	    }
    	}
    
    std::cout << "The highest value in the array is " << y;
    Or something similar to that?

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    17

    ok, here's my code....

    I think i'm calling it wrong too. it's at the bottom.

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    using namespace std;
    
    void ProcessPayroll(string [], int[], float[], float&, char&, float&, ifstream&, ofstream&);
    void findMax(int, int);
    int main()
    {
    	ifstream infile;  //opening infile
    	infile.open ("lab1.txt");
    	if(infile.fail())
    	{
    		cout << "failing to open input file\n";  //check to see if infile is open
    		getchar();
    		exit(1);
    	}
    
    	ofstream outfile;  //opening outfile
    	outfile.open ("out1.txt");
    	if(outfile.fail())
    	{
    		cout << "failing to open output file\n";  //check to see if outfile is open
    		getchar();
    		exit(1);
    	}
    	
    	
    	const int i=15;
    	int hours[i];
    	float rate[i], gross, adjgross;
    	string name[i];
    	char bonus;
    
    	ProcessPayroll(name, hours, rate, gross, bonus, adjgross, infile, outfile);  //calling ProcessPayroll function
    	getchar();
    	return 0;
    }
    
    void ProcessPayroll(string name[], int hours[], float rate[], float& gross, char& bonus, float& adjgross, ifstream& infile, ofstream& outfile)
    //processes payroll
    {
    	int j, i=0, count=0, y;
    
    	cout << "December 1997 Payroll\n\n" << "           Name" << "  " << "Hours" << "   " << "Rate" << "   " << "Gross" << "  " 
    		 << "Bonus" << "  " << "Adjusted-Gross\n\n";  //shows headers
    	outfile << "Name" << "  " << "Hours" << "  " << "Rate" << "   " << "Gross" << "  " 
    		 << "Bonus" << "  " << "Adjusted-Gross\n\n";  //shows headers in outfile
    
    
    	while(infile >> name[i] >> rate[i] >> hours[i])
         	    i++;
    	for(j=0; j<i; j++)
    	{
    		if(hours[j] < 30)
    		
                gross=(rate[j]-.25)*hours[j];  //calc gross pay
    		else
    			gross=rate[j]*hours[j];
    
    		if(hours[j] < 30)
    
    			rate[j]=(rate[j]-.25);
    		else
    			rate[j]=rate[j];
    		
    
    		if(hours[j] > 45)  //calc bonus code
    		
    			bonus = 'Y';
    		else
    		    bonus = 'N';
    		
    		
    		if(hours[j] > 45)  //calc adjusted gross
    		
    			adjgross = gross + 50;
    		else
    			adjgross = gross;
    
    		count = i ;
    	
    	
    	cout << setw(15) << name[j] << "     "
    		 << setw(2) << hours[j] << "  "
    		 << setw(5) << setiosflags(ios::fixed) << setprecision(2) << rate[j] << "  " 
    	     << setw(7) << setiosflags(ios::fixed) << setprecision(2) << gross << "    " 
    	     << setw(2) << bonus << "         "
    		 << setw(7) << setiosflags(ios::fixed) << setprecision(2) << adjgross << "\n";
    	}
    
    	cout << "\nNumber of Employees\n\n" << count << "\n\n"
    		 << "Maximum Pay Rate\n\n" << findMax;
    
    
    	getchar ();
    	outfile.close();
    	return;
    }
    
    void findMax(int j, int y)
    {
    	int y=0;
    	for(int j=1; j<Max_I_Val; j++)
    	{
    		if((rate[j] > rate[j-1]) && (rate[j] > y))
    		{
    			y=rate[j];
    		}
    		else if((rate[j-1] > rate[j] && (rate[j-1] > y))
    		{
    			y=rate[j-1];
    		}
    	}
    }

  4. #4
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    1.) Why are you passing it a y value? It's made to return(y) so you can do statements such as 'std::cout << findMax(Your_Array)' and not have to worry about giving it a y value to modify.

    Code:
    cout << "\nNumber of Employees\n\n" << count << "\n\n" << "Maximum Pay Rate\n\n" << findMax;
    For example, that should be..

    Code:
    cout << "\nNumber of Employees\n\n" << count << "\n\n" << "Maximum Pay Rate\n\n" << findMax(Your_Array);
    Also, where is Max_I_Val #defined (or const defined)?



    In the function, declare it as findMax(char* Your_Array) <drop the y>, and return(y) at the end, for starters.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21