#include<stdio.h>
int main()
{
    int i ,j ,a[5];
    for(i=0;i<=4;i++)
    {
        printf("\n\tEnter the %dst value of the array: ", i+1);
        scanf("%d", &a[i]);
    }
    j = a[0];
    printf("\n\tOriginal array entered by the user: ");
    for(i=0;i<=4;i++)
    {
        printf(" %d ", a[i]);
        if(a[i]<a[i+1])
        {
             j = a[i+1];
        }
    }
    printf("\n\n\tGreatest no. from the array: %d", j);
getch();
return 0;   
}

according to me there is no problem in this code.......

but its printing some wierd values again n again.....

plz...help me out here....

My Wierd Output:

Enter the 1st value of the array: 56

Enter the 2st value of the array: 35

Enter the 3st value of the array: 26

Enter the 4st value of the array: 15

Enter the 5st value of the array: 24

Original array entered by the user: 56 35 26 15 24

Greatest no. from the array: 4007048

Your i+1 means you're reading off the end of the array.

Also, finding the maximum is a lot simpler than this.

your value of j= [i+1] reads the value of the a[5] in the last cycle which does not exist at all...

I solved it myself......

the code is:

#include<stdio.h>
int main()
{
    int i ,j ,a[5];
    for(i=0;i<=4;i++)
    {
        printf("\n\tEnter the %dst value of the array: ", i+1);
        scanf("%d", &a[i]);
    }
    j = a[0];
    printf("\n\tOriginal array entered by the user: ");
    for(i=0;i<=3;i++)
    {
        printf(" %d ", a[i]);
        if(a[i]<a[i+1])
        {
             j = a[i+1];
        }
    }
    printf("\n\n\tGreatest no. from the array: %d", j);
getch();
return 0;
}

the loop that was comparing the numbers was looping one extra time so i limited it to 4 times (i<=3) instead of 5(i.e. the array size).....

now it works fine......

and i dont see a better way of doing it......maybe there is a easier way.......

good thing about shotguns is that if you're close enough you can still hit the target even if your aim is lousy.

Change the prog as follows

#include<stdio.h>
#include<conio.h>
int main()
{
    int i ,j ,a[5];
    for(i=0;i<=4;i++)
    {
        printf("\n\tEnter the %dst value of the array: ", i+1);
        scanf("%d", &a[i]);
    }
    j = a[0];
    printf("\n\tOriginal array entered by the user: ");
    for(i=0;i<=4;i++)
    {
        printf(" %d ", a[i]);
	if(j<a[i])
        {
	     j = a[i];
        }
    }
    printf("\n\n\tGreatest no. from the array: %d", j);
getch();
return 0;
}

Your program still contain serious logical error.give data like 120,12,13,45,90 and check with your prog.

I solved it myself......

the code is:

#include<stdio.h>
int main()
{
    int i ,j ,a[5];
    for(i=0;i<=4;i++)
    {
        printf("\n\tEnter the %dst value of the array: ", i+1);
        scanf("%d", &a[i]);
    }
    j = a[0];
    printf("\n\tOriginal array entered by the user: ");
    for(i=0;i<=3;i++)
    {
        printf(" %d ", a[i]);
        if(a[i]<a[i+1])
        {
             j = a[i+1];
        }
    }
    printf("\n\n\tGreatest no. from the array: %d", j);
getch();
return 0;
}

the loop that was comparing the numbers was looping one extra time so i limited it to 4 times (i<=3) instead of 5(i.e. the array size).....

now it works fine......

and i dont see a better way of doing it......maybe there is a easier way.......

since he's happy with his own "solution", i think it's safe to say he's not looking to improve/fix it -- hence, my earlier shotgun metaphor.

theres another metaphor that applies here, it starts with "you can lead a horse to water..."


.

#include<stdio.h>
int main()
{
    int i ,j ,a[5];
    for(i=0;i<=4;i++)
    {
        printf("\n\tEnter the %dst value of the array: ", i+1);
        scanf("%d", &a[i]);
    }
    j = a[0];
    printf("\n\tOriginal array entered by the user: ");
    for(i=0;i<=4;i++)
    {
        printf(" %d ", a[i]);
        if(a[i]<a[i+1])
        {
             j = a[i+1];
        }
    }
    printf("\n\n\tGreatest no. from the array: %d", j);
getch();
return 0;   
}

according to me there is no problem in this code.......

but its printing some wierd values again n again.....

plz...help me out here....

My Wierd Output:

Enter the 1st value of the array: 56

Enter the 2st value of the array: 35

Enter the 3st value of the array: 26

Enter the 4st value of the array: 15

Enter the 5st value of the array: 24

Original array entered by the user: 56 35 26 15 24

Greatest no. from the array: 4007048

Hi...
U were not comparing all the elements. code started with comparing a single element with others. Following code will help u out.

#include<stdio.h>
int main()
{
    int i ,j ,a[5],temp;
    for(i=0;i<=4;i++)
    {
        printf("\n\tEnter the %dst value of the array: ", i+1);
        scanf("%d", &a[i]);
    }
    i=0;
//    printf("\n\tOriginal array entered by the user: ");

    //for(i=0;i<=3;i++)
    //{
//        printf(" %d ", a[i]);
        i=0;
    for(j=1;j<=4;j++)
        {
            if(a[i]>a[j])
                {
                        temp =a[i];
                        a[i]=a[j];
                        a[j]=temp;

                }

                i++;
        }

    printf("\n\n\tGreatest no. from the array: %d\n", a[4]);

return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.