LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   array can't check for user input duplicate integers (https://www.linuxquestions.org/questions/programming-9/array-cant-check-for-user-input-duplicate-integers-4175537607/)

andrew.comly 03-23-2015 09:49 AM

array can't check for user input duplicate integers
 
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 until the user finishes inputting 4 unique integers(no repeats).

Code:

#include<stdio.h>
#include<stdlib.h>

#define MAX 4
int error;


int main()
{
//Declare Variables
        unsigned 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[MAX]);

//Search for duplicates
                        if(element !=0)
                        {
                                for(dup=element-1;dup-- >0;)                /*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
                        /*else if(element=0)
                        {
                                error=0;
                        }        */
                        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])".

Code:

Enter> 1
error=0

Enter> 2
error=0

Enter> 1                                        // *** DUPLICATE with arr[0]  ***
Checked for element=2  dup=0 : No duplicate.            // *** HERE IS THE MISTAKE!! ***
error=0

Another strange thing is how the program always reports j's value back one less than it should be(

Code:

Enter> 3
Checked for element=3  dup=1 : No duplicate.                // *** WHAT HAPPENED TO dup=2 ?? ***
Checked for element=3  dup=0 : No duplicate.
error=0

Please Help.

suicidaleggroll 03-23-2015 10:14 AM

Why are you always reading into &arr[MAX]? Not only is the input not going into the "element"th index in arr where it can later be checked, arr[MAX] is undefined memory as well.

There's also a problem with your for loop
This:
Code:

for(dup=element-1;dup-- >0;)
will decrement dup when it's compared with 0, which is the beginning of the loop, NOT at the end. This is why your loop runs from element-2 to 0 instead of element-1 to 1.

andrew.comly 03-23-2015 10:58 PM

Quote:

Originally Posted by suicidaleggroll (Post 5336402)
Why are you always reading into &arr[MAX]? Not only is the input not going into the "element"th index in arr where it can later be checked, arr[MAX] is undefined memory as well.

What's wrong with the "&" in 'scanf("%d",&arr[i]);' ? There's even a tutorial on http://www.programiz.com/c-programming/c-arrays where they scanf in data in the same way:
Code:

/* C program to find the sum marks of n students using arrays */

#include <stdio.h>

int main()
{
    int marks[10],i,n,sum=0;

        printf("Enter number of students: ");
        scanf("%d",&n);
            for(i=0;i<n;++i)
            {
                printf("Enter marks of student%d: ",i+1);
                scanf("%d",&marks[i]);
                sum+=marks[i];
            }
                printf("Sum= %d",sum);
    return 0;
}


sundialsvcs 03-24-2015 08:02 AM

Bear in mind, especially when using "C," that "there's one way to do it." In this instance, you should consider that there is one right-way to write a For-loop:

