Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy removing repetition in result

    this is the code of a program which takes upto 20 numbers and puts them in an array. (the sentinel value is -1). then the program calculates if any of the values was repeated and stores that value in another array.in the end it is supposed to print the results in this form
    example for input 1,2,3,3,4:
    Value Count
    1 1
    2 1
    3 2
    4 1

    but the output I get is

    Value Count
    1 1
    2 1
    3 2
    3 2
    4 1


    I need help adding to the code so that if a value has count more then 1 it should be printed only once.

    here is the code :

    **************************************************************************************************** ***************************************

    import java.util.Scanner;

    public class Value
    {
    public static void main(String [] args)

    {

    /* declaring variables */
    int [] userNumbers = new int [20];
    int i=0;
    int m=0;
    int h=0;
    int j,k,z,d;
    int breakNumber = 0;
    int [] count = new int [20];


    /* initializing all elements to -1 */

    userNumbers[0] = -1;
    userNumbers[1] = -1;
    userNumbers[2] = -1;
    userNumbers[3] = -1;
    userNumbers[4] = -1;
    userNumbers[5] = -1;
    userNumbers[6] = -1;
    userNumbers[7] = -1;
    userNumbers[8] = -1;
    userNumbers[9] = -1;
    userNumbers[10] = -1;
    userNumbers[11] = -1;
    userNumbers[12] = -1;
    userNumbers[13] = -1;
    userNumbers[14] = -1;
    userNumbers[15] = -1;
    userNumbers[16] = -1;
    userNumbers[17] = -1;
    userNumbers[18] = -1;
    userNumbers[19] = -1;



    /* inputting values in the array */
    Scanner scan = new Scanner(System.in);

    System.out.println("Enter some numbers ranging in vlaue from 0 to 10 ; upto 20 number separated by space(after your last number enter -1)");




    for(breakNumber=0 ; breakNumber != -1 ;breakNumber = scan.nextInt())

    {


    userNumbers[i]= breakNumber;
    i++;



    }


    /*calculating count of each value */

    z=userNumbers.length;

    for(j=0;j<z;j++)
    {
    for(k=0;k<z;k++)
    {
    if (userNumbers[k]==userNumbers[j])
    {
    count[j] += 1 ;
    }

    }
    }


    /*printing results */


    System.out.println("Value Count \n" );
    for(d=0;d<z;d++)
    {

    if(userNumbers[d]>0 && count[d]<2 )

    {
    System.out.println(userNumbers[d] + " " + count[d]);
    }
    }






    }}

    **************************************************************************************************** *********************

    HELP

  • #2
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts
    sir, i've isolated your problem. you must wrap your code inside tags or you'll get few responses

    first, initialize your variables like this
    Code:
     
    for(int tmp1 = 0; tmp1 < 20; tmp1++)
        userNumbers[tmp1] = -1;
    next
    Code:
     
    int[] uniqueNums = new int[20];
    int[] uniqueCount = new int[20];
    int nUniques = 0;
    for(int zz = 0; ((zz < 20) && (userNumbers[zz] != -1)); zz++){
        int foundat = -1;
        for(int zzz = 0; zzz < nUniques; zzz++)
            foundat = (foundat==-1)?(uniqueNums[zzz] == userNumbers[zz])?zzz:foundat:foundat;
        if(foundat == -1){
            uniqueNums[nUniques] = userNumbers[zz];
            uniqueCount[nUniques] = 1;
            nUniques++;
        }
        else
            uniqueCount[foundat]++;
    }
    the unique numbers (unsorted mind you, make certain you sort both elements in the pair if sorting is necessary) are stored in uniqueNums and the count of each item is stored in uniqueCount.
    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender

  • Users who have thanked mjlorbet for this post:

    ollyb342 (04-18-2008)

  • #3
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks mjlorbet I dont get it right away I ll have to meditate on it a bit lol


  •  

    Posting Permissions

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