Thread: Finding a repeating number in an array?

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    26

    Finding a repeating number in an array?

    Is there a way to find if a number is in an array more than once? How would I go about doing this? Thanks for the help.

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Iterate through the whole array and have a variable that increments for each time it finds that an element equals a certain number.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    26
    Okay, I figured that out. Thanks for the help. I have a new problem though, which should be easy for anyone who isn't me to figure out. I have the user enter his first and last name and favorite number, and then have the program print it out. However, It will not say "Hello, First name Last name!" but rather it will display the first name, then the second name on a new line.

    Code:
        printf( "Please enter your first name:\t " );
        fgets(str, 20, stdin);
        printf("\nPlease enter your last name:\t");
        fgets(lastname, 20, stdin);
        printf("\nWhat is your favorite number?\t");
        scanf("%d", &number);
        
        printf("Hello, %s %s!  I see that your favorite number is %d!\n", str, lastname, number);

    Also, I need to use fgets because I am using pointers for another part.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    If it fits, fgets will also read the \n into the buffer, that's why the last name is being printed on the other line. There are many ways to get rid of the \n. I personally do it like this:
    Code:
    #include <string.h>
    char *p;
    
    if ((p = strchr(str, '\n')))
        *p = '\0';

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    26
    Ah! Thanks a lot!

  6. #6
    Sys.os_type="Unix";;
    Join Date
    Aug 2005
    Posts
    52
    Quote Originally Posted by tcpl
    If it fits, fgets will also read the \n into the buffer, that's why the last name is being printed on the other line. There are many ways to get rid of the \n. I personally do it like this:
    Code:
    #include <string.h>
    char *p;
    
    if ((p = strchr(str, '\n')))
        *p = '\0';
    As well as getting into the habbit of checking that the pointer doesn't equal NULL before trying to utilize it.
    Granted from getting input from the user there should be a new line character but getting into the habbit is good because if you're reading from a user supplied file killing new line characters in that method without checking first can bite you.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That's what the if statement does, makes sure p isn't NULL.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Sys.os_type="Unix";;
    Join Date
    Aug 2005
    Posts
    52
    Quote Originally Posted by dwks
    That's what the if statement does, makes sure p isn't NULL.
    Oops should of looked at the code closer before posting -.- sorry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. finding the largest number in a binary search
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 07-31-2008, 03:19 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Replies: 5
    Last Post: 05-30-2003, 12:46 AM