for ... ( initial_condition ; test ; increment_or_decrement ) { ...

Don't put "side effect" into the first two clauses. Write exactly what the next programmer who will follow you, expects to see.

I also strongly advise that you start your work with ... a number-two pencil and a legal pad. :eek: I am perfectly serious. I'm taking a short break right now and then it will be back to the pencil-and-pad to continue sketching out an algorithm. All of the pages on that pad are kept. Eventually, the pad itself will go into a banker's box in which I (still) keep them. I have years' worth of now-interesting legal pads in that box. Work out your thoughts, clearly, on paper ... first. If you change your mind, lightly "X"-through the page, flip over to the next one, and try again ... keeping them all.

When you finally achieve clarity as to what it is you're setting out to do, "doing it" is the very-easy part. Whereas, if you try to "ride the pony bareback," yes, you probably will arrive someplace, but you will have at the same time discovered where all of the thorn-bushes are located along the very-circuitous route.

ntubski 03-24-2015 08:58 AM

Quote:

Originally Posted by andrew.comly (Post 5336768)
What's wrong with the "&" in 'scanf("%d",&arr[i]);' ?

The problem isn't the "&", it's the "MAX".

andrew.comly 03-25-2015 02:19 AM

Ouch!!
 
Quote:

Originally Posted by ntubski (Post 5336971)
The problem isn't the "&", it's the "MAX".

My apologies, this really is a horrendous mistake, I simply am so talentless at computer programming. I will have to take naming variables appropriately more seriously.

I have already changed it to
Code:

scanf("d",&arr[element]);
Thank you so much for pointing it out to me.

Unfortunately, the program still makes the same output, but at least now it is one step closer to the final goal.

millgates 03-25-2015 04:31 AM

Quote:

Originally Posted by andrew.comly (Post 5337320)
I have already changed it to
Code:

scanf("d",&arr[element]);

Perhaps you meant
Code:

scanf("%d",&arr[element]);
//    ^


andrew.comly 03-25-2015 06:38 AM

I manually typed that part
 
Quote:

Originally Posted by millgates (Post 5337350)
Perhaps you meant
Code:

scanf("%d",&arr[element]);
//    ^


I manually typed that part, but the code in the program was indeed '%d', and not 'd'. Thanks for your suggestion, I'll have force myself to copy everything over in the future rather than utilizing my quick typing speed.

andrew.comly 03-25-2015 07:43 AM

soln :)
 
I made the following changes:
Code Changed:
1) for(dup=element-1;dup>0;dup--)----------> for(dup=element-1;dup>=0;dup--)
// **** {">" to ">="} *****

2) Moved "error=0;" from below "//Initialize values" to just below do.

3) Changed if(arr[element]==arr[dup]) section's "error=1;" to "error+=1;" and got rid of "error=0;" in following else if section.

Final results (Success at last!)
Code:

Enter> 1
error=0

Enter> 2
Checked for element=1  dup=0 : No duplicate.
error=0

Enter> 3
Checked for element=2  dup=1 : No duplicate.
Checked for element=2  dup=0 : No duplicate.
error=0

Enter> 1
Checked for element=3  dup=2 : No duplicate.
Checked for element=3  dup=1 : No duplicate.
Checked for element=3  dup=0 :  DUPLICATE!!
error=1

Enter> 3
Checked for element=3  dup=2 :  DUPLICATE!!
Checked for element=3  dup=1 : No duplicate.
Checked for element=3  dup=0 : No duplicate.
error=1

Enter> 2
Checked for element=3  dup=2 : No duplicate.
Checked for element=3  dup=1 :  DUPLICATE!!
Checked for element=3  dup=0 : No duplicate.
error=1

Enter> 1
Checked for element=3  dup=2 : No duplicate.
Checked for element=3  dup=1 : No duplicate.
Checked for element=3  dup=0 :  DUPLICATE!!
error=1

Enter> 3
Checked for element=3  dup=2 :  DUPLICATE!!
Checked for element=3  dup=1 : No duplicate.
Checked for element=3  dup=0 : No duplicate.
error=1

Enter> 2
Checked for element=3  dup=2 : No duplicate.
Checked for element=3  dup=1 :  DUPLICATE!!
Checked for element=3  dup=0 : No duplicate.
error=1

Enter> 1
Checked for element=3  dup=2 : No duplicate.
Checked for element=3  dup=1 : No duplicate.
Checked for element=3  dup=0 :  DUPLICATE!!
error=1

Enter> 3
Checked for element=3  dup=2 :  DUPLICATE!!
Checked for element=3  dup=1 : No duplicate.
Checked for element=3  dup=0 : No duplicate.
error=1

Enter> 4
Checked for element=3  dup=2 : No duplicate.
Checked for element=3  dup=1 : No duplicate.
Checked for element=3  dup=0 : No duplicate.
error=0

______________________________________________________
FINAL PROGRAM (Freedom at last!)
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



All times are GMT -5. The time now is 07:41 PM.