Got it! This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
The maximum value of a list represented as an array from 0 to n-1 (where n is not zero) is the value in the list that is not smaller than any other value. The position of that value (or values) is not important here.
Note: When there are no elements in the array, the maximum value is usually taken to be either not defined (null, void, etc.) or negative infinity (the most negative value that can be represented for that type).
2. Array literals
Here is an array initialized as an array literal.
The array values has elements from 0 to 3 and is a a shortened form of the following.
Note: A better way is to use a constant, but literals are used here as an example.
3. While loop
Here is an example using a literal array and a while loop.
Here is the C code.
Note: There is no need to check the element at position 0 so the loop starts at position 1.
Here is the output of the C code.
4. For loop
Here is an example using a literal array and a for loop.
Here is the C code.
Here is the output of the C code.
5. Goto statements
Here is the same code using labels and the goto statement. (For advanced classes only; do not use the goto in the CS101 class).
Here is the C code.
Note that, by convention, labels are placed at the left (ignoring indent rules) and if statements that are conditional goto statements are written on one line.
Here is the output of the C code.
6. Constant abstraction
A much better way to write the above program, using constant abstraction and a more consistent naming convention is as follows.