aspose file tools*
The moose likes Beginning Java and the fly likes how to compare the values in arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to compare the values in arrays" Watch "how to compare the values in arrays" New topic
Author

how to compare the values in arrays

Mayur Soneta
Greenhorn

Joined: Jan 04, 2006
Posts: 11
hello guys,

I have a question where i want to find f(a) = f(b) using any pigeon hole principle. I have made the program and working fine by giving me the right results.

Here is my code:

public class array1
{

public static void main(String args[])
{
final int TOTAL = 50;
int x,y;
int[] numbers = new int[TOTAL];

for( x = 0; x < numbers.length; x++)
{

y =( ((x * x * x) + ( 3 * x)) % 50);
// System.out.print("when x = " + x);
numbers[x] = y;
}
int z = 0;
for(int l = 0; l < numbers.length; l++)
{
z = numbers[l];

System.out.println("When x = " + l + " Then Y = " + z);

}



}
}

THe code above gives me the following result:
When x = 0 Then Y = 0
When x = 1 Then Y = 4
When x = 2 Then Y = 14
When x = 3 Then Y = 36
When x = 4 Then Y = 26
When x = 5 Then Y = 40
When x = 6 Then Y = 34
When x = 7 Then Y = 14
When x = 8 Then Y = 36
When x = 9 Then Y = 6
When x = 10 Then Y = 30
When x = 11 Then Y = 14
When x = 12 Then Y = 14
When x = 13 Then Y = 36
When x = 14 Then Y = 36
When x = 15 Then Y = 20
When x = 16 Then Y = 44
When x = 17 Then Y = 14
When x = 18 Then Y = 36
When x = 19 Then Y = 16
When x = 20 Then Y = 10
When x = 21 Then Y = 24
When x = 22 Then Y = 14
When x = 23 Then Y = 36
When x = 24 Then Y = 46
When x = 25 Then Y = 0
When x = 26 Then Y = 4
When x = 27 Then Y = 14
When x = 28 Then Y = 36
When x = 29 Then Y = 26
When x = 30 Then Y = 40
When x = 31 Then Y = 34
When x = 32 Then Y = 14
When x = 33 Then Y = 36
When x = 34 Then Y = 6
When x = 35 Then Y = 30
When x = 36 Then Y = 14
When x = 37 Then Y = 14
When x = 38 Then Y = 36
When x = 39 Then Y = 36
When x = 40 Then Y = 20
When x = 41 Then Y = 44
When x = 42 Then Y = 14
When x = 43 Then Y = 36
When x = 44 Then Y = 16
When x = 45 Then Y = 10
When x = 46 Then Y = 24
When x = 47 Then Y = 14
When x = 48 Then Y = 36
When x = 49 Then Y = 46

So the output is good. Now, I have to find when two Y have the same value..
so when x = 2 and Y = 14, and when x = 7, Y = 14. So in this way I have to find all the duplicate values of Y and print the their following x (index)

Guys I am almost done with my project. Could please tell me or write the code for me how to do it.

waiitng for your reply..

Mayur Soneta
umut uzumcu
Ranch Hand

Joined: Jan 10, 2007
Posts: 43


is that the solution you were looking for? Thats what i could do check it out.
[ January 30, 2007: Message edited by: Fred Rosenberger ]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10909
    
  12

umut,

We try to focus here on helping people learn how to do things themselves, rather than just handing them the answer. Please don't just post the solution. In the long run, you're helping nobody.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Mayur Soneta
Greenhorn

Joined: Jan 04, 2006
Posts: 11
I still didn't understand.. what I trying to do is
I want to count the all the duplicates Y corresponding to their X's (index value)

Can you please helpp me
Mayur Soneta
Greenhorn

Joined: Jan 04, 2006
Posts: 11
Guys give me some hint...or code or ideas...
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

Originally posted by Mayur Soneta:
Guys give me some hint...or code or ideas...


Are you familiar with associative arrays?

