finding largest in array

I am trying to find the largest number in an array. Please take a look at the code below. I am confused on how exactly to call the numbers stored in the array. Thank you for any helpe given.

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

const int ROW_MAX = 8;
const int COL_MAX = 10;

typedef int ItemType;

typedef ItemType Chart[ROW_MAX][COL_MAX];

void GetChart(ifstream&, Chart, int&, int&);
// Reads values and stores them in the chart.

void PrintChart(ofstream&, const Chart, int, int);
// Writes values in the chart to a file.

void FindLargest(ofstream&, const Chart, int, int&);
//Finds largest number in chart.

int main ()
{
Chart chart;
int rowsUsed;
int colsUsed;
int largest;
ifstream dataIn;
ofstream dataOut;

dataIn.open("twod.dat");
dataOut.open("twod.out");
GetChart(dataIn, chart, rowsUsed, colsUsed);
PrintChart(dataOut, chart, rowsUsed, colsUsed);
FindLargest(chart, rowsUsed, largest);
return 0;
}

//***************************************************

void GetChart(ifstream& data, Chart chart,
int& rowsUsed, int& colsUsed)
// Pre: rowsUsed and colsUsed are on the first line of
// file data; values are one row per line
// beginning with the second line.
// Post: Values have been read and stored in the chart.

{
ItemType item;

data >> rowsUsed >> colsUsed;

for (int row = 0; row < rowsUsed; row++)
for (int col = 0; col < colsUsed; col++)
data >> chart[row][col];


// FILL IN Code to read and store the next value.
}

//****************************************************

void PrintChart(ofstream& data, const Chart chart,
int rowsUsed, int colsUsed)
// Pre: The chart contains valid data.
// Post: Values in the chart have been sent to a file by row,
// one row per line.
{
for (int row = 0; row < rowsUsed; row++)
{
for (int col = 0; col < colsUsed; col++)
cout << setw(5) << chart[row][col];
cout << endl;
}
}
//****************************************************

void FindLargest(Chart chart[], int rowsUsed, int& largest)
{
int n;
largest = chart[0];

for(n = 1; n < rowsUsed; n++)
{
if(chart[n] > largest)
largest = chart[n];
}
cout << "Largest number from the chart is " << largest << endl;
}
it seems like in FindLargest() you're using a single dimension array when your values were all stores in a 2d array.
That is correct, it is a 2d array. How do I call it in FindLargest() for a 2d?
You can translate between 2d arrays and 1d arrays easily.
int pointon1d = xcoordinate + ycoordinate*lengthoftherows;
Where all the other variables are ints.

The top-left-most element on the array is at [0][0];

-Albatross
Last edited on
So in this case would I do the following. Basicly using the same for statement I used previously?

void FindLargest(Chart chart[], int rowsUsed, int& largest)
{
largest = chart[0][0];

for (int row = 0; row < rowsUsed; row++)
for (int col = 0; col < colsUsed; col++)
{
if(chart[row] > largest)
largest = chart[row];
}
cout << "Largest number from the chart is " << largest << endl;
}
Let me make one more change. Is this correct?

void FindLargest(Chart chart[], int rowsUsed, int& largest)
{
largest = chart[0][0];

for (int row = 0; row < rowsUsed; row++)
for (int col = 0; col < colsUsed; col++)
{
if(chart[row] [col]> largest)
largest = chart[row][col];
}
cout << "Largest number from the chart is " << largest << endl;
}
Eh... no... sorry...

If you are going to use a 2d array, then have your input be a 2d array, so instead of Chart chart[] (which I assume that Chart is not a typedef for char[]) use type chart[][COL_MAX]. As an example.

Also, you need to have colsUsed as an argument to your function. Oops. :)

-Albatross

Last edited on
I got it!!!!!

Couldn't have done it with everyone's help. Thank you so much!!!!

// Program TwoDim manipulates a two-dimensional array
// variable.

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

const int ROW_MAX = 8;
const int COL_MAX = 10;

typedef int ItemType;

typedef ItemType Chart[ROW_MAX][COL_MAX];

void GetChart(ifstream&, Chart, int&, int&);
// Reads values and stores them in the chart.

void PrintChart(ofstream&, const Chart, int, int);
// Writes values in the chart to a file.

void FindLargest(const Chart, int, int, int&);
//Finds largest number in chart.

int main ()
{
Chart chart;
int rowsUsed;
int colsUsed;
int largest;
ifstream dataIn;
ofstream dataOut;

dataIn.open("twod.dat");
dataOut.open("twod.out");
GetChart(dataIn, chart, rowsUsed, colsUsed);
PrintChart(dataOut, chart, rowsUsed, colsUsed);
FindLargest(chart, ROW_MAX, COL_MAX, largest);
return 0;
}

//***************************************************

void GetChart(ifstream& data, Chart chart,
int& rowsUsed, int& colsUsed)
// Pre: rowsUsed and colsUsed are on the first line of
// file data; values are one row per line
// beginning with the second line.
// Post: Values have been read and stored in the chart.

{
/* ItemType item;*/

data >> rowsUsed >> colsUsed;

for (int row = 0; row < rowsUsed; row++)
for (int col = 0; col < colsUsed; col++)
data >> chart[row][col];


}

//****************************************************

void PrintChart(ofstream& data, const Chart chart,
int rowsUsed, int colsUsed)
// Pre: The chart contains valid data.
// Post: Values in the chart have been sent to a file by row,
// one row per line.
{
for (int row = 0; row < rowsUsed; row++)
{
for (int col = 0; col < colsUsed; col++)
cout << setw(5) << chart[row][col];
cout << endl;
}
}
//****************************************************

void FindLargest(const Chart chart, int ROW_MAX, int COL_MAX, int& largest)
{
int col;
int row;
largest = chart[0][0];

for (row = 0; row < ROW_MAX; row++)
for (col = 0; col < COL_MAX; col++)
{
if(chart[row][col] > largest)
largest = chart[row][col];
}
cout << "Largest number from the chart is " << largest << endl;
}
Topic archived. No new replies allowed.