Results 1 to 11 of 11
- 02-22-2009, 04:59 AM #1
Member
- Join Date
- Jan 2009
- Posts
- 90
- Rep Power
- 0
Any suggestions on what i need tp change for a proper output.
I want my program to produce an output like this
for example: This program's array have 10 elements: 65, 50, 79, 58, 83, 91, 65, 98, 79, 79
and outputs
The number of times that 65 appears in the array: 2
Indices that 65 is located: 0 6
The number of times that 79 appears in the array: 3
Indices that 79 is located: 2 8 9
if none matches display "None"
my logics are correct of getting those numbers, im just having trouble to produce a proper output.
i need to change the highlighted codes , but how?
im stuck with this for sometimes and its making me mad!:mad::mad:
Java Code:import java.util.Scanner; public class RedundantElements { public static void main (String[]args) { Scanner input = new Scanner(System.in); System.out.println("Enter an integer for n, ranging 10 and 30."); int inp = input.nextInt(); int [] array = new int[inp]; int [] newArr = assign(array); System.out.println("The random values in an array with size "+array.length+" are:"); displaynum(newArr); displayVaFq(newArr); } //assign random number to n size array public static int[] assign(int [] array) { int [] newArr = new int[array.length]; for (int i =0; i<array.length; i++) { newArr[i] = (int)(Math.random()*51)+50; } return newArr; }//end of assign method //display numbers public static void displaynum(int [] array) { for (int j=0; j< array.length; j++) { if ((j+1)%5==0) System.out.println(array[j]+" "); else System.out.print(array[j]+" "); }//end of for }//end of void method //display value and fequency public static void displayVaFq(int [] array) { int count=0; int loc = 0; for (int k=0; k<array.length; k++) { for (int L=0; L<array.length; L++) { if (array[k] == array[L] && k != L) { count++; loc = L; [COLOR="Green"]System.out.print("Indices that "+array[k]+" is located: "); System.out.println("\n"+loc+" "); System.out.println("The number of times that "+ array[k] + " appears in the array: "+count); }//end of if else System.out.println("No value in the array appear more than once"); }//end of second for[/COLOR] }//end of first for }//end of displayVaFq method }
- 02-22-2009, 05:43 AM #2
1) don't use random # to test your code, use fixed #s first.
2) you are using "count" to count the repeats for all values in the array. you need to keep separate counts for each value. that also applies to "loc" that you used to keep track of the indices.
3) your print statements are in the inner loop, so it prints everytime while counting. you need the count to finish first, then print the final result.USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 02-22-2009, 06:02 AM #3
Member
- Join Date
- Jan 2009
- Posts
- 90
- Rep Power
- 0
ok...i figured it someout and i modified the code and one more problem that in green code i dont want to displayed the last number of the code i want to display all the numbers that satisfied in the loop.
Java Code:import java.util.Scanner; public class RedundantElements { public static void main (String[]args) { Scanner input = new Scanner(System.in); System.out.println("Enter an integer for n, ranging 10 and 30."); int inp = input.nextInt(); int [] array = new int[inp]; int [] newArr = assign(array); System.out.println("The random values in an array with size "+array.length+" are:"); displaynum(newArr); displayVaFq(newArr); } //assign random number to n size array public static int[] assign(int [] array) { int [] newArr = new int[array.length]; for (int i =0; i<array.length; i++) { newArr[i] = (int)(Math.random()*51)+50; } return newArr; }//end of assign method //display numbers public static void displaynum(int [] array) { for (int j=0; j< array.length; j++) { if ((j+1)%5==0) System.out.println(array[j]+" "); else System.out.print(array[j]+" "); }//end of for }//end of void method //display value and fequency public static void displayVaFq(int [] array) { int count=0; int loc = 0; int rep =0; for (int k=0; k<array.length; k++) { for (int L=0; L<array.length; L++) { if (array[k] == array[L] && k != L) { count++; [COLOR="seagreen"]loc = L;[/COLOR] rep = k; }//end of if }//end of second for }//end of first for System.out.print("Indices that "+array[rep]+" is located: "); [COLOR="SeaGreen"]System.out.println("\n"+loc+" ");[/COLOR] System.out.println("The number of times that "+ array[rep] + " appears in the array: "+count); }//end of displayVaFq method }
- 02-22-2009, 06:25 AM #4
- 02-22-2009, 09:26 AM #5
Member
- Join Date
- Jan 2009
- Posts
- 90
- Rep Power
- 0
sorry i didnt check when i posted my second codes.
- 02-22-2009, 09:33 AM #6
Member
- Join Date
- Jan 2009
- Posts
- 90
- Rep Power
- 0
i read #2 and i think thats only applied to count because i only want one number, as for loc (location)i want all the Ls in array[L] that is equals to array[K] or im just not clear of what you saying.2) you are using "count" to count the repeats for all values in the array. you need to keep separate counts for each value. that also applies to "loc" that you used to keep track of the indices.
- 02-22-2009, 09:27 PM #7
if you want to hold more than one value, you will need more than one variable right. you have only 1 count and 1 loc, so you end up w/ only the last loop count and index.
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 02-23-2009, 06:04 AM #8
Member
- Join Date
- Jan 2009
- Posts
- 90
- Rep Power
- 0
yea, but the number of variable that i need is unknown.
- 02-23-2009, 07:08 AM #9
study this example well, because alot of programing involves this kind of work.
Java Code://display value and fequency public static void displayVaFq(int [] array) { int[] mainCount = new int[array.length]; java.util.Arrays.fill(mainCount,1); String[] loc = new String[array.length]; java.util.Arrays.fill(loc,""); for (int k=0; k<array.length; k++) { for (int L=k; L<array.length; L++) { if (array[k] == array[L] && k != L && array[L] !=0) { array[L] = 0; //clear array[L] mainCount[k]++; loc[k] += L + ", "; }//end of if }//end of second for //mainCount[k] = count; }//end of first for for(int i=0; i<array.length; i++){ if(array[i] != 0){ System.out.println("Indices that "+array[i]+" is located: "); System.out.println(i+", "+loc[i]); System.out.println("The number of times it appears in the array: "+mainCount[i]); } } }//end of displayVaFq methodUSE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 02-23-2009, 08:47 AM #10
Member
- Join Date
- Jan 2009
- Posts
- 90
- Rep Power
- 0
Thank you! this is very helpful!
but i haven't learn this yet "java.util.Arrays.fill(mainCount,1);" so i dont know what does it means.Last edited by PureAwesomeness; 02-23-2009 at 09:40 AM.
- 02-24-2009, 12:46 AM #11
that fills the array w/ 1 instead of using a for loop. check out the api doc for more info.
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
Similar Threads
-
any suggestions?
By PureAwesomeness in forum New To JavaReplies: 4Last Post: 01-19-2009, 07:34 AM -
Suggestions for my Java-project?
By shabbee in forum New To JavaReplies: 1Last Post: 01-01-2009, 10:11 PM -
Need help. Method won't returning proper value..
By zlwilly in forum New To JavaReplies: 2Last Post: 12-02-2008, 09:44 PM -
Suggestions of crufty or otherwise bad APIs
By Andre in forum Forum LobbyReplies: 8Last Post: 11-17-2008, 11:06 PM -
Java Script comes with No proper error message in the Alert box in Struts.
By anandguna in forum Advanced JavaReplies: 4Last Post: 07-31-2008, 01:43 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks