Saturday 9 January 2016

Find A Specific Item From An Array By Linear Search. (Algorithm 2.4)


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;
    printf("Please, input array element number:\n");
    scanf("%d", &N);
    int DATA[N], ITEM, K, LOC;
    printf("Input %d element:\n", N);
    for(K=0; K<N; K++)
        scanf("%d", &DATA[K]);
    printf("Input Which Item You Want:\n", N);
    scanf("%d",&ITEM);
    LOC = -1; K = 0;
    while(LOC == -1 && K<N)
    {
        if(DATA[K] == ITEM)
            LOC = K;
        K++;
    }
    if(LOC == -1)
        printf("ITEM is not in the array DATA.\n");
    else
        printf("Your ITEM is on location %d.\n", LOC+1);
    return 0;
}  

0 comments:

Post a Comment

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