answersLogoWhite

0

One way to find the maximum of n numbers is to initialize a variable to store the maximum value, then iterate through the n numbers. During each iteration, compare the current number to the stored maximum value, updating the maximum if the current number is greater. After iterating through all n numbers, the variable will contain the maximum value. This algorithm has a time complexity of O(n) as it requires iterating through all n numbers once.

User Avatar

ProfBot

5mo ago

Still curious? Ask our experts.

Chat with our AI personalities

LaoLao
The path is yours to walk; I am only here to hold up a mirror.
Chat with Lao
SteveSteve
Knowledge is a journey, you know? We'll get there.
Chat with Steve
JudyJudy
Simplicity is my specialty.
Chat with Judy
More answers

#include void main()
{
int a[10] = { 4, 55, 65, 73, 87, 99, 45, 454, 4353, 243}; int i, j, k, l; for ( j = 9, i = 1, k = 5, l = 6; i <= 2, k >= 3, l<= 7, j >= 8 ; i++, j--, k--, l++)
{

if (a[0] < a[i])
{
a[0] = a[i];
}
if (a[0] < a[j])
{
a[0] = a[j];
}
if (a[0] < a[k])
{
a[0] = a[k];
}
if (a[0] < a[l])
{
a[0] = a[l];
}
} printf("highest number = %d", a[0] );
}

User Avatar

Wiki User

17y ago
User Avatar

Given a set of n numbers, to find the largest is a simple matter. Iterate through the numbers with a controlled loop structure using a maxValue variable. If the next variable is greater than maxValue, then that variable is the new maxValue. This continues until there are no more variables. Once that occurs, maxValue will have the largest number in the set of n numbers.

User Avatar

Wiki User

10y ago
User Avatar

In Java

// returns largest number in nums; or 0 if nums is empty

int findLargest(int[] nums) {

if(nums.length == 0) {

return 0;

}

int max = nums[0];

for(int i = 1; i < nums.length; ++i) {

if( nums[i] > max ) {

max= nums[i];

}

}

return max;

}

User Avatar

Wiki User

16y ago
User Avatar

You need to save all your numbers in array nums[array_size] of type double (int if you like it more). Then you define function max(nums, array_size) which takes the array and its length as arguments. Thus you have:

double max(nums, array_size)

{

double maximum = nums[0];

for (int i = 0; i < array_size, i++)

{

if (maximum < nums[i])

{

maximum = nums[i];

}

}

return maximum;

}

User Avatar

Wiki User

15y ago
User Avatar

To find the maximum number in an array, assume the first number is the largest and store it. Then compare this stored value with all other values in the array, updating the stored value each time a larger value is found. Once all values have been tested, the stored value is the largest in the array.

User Avatar

Wiki User

9y ago
User Avatar

Add your answer:

Earn +20 pts
Q: Write an algorithm that can find the maximum of n numbers?
Write your answer...
Submit
Still have questions?
magnify glass
imp