Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unexpected output – Choosing the lowest number from an array

I want to find the smallest number from the numbers in an array entered by the user.
The output I am getting for the program is a vague number -858993460. This is generally because of a type mismatch but they seem alright here. Not able to think of a solution. Thanks for the help!

    
```

    //
    #include<stdio.h>
    int main()
    {
        int a;
        int i;
        int num[25];
        printf("Enter the 25 numbers:");
        for (i = 0; i <= 24; i++)
        {
            scanf("%d", &num[i]);
        }
    
        for (i = 0; i <= 24; i++)
        {
            a = num[i];
            if (a < num[i+1])
            {
                a = a;
            }
            else
                a = num[i+1];
        }
        printf("Lowest number is %d\n", a);
        return 0;
    
    }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Hope this will work for you. Just remove the else statement and bring a=num[i] inside if statement and consider the first element is the minimum element by doing this a = num[0]

#include<stdio.h>
int main()
{
    int a;
    int i;
    int num[25];

    printf("Enter the 25 numbers:");

    for (i = 0; i <= 24; i++)
    {
        scanf("%d", &num[i]);
    }

    a = num[0];
    
    for (i = 0; i <= 24; i++)
    {     
        if (num[i] < a)
        {
             a = num[i]; 
        }
    }
    printf("Lowest number is %d\n", a);
    return 0;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading