# Thread: program to find maximum number in array

1. ## program to find maximum number in array

C program to find maximum number in array
Code:
```#include<stdio.h>
int main (void)
{
int i, max, N;

unsigned int array = {4,9,2,6,5};

max = array ;

for (i = 1; i < N; i++)
{
if (max > array[i])
{
max = array [i];
}
else
{
array [i] = max;
}
}
printf(" Maximum number in array :  %d ",max);
return 0;
}```
When I compile program I found maximum number is 4, that is not true. What's wrong in program 2. So when the program starts, it will think that the largest is 4. You will want to assign a different number to max when the number you are comparing is larger than max.

The else part isn't necessary, and corrupting the array's original numbers.
> array [i] = max;
when this executes it will make array[i] 4, or whatever the current max is.

Also it doesn't look like N has a value, you might want to make it equal the size of your array. 3. Originally Posted by whiteflags So when the program starts, it will think that the largest is 4. You will want to assign a different number to max when the number you are comparing is larger than max.

The else part isn't necessary, and corrupting the array's original numbers.
> array [i] = max;
when this executes it will make array[i] 4, or whatever the current max is.

Also it doesn't look like N has a value, you might want to make it equal the size of your array.
I am assuming that the first number of array is the maximum number. After that I taking loop in which I compare second number of array with maximum variable if maximum is greater then array number then don't update the maximum variable and if maximum number is not greater then array number then update value of maximum variable with array number 4. >I am assuming that the first number of array is the maximum number.
I agree, the first number happens to be 4 though.

>if maximum is greater then array number then don't update the maximum variable
This is where you are messing up: what you are saying and the comparison you actually wrote in the code don't agree. max > array[i] is not true when array[i] is bigger than max. Also, like I said, you don't need an else clause to achieve everything you said. What happens when else's path is executed the current max is assigned to array[i]. I am assuming that you really don't want to do that - that changes the array. 5. Originally Posted by vead
I compare second number of array with maximum variable if maximum is greater then array number then don't update the maximum variable and if maximum number is not greater then array number then update value of maximum variable with array number
That logic is sound, though it is unnecessary to update the maximum when it is equal to the "array number" (i.e., the current element) since you would merely be updating it to the same value as before. However, look at what you're actually doing:
Code:
```if (max > array[i])
{
max = array [i];
}
else
{
array [i] = max;
}```
The above code says "if maximum is greater than the current element then update the maximum variable". That contradicts your statement that "if maximum is greater then array number then don't update the maximum variable". Next, in the else branch, you update the current element with max by assigning max to array[i], but your statement has nothing about updating the current element ("array number") as you only talked about updating the maximum variable. Therefore, your description may be correct, but your code does not match your description. 6. Thank you very much. I find out where I was doing wrong
Code:
```#include<stdio.h>
int main (void)
{
int i, max, N = 5;

unsigned int array = {4,9,2,6,5};

max = array ;

for (i = 1; i < N; i++)
{
if (max < array[i])
{
max = array [i];
}

}
printf(" Maximum number in array :  %d ",max);
return 0;
}```
Maximum number in array : 9 7. A refactored version of the code:

Code:
```#include <stdio.h>
#include <stdlib.h>

unsigned find_max(unsigned array[],int n);

int main (void)
{
// int i, max, N = 5;
int max;
unsigned array[] = {4,9,2,6,5};
int n = sizeof(array)/sizeof(array);

max=find_max(array,n);

printf(" Maximum number in array :  %u \n",max);
return EXIT_SUCCESS;
}

unsigned find_max(unsigned array[],int n)
{
int i;
unsigned max = array ;

for (i = 1; i < n; i++)
{
if (max < array[i])
max = array [i];
}

return max;
}``` 8. Improved code using typedef and size_t:

Code:
```#include <stdio.h>
#include <stdlib.h>

typedef unsigned num_t;

num_t find_max(num_t array[],size_t n);

int main (void)
{
// int i, max, N = 5;
num_t max;
num_t array[] = {4,9,2,6,5};
size_t n = sizeof(array)/sizeof(array);

max=find_max(array,n);

printf(" Maximum number in array :  %u \n",max);
return EXIT_SUCCESS;
}

num_t find_max(num_t array[],size_t n)
{
size_t i;
num_t max = array ;

for (i = 1; i < n; i++)
{
if (max < array[i])
max = array [i];
}

return max;
}``` 9. Using num_t is likely a bad idea. Anything ending in "_t" is likely reserved for either the standard writers or compiler writers.

Tim S. Popular pages Recent additions array, max, maximum, number, program 