Need Help with finding the min value in a 2d array

Sorry. The issue is when we enter the values into the array, the highest usually comes out right. But the lowest comes out as zero.

Updated



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <iomanip>

using namespace std;
const int MAXROWS = 10;
const int MAXCOLS = 10;

typedef float PriceType[MAXROWS][MAXCOLS]; 

void getPrices(float [][MAXCOLS], int&, int&); // gets the prices into the array 
void printPrices(float [][MAXCOLS], int, int); // prints data as a table
float findHighest ( float[][MAXCOLS], int, int); // finds highest of all grades 
float findLowest ( float[][MAXCOLS], int, int);

int main()
{
	float highest,
		lowest,
		totalNum;
	int rowsUsed; // holds the number of rows used
	int colsUsed; // holds the number of columns used
	PriceType priceTable; // a 2D array holding the prices
	getPrices(priceTable, rowsUsed, colsUsed); // calls getPrices to fill the array 
	printPrices(priceTable, rowsUsed, colsUsed);// calls printPrices to display array
	//totalNum = colsUsed * rowsUsed;
	highest = findHighest(priceTable, rowsUsed, colsUsed);
	lowest = findLowest(priceTable, rowsUsed, colsUsed);
	cout << "The highest value is : "<< highest<<endl;
	cout << "The lowest value is : "<< lowest<<endl;
	return 0;
}

void getPrices(float table[][MAXCOLS], int& numOfRows, int& numOfCols)
{
	do{
		cout << "Please input the number of rows from 1 to "<< MAXROWS << endl;
		cin >> numOfRows;
	}while(numOfRows<=0);
	do{
		cout << "Please input the number of columns from 1 to "<< MAXCOLS << endl;
		cin >> numOfCols;
	}while(numOfCols<=0);
	for (int row = 0; row < numOfRows; row++)
	{
		for (int col = 0; col < numOfCols; col++)
			cin >> table[row][col];
		// Fill in the code to read and store the next value in the array
	}
}


void printPrices(float table[][MAXCOLS], int numOfRows, int numOfCols)
{
	
	cout << fixed << showpoint << setprecision(2);
	cout << "Here are the results : "<<endl;
	
	for (int row = 0; row < numOfRows; row++)
	{
		for (int col = 0; col < numOfCols; col++)
			cout << table[row][col] << " ";
		cout << endl;
	}
	
}

float findHighest ( float array[][MAXCOLS], int numOfRows, int numOfCols)
{
	int size= numOfRows*numOfCols;
	float high=array[0][0];
	 for(int i=0;i<size;i++)
    {
		for(int j=0; j<size; j++)
		{
			if (high < array[i][j])
			high=array[i][j];
		}
    }
	
return high;
}

float findLowest ( float array[][MAXCOLS], int numOfRows, int numOfCols)
{
	int size= numOfRows*numOfCols;
	float low=array[0][0];
	for(int i=0;i<size;i++)
    {
		for(int j=0; j<size; j++)
		{
			if (low > array[i][j])
			low=array[i][j];
		}
    }
	
return low;
}
Last edited on
You don't actually describe the issue that you observe.

Nevertheless, do you notice a logical difference in these functions:
1
2
void  printPrices ( float table[][MAXCOLS], int numOfRows, int numOfCols );
float findHighest ( float array[][MAXCOLS], int size );
Sorry, updated.
Look at this snippet:
1
2
3
4
5
6
7
8
9
10
11
12
float findHighest ( float array[][MAXCOLS], int numOfRows, int numOfCols)
{
	int size= numOfRows*numOfCols;
	float high=array[0][0];
	 for(int i=0;i<size;i++)
    {
		for(int j=0; j<size; j++)
		{
			if (high < array[i][j])
			high=array[i][j];
		}
    }


Do you realize that you're possibly accessing the array out of bounds? You shouldn't be using size for the condition statements, you should be using numOfRows and numOfCols.

And please don't update your code, post the modified code into a new post instead. Modifying your previous posts makes following the thread hard.


Last edited on
Indeed.

This is what you do in printPrices() to visit every element:
1
2
3
4
5
for (int row = 0; row < numOfRows; row++)
{
  for (int col = 0; col < numOfCols; col++)
    // something
}


This is what you do in find***():
1
2
3
4
5
for (int row = 0; row < numOfRows*numOfCols; row++)
{
  for (int col = 0; col < numOfRows*numOfCols; col++)
    // something
}
Topic archived. No new replies allowed.