answersLogoWhite

0


Best Answer

To calculate the maximum value in an (unsorted) array, assume the first element is the largest element and store its value. If the next value is larger, store that value, otherwise continue to the next value. Repeat this process, updating the stored value each time you find a larger value. After a single pass of the array, the stored value holds the largest value.

Locating the minimum is exactly the same except you're looking for values less than the stored value.

The number of dimensions is immaterial. Simply treat the array as if it were one-dimensional. The following example assumes all arrays or of type int:

int largest (int* a, unsigned len) {

int m = a[0];

for (unsigned i=1; i<len; ++i) if (a[i]>m) m=a[i];

return m;

}

int smallest (int* a, unsigned len) {

int m = a[0];

for (unsigned i=1; i<len; ++i) if (a[i]<m) min=a[i];

return m;

}

Given these functions, we can determine the largest or smallest value in any array of type int, regardless of the number of dimensions:

int main () {

int min, max;

int a[5] = {4, 2, 5, 3, 1};

min = smallest (a, 5); // min = 1

max = largest (a, 5); // max = 5

int b[3][4] = {{1, 2, 3, 4}, {5, 4, 3, 2}, {0, 1, 2, 3}};

min = smallest ((int*) b, 3*4); // min = 0

max = largest ((int*) b, 3*4); // max = 5

return 0;

}

Note that the only real complication is that you must cast all multi-dimensional arrays to one-dimensional arrays. That is, for each dimension greater than 1, you add one level of indirection. E.g., a three-dimensional array of type int would be cast to int** while a four-dimensional array casts to int***.

User Avatar

Wiki User

6y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you find c program the maximum and minimum values from a given two dimension array?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What are extremes in math terms?

The extrema are the maximum and minimum values.The extrema are the maximum and minimum values.The extrema are the maximum and minimum values.The extrema are the maximum and minimum values.


What are the likely maximum and minimum values for this measurement 20.4 0.1cm?

What are the likely maximum and minimum values for this measurement 20.4+_0.1cm


How do you find relative minimum and maximum according to a graph in math?

Any graph should be titled and have maximum and minimum values listed on it. The minimum values are usually on the bottom left and the maximum values are on the top right and bottom right of the graph.


Find the range of the set of numbers?

Find the minimum and maximum values from the given data. Then range is the difference between maximum and minimum values.


How do you determine the range of a function?

Find the maximum and minimum values that the function can take over all the values in the domain for the input. The range is the maximum minus the minimum.


What are the maximum and minimum values of data called?

The extrema.


What is the difference between minimum and maximum numbers?

the rang ( of the rang ) the difference between the maximum and minimum values in a data set.


Maximum and minimum values of quadratic formula?

maximum and minimum are both (-b/2a , c - (b^2/4a))


What did cavendish invent?

The thermometer which captured the minimum and maximum values.


Can standard deviation value be bigger than maximum and minimum value?

No standard deviation can not be bigger than maximum and minimum values.


What is range data?

Data Range is the values of the data from the minimum to the maximum that you are sampling.For plotting purposes(such as in EXCEL spreadsheet), it is the minimum and Maximum range of the values of X-Axis and Y-Axis.


What does data range mean?

Data Range is the values of the data from the minimum to the maximum that you are sampling. For plotting purposes(such as in EXCEL spreadsheet), it is the minimum and Maximum range of the values of X-Axis and Y-Axis.