Thread: program to find maximum number in array

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    99

    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[5] = {4,9,2,6,5};
     
     max = array [0];
     
     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. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by whiteflags View Post
    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. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >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.
    Last edited by whiteflags; 12-18-2017 at 02:19 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    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[5] = {4,9,2,6,5};
     
     max = array [0];
     
     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. #7
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    138
    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[0]);
     
        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 [0];
     
        for (i = 1; i < n; i++)
        {
            if (max < array[i])
                max = array [i];
        }
      
      return max;
     }

  8. #8
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    138
    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[0]);
     
        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 [0];
     
        for (i = 1; i < n; i++)
        {
            if (max < array[i])
                max = array [i];
        }
      
        return max;
    }

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    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.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-30-2015, 09:15 AM
  2. Problem with function to find the maximum of an array
    By Nhân Trần in forum C Programming
    Replies: 8
    Last Post: 07-14-2015, 11:31 PM
  3. Find the maximum number and its position in String
    By duongducthieniu in forum C Programming
    Replies: 4
    Last Post: 12-21-2012, 08:53 PM
  4. Best way to find maximum number
    By mutombo in forum C Programming
    Replies: 3
    Last Post: 02-27-2009, 04:36 AM
  5. Find maximum number (while loop)
    By katherene in forum C++ Programming
    Replies: 1
    Last Post: 10-16-2006, 05:44 PM

Tags for this Thread