8 Replies - 2349 Views - Last Post: 08 April 2011 - 06:20 PM Rate Topic: -----

#1 TheGreatOne2k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-April 11

Comparing and Adding Values in Arrays

Posted 08 April 2011 - 04:41 PM

Hello everybody! I want to compare values in arrays and if they are similar, I want to multiple them by a number without using hundreds of if and else statements. My code is listed below if you all have any ideas.

import java.util.*;
public class NumberGames
{
    public static void sortArrays(int[]x, int i) //Method used to help sort the array.
    {
        int temp = x[i];
        int j = i - 1;
        while (j >= 0 && temp < x[j])
        {
            x[j + 1] = x[j];
            j--;
        }
        x[j + 1] = temp;
    }
    
    public static void sortInserting(int[]x, int size) //Method used to insert array values.
    {
        for (int i = 1; i < size; i++)
        {
            sortArrays (x, i);
        }
    }  
    
    //My problem is here.  If the values in the array are similar, multiply that number by a certain number.  I have seven different possibilities.  I have comparing
    //two values, but I also have to do, three values, four values, and up to seven values.
//I could use a bunch of if else statements, but my code would be longer than the eye can see.  LOL!

    public static double addThemUp(int[]numbers) //Method used to calculate values.
    {
        double total = 0;
        while (numbers[0] == numbers[1])
        {
            total = numbers[0] * 0.1;
            System.out.println("Total is " + total);
        }
        System.out.println();
        return total;
    }    

    public static void main (String[]args)
    {
        int[]randomNumbers = new int [7]; //Declare seven numbers array.
        for (int i = 0; i < 7; i++)
        {
            randomNumbers[i] = (int)(7 * Math.random() + 1); //Prints random integers from 1 to 20.
        }
        
        sortInserting(randomNumbers, 7); //Method that calls sortInserting method.
        
        System.out.println("Your winning numbers are: ");
        for (int i = 0; i < 7; i++)
        {
            System.out.print(randomNumbers[i] + " "); //Randomly printed numbers.
        }
        System.out.println();
        
        addThemUp(randomNumbers); //Method that calls addThemUp method
    }
} 



Is This A Good Question/Topic? 0
  • +

Replies To: Comparing and Adding Values in Arrays

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1275
  • View blog
  • Posts: 2,839
  • Joined: 05-April 11

Re: Comparing and Adding Values in Arrays

Posted 08 April 2011 - 04:57 PM

I don't quite understand what you are trying to do. What problem does this solve? You have two numbers, and if one of them is smaller than the other, then you multiply the number with 0.1 and add it to the total varible ?

First of all, you can use /* */ to comment multiple lines :).

Second, this will go on in infinity
    while (numbers[0] == numbers[1])
        {
            total = numbers[0] * 0.1;
            System.out.println("Total is " + total);
        }
     System.out.println();
     return total;


This post has been edited by CasiOo: 08 April 2011 - 05:02 PM

Was This Post Helpful? 0
  • +
  • -

#3 TheGreatOne2k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-April 11

Re: Comparing and Adding Values in Arrays

Posted 08 April 2011 - 05:10 PM

Sorry if my question was misunderstood. I declared and instiante a random array, (int)(7 * Math.random() + 1). It will print out 7 values. If 2 of the values are similar in the array. I want to multiple that array by .01. If 3 of the values are similar in the array, I want to multiple them by 0.5. I want to do the same thing if the four of the values are similar and more.

This is what I have so far.
     public static double addThemUp(int[]numbers) //Method used to calculate values.
    {
        double total = 0;
        for (int i = 0; i < 7; i++)
        {  
            for(int j = 0; j < i; j++)//Loop used to bar exact numbers from being used.
            {  
                if(numbers[i] == numbers[j])
                {
                    total = numbers[i] * .01;
                    System.out.println("Total " + total);                            
                    i--;  
                    return total;
                }
            } 
        }
        return total;
    }    


Was This Post Helpful? 0
  • +
  • -

#4 TheGreatOne2k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-April 11

