8 Replies - 418 Views - Last Post: 08 March 2010 - 04:42 PM Rate Topic: -----

#1 Shutterbug84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-February 10

Averaging in Java

Posted 08 March 2010 - 12:06 AM

So here is my problem:

you are given an array of scores. if the average of those scores are less than a 6.0, then you continue to add 8 to the average until it becomes 6.

here is mycode so far:

public int averageThat(int[] scores) 
	{
		int summ=0;
		int count=0;
		int yohowmany = scores.length;
				
		for (int i : scores)
		{
			summ= summ + scores [i];
		}
		
		double average = summ / yohowmany;
		
		if (average >= 6.0)
		{
			return count;
		}
		
		
		while (average < 6.0)
		{
			average= average +8;
			
			average= average / (yohowmany + 1);
			
			count ++;
		}
		
		return count;
		
	}
}





and for some reason, my counter isn't giving me the correct answers. help?

This post has been edited by Shutterbug84: 08 March 2010 - 12:07 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Averaging in Java

#2 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 557
  • Joined: 19-February 10

Re: Averaging in Java

Posted 08 March 2010 - 12:58 AM

First, I am getting an error when at your foreach loop. Remember, this:
for(int i = 0; i  < scores.length; i++)
{
    summ += scores[i];
}



Is the same things as:
for(int i : scores)
{
    summ += i;
}



You also used the average in your while loop, added to it, and then took the new average. You need to add to the sum and then take the average. Also, you wanted to find the average when it was 6, so that's how I set up the while loop instead of the way you had it.

This post has been edited by zim1985: 08 March 2010 - 01:31 AM

Was This Post Helpful? 0
  • +
  • -

#3 Shutterbug84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-February 10

Re: Averaging in Java

Posted 08 March 2010 - 01:07 AM

Yeah, I'm counting the number of 8's i have to add.
Was This Post Helpful? 0
  • +
  • -

#4 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 557
  • Joined: 19-February 10

Re: Averaging in Java

Posted 08 March 2010 - 01:31 AM

You should do something more like this:

Like this:
    int sum = 0;
    int count = 0;
    double average = 0;
    
    public int averageThat(int[] scores)
    {
        int len = scores.length;
        
        for(int i : scores)
        {
            sum += i;
        }
        //System.out.println("Sum: " + sum);
        
        average = sum / len;
        //System.out.println("Average: " + average);
        
        if(average >= 6.0)
        {
            return count;
        }
        
        while(average != 6.0)
        {
            sum += 8;
            //System.out.println("Sum: " + sum);
            average = summ / (len + 1);
            //System.out.println("Average: " + average);
            count++;
        }
        return count;
    }



I added output statements so I could see what was going on. This is a very good practice if you can't tell what is wrong with your program. Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#5 Shutterbug84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-February 10

Re: Averaging in Java

Posted 08 March 2010 - 11:29 AM

Ok, I changed the code this morning- and for some reason, my counter is only returning a value of 1 whenever I test it. What else am I doing wrong?

Thank you!
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10185
  • View blog
  • Posts: 37,603
  • Joined: 27-December 08

Re: Averaging in Java

Posted 08 March 2010 - 11:30 AM

Can you post your revised code?
Was This Post Helpful? 0
  • +
  • -

#7 Shutterbug84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-February 10

Re: Averaging in Java

Posted 08 March 2010 - 11:39 AM

Sure thing!

public int averageThat(int[] scores) 
	{
		// sum the values in scores
			int summation = 0;
			double average = 0;
			int count = 0;
			int denominator = scores.length;
			
		for (int q : scores)
		{
			summation += scores [q];
		}
		
		//find the average
		
		average = summation / scores.length;
		
		// check to see if value of average is 6.0
		
		if (average >= 6.0)
		{
			return count;
		}
		
		while (average < 6.0)
		{
			summation += 8.0;
			denominator += 1;
			count ++;
			average = summation / denominator;
		}
		return count;
		
	}

This post has been edited by Shutterbug84: 08 March 2010 - 11:39 AM

Was This Post Helpful? 0
  • +
  • -

#8 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 557
  • Joined: 19-February 10

Re: Averaging in Java

Posted 08 March 2010 - 11:41 AM

View PostShutterbug84, on 08 March 2010 - 09:29 AM, said:

Ok, I changed the code this morning- and for some reason, my counter is only returning a value of 1 whenever I test it. What else am I doing wrong?

Thank you!

Yes, please post your code. I ran this and it worked for me. You might have wrote something wrong...
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10185
  • View blog
  • Posts: 37,603
  • Joined: 27-December 08

Re: Averaging in Java

Posted 08 March 2010 - 04:42 PM

There are two problems here. First, at this line in the foreach loop: summation += scores [q];, the variable q takes on the value of the element at the specific iteration, so you should add q, not scores[q]. So to contrast the two for loops:
for(int q = 0; q < scores.length; q++){
   //scores[q] is the element
}

for(int q:scores){
   //q is the element here
}



Also, at this line: average = summation / scores.length;, since summation and scores.length are both int variables, they are divided according to the rules of integer division, then the result is converted and assigned to the average variable of type double. So for example, if you have 33/4, the result would be 8.0, as 33/4 evaluates to 8 using integer division, then it is implicitly cast to a double, leaving you with 8.0. If you want the result to be able to hold non-integral numbers, you should either change summation to double at declaration or cast it to a double when you assign the average variable. Like so: average = (double)summation / scores.length;. So here, the value stored in summation is converted to a double, which will then allow your result of 33.0/4 to be 8.25.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1