[SOLVED] array can't check for user input duplicate integers
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
Last edited by andrew.comly; 03-23-2015 at 09:51 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.
Last edited by suicidaleggroll; 03-23-2015 at 10:17 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.
/* 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;
}
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. 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.
Last edited by sundialsvcs; 03-24-2015 at 08:08 AM.
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.
Last edited by andrew.comly; 03-25-2015 at 02:23 AM.
Reason: incomplete
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.