Last Occurence in Array

This is a discussion on Last Occurence in Array within the C Programming forums, part of the General Programming Boards category; I have to find the last occurence of 'x' in an array of any given length. If the variable 'x' ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    16

    Last Occurence in Array

    I have to find the last occurence of 'x' in an array of any given length. If the variable 'x' is not in the array then -1 should be displayed. This is what ive done so far...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int SearchLastOccurrence(int x, int *p, int len);
    
    int main()
    {
      int * p;
      int i, len, x;
    
      printf("Please enter how many integers are in the array?:");
      scanf("%d", &len);
      printf("Please enter the integer to search among array:\n");
      scanf("%d", &x);
      p = (int*)malloc(len * sizeof(int));
      printf("Enter the numbers now please:\n");
        for(i=0;i<len;i++)
          {
    	scanf("%d", &p[i]);
          }
    printf("The index of the last occurrence is: %d\n", SearchLastOccurrence(x ,p ,len));
    free(p);
    return 0;
    }
    int SearchLastOccurrence(int x, int * p, int len)
    {
      int i, r;
      r = -1;
      for(i = 0 ; i <= len; i++)
       {
         if(x = *(p + i))
           {
           r = i;
           }
       }
     return r;
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,068
    Looks pretty good to me. It works, was there more to the assignment?
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    it worked for you?

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,068
    Yes it did, where were you having problems with it working?

    ...on second though I can't get it to display -1. It will give the last element of the array if it doesn't find it.
    Last edited by SlyMaelstrom; 11-09-2005 at 08:43 PM.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    I can't get the '-1' to display.

  6. #6
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Code:
      p = (int*)malloc(len * sizeof(int));
    Don't cast the return value of malloc in C, and take the size of the dereferenced pointer instead of its type, in case you later decide to change the type.
    Code:
      p = malloc(len * sizeof(*p));
    You have assignment instead of comparison. Also *(p + i) is more readable is p[i].
    Code:
     if(x = *(p + i))
         if(x == p[i])

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,068
    Yeah that should fix it. Didn't catch the single equal sign. It reads -1 now.
    Sent from my iPadŽ

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    thanx for all the help.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    for(i = 0 ; i <= len; i++)
    The usual idiom is this:
    Code:
    for(i = 0 ; i < len; i++)
    This keeps you from looking one past the last element.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21