Thread: program to find largest number from given numbers

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    129

    program to find largest number from given numbers

    I wrote c program to find smallest number from given numbers

    Code:
    #include<stdio.h> 
    int main (void)
    {
        int numbers[8] = { 9, 4, 2, 6 , 8, 3, 1} ;
         
        int i = 0;
         
        int smallest ;
         
        smallest = numbers[0];
         
        for (i = 0; i < 8; i++  )
        {
             
             
            if (smallest > numbers[i])
            {
                 
                 smallest = numbers[i];   
            }           
             
        }
         
        printf ("smallest number = %d", smallest);
     
     
          
        return 0;   
    }
    smallest number = 0

    Why program doesn't show smallest number ?
    Last edited by abhi143; 10-13-2019 at 01:00 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That's because the smallest number is indeed 0. Notice that you declared the array as having 8 int values, then initialised it with 7 int literals. Therefore, the remaining element in the array is 0, and since you looped over the entire array, you found that 0 is the smallest number.
    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

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    129
    Quote Originally Posted by laserlight View Post
    That's because the smallest number is indeed 0. Notice that you declared the array as having 8 int values, then initialised it with 7 int literals. Therefore, the remaining element in the array is 0, and since you looped over the entire array, you found that 0 is the smallest number.
    Thanks laserlight, I have another error in following code

    code to find reverse number
    Code:
    #include<stdio.h> 
    int main (void)
    {
        int numbers[7] = { 9, 4, 2, 6 , 8, 3, 1} ;
         
        int i = 0;
         
         
        for (i = 7; i > 0; i--  )
        {
             printf ("%d", numbers[i]);
     
        }
          
        return 0;   
    }
    
    output is 7138624  that is not the reverse number

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What do you think is the problem and what have you tried for debugging, e.g., have you stepped through the loop with a debugger?
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-27-2013, 01:43 PM
  2. Replies: 3
    Last Post: 01-30-2013, 09:30 AM
  3. Program to find largest of 3 numbers
    By stuartbaggs in forum C Programming
    Replies: 9
    Last Post: 10-11-2012, 04:50 AM
  4. Find largest numbers
    By krakatao in forum C Programming
    Replies: 1
    Last Post: 02-04-2012, 09:58 AM
  5. Find largest and second largest number (help)
    By Arkon in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2006, 11:21 PM

Tags for this Thread