# findmax from imported file

• 03-13-2005
sugie
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.
• 03-13-2005
Lithorien
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?
• 03-13-2005
sugie
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];                 }         } }```
• 03-13-2005
Lithorien
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.