Page 2 of 2 FirstFirst 12
Results 11 to 17 of 17

Thread: Your insights needed!

  1. #11
    Join Date
    Oct 2005
    Posts
    745
    Rep Power
    0

    Default

    1. You should always return a value from dup_check. Return check instead of assigning it to the array since you'll assign it after exiting the function.
    2. Also, you didn't assign the value returned from dup_check.
    3. In the dup_check you only need to go as far as you've reached in the array so you might want to pass the current index to dup_check
    4. As it is they could enter the same value or another duplicate value as newone
    Last edited by recursion; Mar 20, 2011 at 01:42 PM. Reason: Added information
    3.14159265358979323846264338327950288
    4197169399375105820974944592307816406
    28620899862803482534211706798 pi 101

  2. #12
    Join Date
    Mar 2006
    Posts
    325
    Rep Power
    0

    Default

    theres a lot wrong with your code.

    "continue;" will skip a space/slot in the array
    the newone variable inside the dup_check() is not seen by main()
    you save a value to the variable called "check" and didnt use the variable or send it anywhere.
    the return statement in dup_check() returns a value to nothing.
    1337

  3. #13
    Join Date
    Jun 2008
    Posts
    74
    Rep Power
    0

    Default

    Code:
    #include <stdio.h>
     
    #define ARR_SIZE 20
     
    int in_array(int[], int, int);
    int is_int(int);
     
    int main()
    {
        int i = 0, num, numbers[ARR_SIZE];
     
        // populating the array
        for(i = 0; i < ARR_SIZE; i++)
        {
            if(i > 0)
            {
                printf("%i number(s) entered so far.", i);
            }else{
                printf("No numbers entered so far.");
            }
     
            printf("\nEnter an integer: ");
     
            scanf("%i", &num);
     
            fflush(stdin);
     
            printf("\n");
     
            /**
             * If the number entered is already in the array, inform the idiot and ask them to enter a different number
             * Also ensure that the idiot entered a number so your program doesn't crash
             */
            while(in_array(numbers, num, i) == 1 || is_int(num) == 0)
            {
                if(is_int(num) == 0)
                {
                    printf("Please refer to http://en.wikipedia.org/wiki/Integer.\n");
                    printf("If you have read the article and understand what an integer is;\nPlease enter one now: ");
                }else{
                    printf("%i is already in the array, enter another number: ");
                }
     
                scanf("%i", &num);
     
                fflush(stdin);
     
                printf("\n");
            }
     
            numbers[i] = num;
        }
     
        /*printing a comma separated list of all integers in the array*/
        for(i = 0; i < ARR_SIZE; i++)
        {
            printf("%i", numbers[i]);
     
            if(i != (ARR_SIZE-1))
            {
                printf(", ");
            }
        }
     
        return 0;
    }
     
    /**
     * Checks if an integer is in an array of integers.
     * @param int numbers[] the array of integers.
     * @param int num the number that is being checked for in the array.
     * @param int limit the highest index in the array to check.
     */
    int in_array(int numbers[], int num, int limit)
    {
        int found = 0;
        int i;
     
        for(i = 0; i <= limit; i++)
        {
            if(numbers[i] == num)
            {
                found = 1;
            }
        }
     
        return found;
    }
     
    int is_int(int num)
    {
        /*This is just a dummy check. You will need a more thorough check as this won't work as expected.*/
        if(sizeof(num) == sizeof(int))
        {
            return 1;
        }else{
            return 0;
        }
    }
    Last edited by jayrulez; Mar 20, 2011 at 02:21 PM.

  4. #14
    Join Date
    Jul 2002
    Posts
    1,395
    Rep Power
    0

    Default

    Working on something else presently. Trying to do about 4 things at once. Going to take the whole day. About fflush(stdin): is it ok to use it?
    Take a look at this.

  5. #15
    Join Date
    Jun 2008
    Posts
    74
    Rep Power
    0

    Default

    Quote Originally Posted by carey View Post
    Working on something else presently. Trying to do about 4 things at once. Going to take the whole day. About fflush(stdin): is it ok to use it?
    Take a look at this.
    According to the article that you referenced, it is not okay. I was not aware of that. You can may implement their suggestion. Apart from that, everything else should be alright.

  6. #16
    Join Date
    May 2003
    Posts
    229
    Rep Power
    0

    Default

    Quote Originally Posted by wiel View Post
    theres a lot wrong with your code.

    "continue;" will skip a space/slot in the array
    Not in this case. Execution will just jump to the top of the while loop and test the condition again.

  7. #17
    Join Date
    Jul 2002
    Posts
    1,395
    Rep Power
    0

    Default

    Major snag: Internet@home disconnected. Trying to go Digi now.

Tags for this Thread

Posting Permissions

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