Sunday, 10 January 2016

Find Specific Item Location From Linear Array (Algorithm 4.5)


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, ITEM, LOC, i;
    scanf("%d", &N);
    int DATA[N+2];
    for(i=1; i<=N; i++) scanf("%d", &DATA[i]);
    scanf("%d", &ITEM);
    DATA[N+1] = ITEM;
    LOC = 1;
    while(DATA[LOC] != ITEM)
        LOC++;
    if(LOC == N+1) LOC=0;
    printf("\nThe location of this item is on %d.\n", LOC);
    return 0;
}
First input N = array element number.  ex. 7
Second input LA[i] = input N element one by one. ex. 11 22 33 44 55 66 77
Third input ITEM = which I want to Search.  ex. 44
Output ex. The location of this item is on 4.


0 comments:

Post a Comment

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