Re: Comparing and Adding Values in Arrays

Posted 08 April 2011 - 05:41 PM

Sorry if my question was misunderstood. I declared and instiante a random array. It will print out 7 values. If 2 of the values are similar in the array. I want to multiple that array by .01. If 3 of the values are similar in the array, I want to multiple them by 0.5. I want to do the same thing if the four of the values are similar and more.

This is what I have so far.

 public static double addThemUp(int[]numbers) //Method used to calculate values.
    {
        double total = 0;
        for (int i = 0; i < 7; i++)
        {  
            for(int j = 0; j < i; j++)//Loop used to bar exact numbers from being used.
            {  
                if(numbers[i] == numbers[j])
                {
                    total = numbers[i] * .01;
                    System.out.println("Total " + total);                            
                    i--;  
                    return total;
                }
            }
        }
        return total;
    }    


This post has been edited by TheGreatOne2k: 08 April 2011 - 05:42 PM

Was This Post Helpful? 0
  • +
  • -

#5 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Comparing and Adding Values in Arrays

Posted 08 April 2011 - 05:58 PM

Use nested loops here. for each value in the array, count how many times it repeats in the array.
have a variable counter to count it.
after going over the array use if statements to check the value of that counter. loop the array again and multiply the values if you have to.
Was This Post Helpful? 1
  • +
  • -

#6 TheGreatOne2k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-April 11

Re: Comparing and Adding Values in Arrays

Posted 08 April 2011 - 06:04 PM

View Postjapanir, on 08 April 2011 - 05:58 PM, said:

Use nested loops here. for each value in the array, count how many times it repeats in the array.
have a variable counter to count it.
after going over the array use if statements to check the value of that counter. loop the array again and multiply the values if you have to.


Thank you. I will give it a try.
Was This Post Helpful? 0
  • +
  • -

#7 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1275
  • View blog
  • Posts: 2,839
  • Joined: 05-April 11

Re: Comparing and Adding Values in Arrays

Posted 08 April 2011 - 06:06 PM

Something like this? What is done is you make an array which goes from 0 to your biggest numbers. Lets say you have a number with the value 5, then you go to your array index 5 and count it up with 1. This you do for each of the numbers in your numbers array.

 int total = 0;
 int countOfNumbers[] = new int[Maximum number];

  
  for ( int i=0; i<numbers.length; i++ )
    countOfNumbers[ numbers[i] ]++;

  for ( int i=0; i<countOfNumbers.length; i++) {
   if ( countOfNumbers[i] == 2
     total += i * 0.01;
   else if ( countOfNumbers[i] > 2 )
     total += i * 0.03;

  }

  return total;



The problem is if you are working with very big numbers, as your helping array with get to be pretty big!

This post has been edited by CasiOo: 08 April 2011 - 06:10 PM

Was This Post Helpful? 1
  • +
  • -

#8 TheGreatOne2k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-April 11

Re: Comparing and Adding Values in Arrays

Posted 08 April 2011 - 06:13 PM

View PostCasiOo, on 08 April 2011 - 06:06 PM, said:

Something like this? What is done is you make an array which goes from 0 to your biggest numbers. Lets say you have a number with the value 5, then you go to your array index 5 and count it up with 1. This you do for each of the numbers in your numbers array.

 int total = 0;
 int countOfNumbers[] = new int[Maximum number];

  
  for ( int i=0; i<numbers.length; i++ )
    countOfNumbers[ numbers[i] ]++;

  for ( int i=0; i<countOfNumbers.length; i++) {
   if ( countOfNumbers[i] == 2
     total += i * 0.01;
   else if ( countOfNumbers[i] > 2 )
     total += i * 0.03;

  }

  return total;



The problem is if you are working with very big numbers, as your helping array with get to be pretty big!


Thank you so much. Arrays and I do not get along. Let me see if I can implement it. Thanks again.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Comparing and Adding Values in Arrays

Posted 08 April 2011 - 06:20 PM

Duplicated threads merged
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1