LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-23-2015, 09:49 AM   #1
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311
Blog Entries: 2

Rep: Reputation: 16
Question 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.
 
Old 03-23-2015, 10:14 AM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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.
 
1 members found this post helpful.
Old 03-23-2015, 10:58 PM   #3
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Quote:
Originally Posted by suicidaleggroll View Post
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;
}
 
Old 03-24-2015, 08:02 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,662
Blog Entries: 4

Rep: Reputation: 3942Reputation: 3942Reputation: 3942Reputation: 3942Reputation: 3942Reputation: 3942Reputation: 3942Reputation: 3942Reputation: 3942Reputation: 3942Reputation: 3942
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.
 
1 members found this post helpful.
Old 03-24-2015, 08:58 AM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,782

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by andrew.comly View Post
What's wrong with the "&" in 'scanf("%d",&arr[i]);' ?
The problem isn't the "&", it's the "MAX".
 
1 members found this post helpful.
Old 03-25-2015, 02:19 AM   #6
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Red face Ouch!!

Quote:
Originally Posted by ntubski View Post
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.

Last edited by andrew.comly; 03-25-2015 at 02:23 AM. Reason: incomplete
 
Old 03-25-2015, 04:31 AM   #7
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by andrew.comly View Post
I have already changed it to
Code:
 scanf("d",&arr[element]);
Perhaps you meant
Code:
scanf("%d",&arr[element]);
//     ^
 
1 members found this post helpful.
Old 03-25-2015, 06:38 AM   #8
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Smile I manually typed that part

Quote:
Originally Posted by millgates View Post
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.
 
Old 03-25-2015, 07:43 AM   #9
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Smile 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
 
  


Reply

Tags
array, c programming, condition, loops



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Need to generate an array based on user input soupmagnet Programming 6 02-04-2013 01:22 PM
Array size base on user input ICO Programming 1 10-27-2010 01:40 PM
Pointer to array of integers thelink123 Programming 7 08-24-2009 08:15 PM
Sorting through associative array with user input nobody123 Linux - Newbie 3 04-08-2009 01:55 PM
C++: user input to an int array? andrw123 Programming 12 09-22-2008 11:55 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration