Results 1 to 11 of 11
  1. #1
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default 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
    
    }

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    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)

  3. #3
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    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
    
    }

  4. #4
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    read #2 above.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  5. #5
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    sorry i didnt check when i posted my second codes.

  6. #6
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    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.
    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.

  7. #7
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    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)

  8. #8
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    yea, but the number of variable that i need is unknown.

  9. #9
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    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 method
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  10. #10
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    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 10:40 AM.

  11. #11
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    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

  1. any suggestions?
    By PureAwesomeness in forum New To Java
    Replies: 4
    Last Post: 01-19-2009, 08:34 AM
  2. Suggestions for my Java-project?
    By shabbee in forum New To Java
    Replies: 1
    Last Post: 01-01-2009, 11:11 PM
  3. Need help. Method won't returning proper value..
    By zlwilly in forum New To Java
    Replies: 2
    Last Post: 12-02-2008, 10:44 PM
  4. Suggestions of crufty or otherwise bad APIs
    By Andre in forum Forum Lobby
    Replies: 8
    Last Post: 11-18-2008, 12:06 AM
  5. Replies: 4
    Last Post: 07-31-2008, 02:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •