Saturday 9 January 2016

Find The Location of First Two Maximum's Value From An Array. (Solved Problem 2.8(a))


Before seeing the solution make sure that you tried enough. Don’t paste the whole code, just find out the logic. If you stuck in trouble, just inform me on comment.

/**Bismillahir Rahmanir Rahim.**/

#include <stdio.h>
int main()
{
    int n, loc1=0, loc2=1,i;
    printf("How Many Element Have on Your Array?\n");
    scanf("%d", &n);
    int data[n];
    printf("Please, input %d Number.\n", n);
    for(i=0; i<n; i++)
        scanf("%d", &data[i]);
    if(data[0]<data[1])
    {
        loc1=1;
        loc2=0;
    }
    for(i=2; i<n; i++)
    {
        if(data[i]>data[loc1])
        {
            loc2=loc1;
            loc1=i;
        }
        else if(data[i]>data[loc2])
            loc2=i;
    }
    printf("Max position on %d.\nSecond Max position on %d.\n", loc1, loc2);
    return 0;
}

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.