Results 1 to 3 of 3

Thread: Function that accepts an integer/char array

  1. #1
    ThompsonHarris is offline Senior Member
    Join Date
    Dec 2009
    Posts
    255
    Rep Power
    3

    Default Function that accepts an integer/char array

    Hi I am last year MCA student. Currently I am learning C programming language. I want to write a function that accepts an integer/char array and a search item. If the search item is there in the array return position of array and value else return -1.without using together array, without sorting, not to use more than one loop? Give me the perfect solution to all this.

  2. #2
    GonzalezBrown is offline Senior Member
    Join Date
    Dec 2009
    Posts
    260
    Rep Power
    3

    Default

    Code:
    int find_num(int *arr, int *arr_num, int arr_size)
    {
    int i;
    for(i=0;i<arr_size;i++)
    {
     if(*(arr+i) != *arr_num)
      continue;
     else
      return i;
    }
    return -1;
    }

    **)
    I have tried the given code but it didn’t solve my problem. Please try to provide some other solution.

  3. #3
    LewisClark is offline Senior Member
    Join Date
    Dec 2009
    Posts
    260
    Rep Power
    3

    Default

    Code:
    int find_num(int *arr, int *arr_num, int arr_size)
    {
    int i;
    while((i<arr_size) && (*(arr+i) != *arr_num))
      i++;
    if(i >= arr_size)
      return -1;
    else
      return i;
    }

Similar Threads

  1. IP Australia accepts CMS, Web 2.0 tools
    By Colin Wood in forum Latest Hardware News
    Replies: 0
    Last Post: 08-04-2010, 07:55 AM
  2. Produce matrix of char and int
    By EvansMitchell in forum Programming
    Replies: 2
    Last Post: 03-24-2010, 01:53 PM
  3. Comparing char array in c++
    By BrooksGray in forum Programming
    Replies: 2
    Last Post: 02-16-2010, 05:41 PM
  4. length of an integer
    By HernandezOrtiz in forum Programming
    Replies: 2
    Last Post: 01-30-2010, 03:08 PM
  5. Integer
    By techno23 in forum Programming
    Replies: 0
    Last Post: 03-26-2008, 11:49 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
SEO by SubmitEdge

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48