Thread: Array's if/else Can't reject user input duplicates

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    9

    Unhappy Array's if/else statement Can't reject user input duplicates

    I have written the program below. I would like the user to input 4 unique integers. Whenever the user inputs a duplicate I want them to be prompted with an "error" message, and then the program should repeat asking the user to input non-duplicate integers until the user finishes inputting 4 unique integers(no repeats). Run from ubuntu lxde platform within code::blocks.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    #define MAX 4
    int error;
    
    
    int main()
    {
    //Declare Variables
        int element, dup;
        int arr[MAX];
    
    //Initialize values
        for(element=0;element<MAX;element++)
        {
            arr[element]==0;
        }
        error = 0;
    
    //User Input
        for(element=0;element<MAX;element++)
        {
            do
            {
                printf("Enter> ");
                scanf("%d",&arr[element]);
    
    //Search for duplicates
                if(element !=0)
                {
                    for(dup=element-1;dup>0;dup--)                /*In order to search for repeats, start comparing element to the element just before element (dup=element-1), and repeat this search again until you get to arr[0] (dup-- >0;)  */
                    {
                        if(arr[element]==arr[dup])
                        {
                            puts("Duplicate. ");
                            error=1;
                        }
    
    //For Debugging Purposes Only
                        else if(arr[element] != arr[dup])
                        {
                            printf("Checked for element=%u  dup=%u : No duplicate.\n",element,dup);
                            error=0;
                        }
                    }        //ends for(dup=element-1;dup-->0;)  loop
                }            //ends if(element !=0) loop
                printf("error=%d\n",error);
            }while(error);        //if duplicate detected than go back and ask/scan all over, but without adding 1 to element.
        }                    //ends for(element=0;element<MAX,element++)
    return 0;
    }                        //ends main
    I don't know why but when I enter in duplicates, the program can't identify duplicates. I guess this narrows the problem down to "if(arr[element]==arr[dup])".

    The following are my results, illustrating the program's failure to identify duplicates:

    Code:
    Enter> 1
    error=0
    
    Enter> 2
    error=0
    
    Enter> 3
    Checked for element=2 dup=1 : No duplicate.
    error=0
    
    Enter> 3
    Duplicate.
    Checked for element=3 dup=1 : No duplicate. // *** 1)HERE IS THE MISTAKE!! 2) What happened to dup=2???***
    error=0
    Why do I get the above result "Duplicate" then "no duplicate". This totally defies the rules of if and else!

    Code:
    if(arr[element]==arr[dup])
    {
         puts("Duplicate. ");
         error=1;
    }
    
    //For Debugging Purposes Only
    else if(arr[element] != arr[dup])
    {
         printf("Checked for element=%u dup=%u : No duplicate.\n",element,dup);
         error=0;
    }
    Another strange thing is how the program reports dup's value back one less than it should be:

    Code:
    Enter> 3
    Duplicate.
    Checked for element=3 dup=1 : No duplicate. // *** 1)HERE IS THE MISTAKE!! 2) What happened to dup=2???***
    error=0
    Last edited by andrew.comly; 03-25-2015 at 05:23 AM. Reason: indentation

  2. #2
    Registered User
    Join Date
    Mar 2015
    Posts
    9
    Please, heeeeeeeelllllllllllllpppppppp!!!!???????????????? ?

    ...........
    Last edited by andrew.comly; 03-25-2015 at 05:25 AM.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Your code is inadvertently resetting the "error" flag when it shouldn't be.

    Let's say you enter 1, 2, 3, 3.

    arr[0] = 1
    arr[1] = 2
    arr[2] = 3
    arr[3] = 3

    Now look at your loop:

    Code:
    for(dup=element-1;dup>0;dup--)
    {
        if(arr[element]==arr[dup])
        {
            puts("Duplicate. ");
            error=1;
        }
        
        else if(arr[element] != arr[dup])
        {
            printf("Checked for element=%u  dup=%u : No duplicate.\n",element,dup);
            error=0;
        }
    }
    In the final iteration (where you expect the "error" to occur), "arr[element]" is "arr[3]", and "arr[dup]" is "arr[2]".

    arr[3] = 3, and arr[2] = 3. So you print "duplicate" and set the "error" flag.

    But the loop continues... "arr[element]" goes to "arr[2]", and "arr[dup]" goes to "arr[1]".

    arr[2] = 3, and arr[1] = 2. So now the "else" is executed, the "no duplicate" message is printed, and the "error" flag is cleared.

    So what you need to do is stop looping if and when the "error" flag gets set.



    Code:
    /*
    main.c||In function 'main':|
    main.c|17|warning: statement with no effect|
    ||=== Build finished: 0 errors, 1 warnings ===|
    */
    This means you're not actually setting any of the array elements in your first "for()" loop to zero (i.e. you're using comparison == instead of assignment =).

    If you want to initialize all elements to zero, you can simply do:

    Code:
    int arr[MAX] = { 0 };
    This sets the first element to zero, and the standard states that if not all elements are initialized in the brace-enclosed list, the rest are automatically set to zero.



    Quote Originally Posted by andrew.comly View Post
    Please, heeeeeeeelllllllllllllpppppppp!!!!???????????????? ?

    ...........
    >> Last edited by andrew.comly; 16 Minutes Ago at 07:25 AM.

    I appreciated your original message there, asking what happened to the formatting. I could have told you that maybe you were using a mix of tabs and spaces, which would cause your indentation to look funky. I also could have told you to try "previewing" your code before submitting, to verify it pasted and posts as expected.

    Your edited message is now whiney and annoying.
    Last edited by Matticus; 03-25-2015 at 05:46 AM.

  4. #4
    Registered User
    Join Date
    Mar 2015
    Posts
    9
    THANKS Matticus!

    I made the following changes and now it works perfectly!

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    #define MAX 4
    int error;
    
    
    int main()
    {
    //Declare Variables
        int element, dup;
        int arr[MAX];
    
    //Initialize values
        for(element=0;element<MAX;element++)
        {
            arr[element]==0;
        }
    
    //User Input
        for(element=0;element<MAX;element++)
        {
            do
            {
                error = 0;
                printf("Enter> ");
                scanf("%d",&arr[element]);
    
    //Search for duplicates
                if(element !=0)
                {
                    for(dup=element-1;dup>=0;dup--)                /*In order to search for repeats, start comparing element to the element just before element (dup=element-1), and repeat this search again until you get to arr[0] ("dup-- >0;" OR "dup>=0;")  */
                    {
                        if(arr[element]==arr[dup])
                        {
                            printf("Checked for element=%u  dup=%u :  DUPLICATE!! \n",element,dup);
                            error+=1;
                        }
    //For Debugging Purposes Only
                        else if(arr[element] != arr[dup])
                        {
                            printf("Checked for element=%u  dup=%u : No duplicate.\n",element,dup);
                        }
                    }        //ends for(dup=element-1;dup-->0;)  loop
                }            //ends if(element !=0) loop
                printf("error=%d\n\n",error);
            }while(error);        //if duplicate detected than go back and ask/scan all over, but without adding 1 to element.
        }                    //ends for(element=0;element<MAX,element++)
    return 0;
    }                        //ends main

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're welcome.

    I note you didn't correct the issue I mentioned about line 17.

  6. #6
    Registered User
    Join Date
    Mar 2015
    Posts
    9

    Talking [solved]

    Matticus,
    Thanks again, the closer to KISS(Keep it simple sir) I can get the better!

    Writing
    Code:
       int array[MAX]={0};
    is way easier than:
    Code:
       for(i=0;i<MAX;i++)
         {
              array[i]=0;
         }
    But again, due to the "minimum effort, maximum efficiency" principle I think I'll keep that unneeded initialization out.

    Final program:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    #define MAX 4
    int error;
    
    
    int main()
    {
    //Declare Variables
        int element, dup;
        int arr[MAX];
    
    //User Input
        for(element=0;element<MAX;element++)
        {
            do
            {
                error = 0;
                printf("Enter> ");
                scanf("%d",&arr[element]);
    
    //Search for duplicates
                if(element !=0)
                {
                     for(dup=element-1;dup>=0;dup--)                /*In  order to search for repeats, start comparing element to the element just  before element (dup=element-1), and repeat this search again until you  get to arr[0] ("dup-- >0;" OR "dup>=0;")  */
                    {
                        if(arr[element]==arr[dup])
                        {
                            printf("Checked for element=%u  dup=%u :  DUPLICATE!! \n",element,dup);
                            error+=1;
                        }
    //For Debugging Purposes Only
                        else if(arr[element] != arr[dup])
                        {
                            printf("Checked for element=%u  dup=%u : No duplicate.\n",element,dup);
                        }
                    }        //ends for(dup=element-1;dup-->0;)  loop
                }            //ends if(element !=0) loop
                printf("error=%d\n\n",error);
            }while(error);        //if duplicate detected than go back and ask/scan all over, but without adding 1 to element.
        }                    //ends for(element=0;element<MAX,element++)
    return 0;
    }                        //ends main

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to create an user input for 2D array
    By Idan Damri in forum C Programming
    Replies: 21
    Last Post: 08-24-2014, 04:28 PM
  2. Odd results on user input array
    By deadrabbit in forum C Programming
    Replies: 4
    Last Post: 08-15-2011, 07:59 PM
  3. User input for array size.
    By coolmoniker in forum C++ Programming
    Replies: 27
    Last Post: 08-24-2006, 11:34 PM
  4. array and user input
    By enlinux in forum C Programming
    Replies: 2
    Last Post: 07-20-2003, 02:08 AM
  5. How to let the user input values for an array...
    By TerranAce007 in forum C++ Programming
    Replies: 2
    Last Post: 08-24-2002, 03:54 AM

Tags for this Thread