If you are not looking for anything space-wise efficient, could you not create an array of 50 elements -- one for each possible value of Y? Each element of this array a sequence of X values that yield Y (the element's index in this array).

How would you go about solving this problem? Think out aloud and the gurus here will point you in the right direction.


"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
I want to count the all the duplicates Y corresponding to their X's (index value)


Where is the y value coming from? Is it a hard-coded value? The first value with a duplicate? All values that have a duplicate?


What do you want to do with the result once you calculate it? Print it to the console? Add it to an array?

Do you know about the Collections framework?


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Dan Walin
Ranch Hand

Joined: Nov 11, 2003
Posts: 109
I would use a Hashtable. If done properly, you'll have a string of x's for every y in the Hashtable. You, would then be interested in only the y's with multiple x values (example: x = 2 5 7) and not those with single values (x = 3).
[ January 30, 2007: Message edited by: Dan Walin ]
Mayur Soneta
Greenhorn

Joined: Jan 04, 2006
Posts: 11
I would like to print

When x = 2 Then Y = 14
When x = 11 Then Y = 14
When x = 12 Then Y = 14
........

So I would like to print all the same Values of Y with Their Index values of X. Please guys let me know I have to submit this my tomorrow.. I am almost done, but need some hintt how to do this..
Mayur Soneta
Greenhorn

Joined: Jan 04, 2006
Posts: 11
Guys I am online and working on my project,please help me out
Mayur Soneta
Greenhorn

Joined: Jan 04, 2006
Posts: 11
When x = 2 Then Y = 14
When x = 7 Then Y = 14
When x = 11 Then Y = 14
When x = 12 Then Y = 14
When x = 17 Then Y = 14
When x = 22 Then Y = 14
When x = 27 Then Y = 14
When x = 32 Then Y = 14
When x = 36 Then Y = 14
When x = 37 Then Y = 14
When x = 42 Then Y = 14
When x = 47 Then Y = 14
==========================
When x = 3 Then Y = 36
When x = 8 Then Y = 36
When x = 13 Then Y = 36
When x = 14 Then Y = 36
When x = 18 Then Y = 36
When x = 23 Then Y = 36
When x = 28 Then Y = 36
When x = 33 Then Y = 36
When x = 38 Then Y = 36
When x = 39 Then Y = 36
When x = 43 Then Y = 36
When x = 48 Then Y = 36

I want to get the results in this way, I'm getting now.. but only when I am hard coding.. below is my code:

/**
* @(#)array1.java
*
*
* @author
* @version 1.00 2007/1/26
*/


public class array1
{

public static void main(String args[])
{
final int TOTAL = 50;
int x,y;
int[] numbers = new int[TOTAL];

for( x = 0; x < numbers.length; x++)
{

y =( ((x * x * x) + ( 3 * x)) % 50);
// System.out.print("when x = " + x);
numbers[x] = y;
}
int z = 0;
for(int l = 0; l < numbers.length; l++)
{
z = numbers[l];
if ( numbers[l] == 14)
System.out.println("When x = " + l + " Then Y = " + numbers[l]);
}
System.out.println("==========================");
for(int l = 0; l < numbers.length; l++)
{
z = numbers[l];
if ( numbers[l] == 36)
System.out.println("When x = " + l + " Then Y = " + numbers[l]);
}
} // End of main

}// End of Class

I dont want to hard code.. in the for loop as you can seee.

Guys give me some hint.. how do I print the results.. with comparing the numbers in array..
Dan Walin
Ranch Hand

Joined: Nov 11, 2003
Posts: 109
Here's a rough sample that may get you to the solution. Each x, y pair is put into a Hashtable with "y" as the key. If there is already a y in the Hashtable, then the new x is appended to the existing value. When you're all finished creating the Hashtable, all the entries with more than one value for "x" would be what you want. At least this would be my approach. Run this test and see if it gives you a good idea of how you can use a Hashtable.

[ January 30, 2007: Message edited by: Dan Walin ]
Mayur Soneta
Greenhorn

Joined: Jan 04, 2006
Posts: 11
Thank you for sharing, but the code is not working.. I got many errors. in that.
.. Wouldn't there would be any easist way to do this...

I am still working .. on ...
Don Sartain
Greenhorn

Joined: Dec 24, 2006
Posts: 11
while I haven't looked at your code in detail, why not just do an if statement somewhere that is like:



Note: I'm new to java, so the syntax may be wrong, but I've had to do similar things in C++ and something like that worked...a bit crude, but it worked...
[ January 30, 2007: Message edited by: Don Sartain ]

Don<br /> <br />"An unexamined life is not worth living." -- Socrates
Mayur Soneta
Greenhorn

Joined: Jan 04, 2006
Posts: 11
Don. Thank you for your reply.. I already did this ... but the problem is I dont want to hard code as "14" ... If I am hard coding that I'm done with my problem.... I DONT WANT TO HARD CODE..

PLEASE GIVE THE SOLUTION....
Nathan Leniz
Ranch Hand

Joined: Nov 26, 2006
Posts: 132
You could try a double iteration through like you have, but nest the iterations. Use a temporary variable to hold a value, compare it with the next loop, and if they match you're in business. Then to prevent duplicates you could set the value you are currently working with to something that would be impossible to be in your array, and make sure when you're testing equality to also check it against that particular impossible value.

Sorry if I'm not explaining myself well, but I hope it helps.

Nate

If it is just the solution you want there are sites like Rent A Coder that may be of more help. I think the whole spirit of the ranch is to foster learning through trial and a bit of coaching, or to answer specific pinpointed questions regarding the language and its constructs. I know I've been close to volunteering for Euthanasia a few times on some of the projects I was working on, but with a bit of coaching and experimenting myself I'd like to think I learned and actually absorbed valuable lessons in the process.
[ January 30, 2007: Message edited by: Nathan Leniz ]

The very existence of flamethrowers proves that at some time, some where, some place, someone once said to themselves "I'd really like to set those people on fire over there, but I just can't get close enough".
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10909
    
  12

Originally posted by Mayur Soneta:
PLEASE GIVE THE SOLUTION....

We don't do that here. We will help you learn, we will answer questions, we will give you advice. But we don't give solutions to problems. A lot of times people are asking for homework solutions, which is generally a violation of a school's code of ethics. We don't wish to be involved in that.

Note that Dan didn't say the code would work, he said it was a hint to help you figure it out. Look at his code, see what he is doing, then work it into YOUR code. His example is not a complete program.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to compare the values in arrays
 
Similar Threads
Convert Into Java Code.
Prime numbers
Magic Square water retention
Project Euler problems with Scala
Trees in Java