Average numbers that are greater than 70 from an Array.

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 547 Views - Last Post: 29 February 2016 - 12:29 PM Rate Topic: -----

#1 jshakil  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 202
  • Joined: 04-February 16

Average numbers that are greater than 70 from an Array.

Posted 29 February 2016 - 11:32 AM

I have this code so far:

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

        int[ ] abc = { 37, 14, 86, 99, 54 };
        int total = 1;
        for (int i = 2 ; i < abc.length; i++)
            total = total + abc[i];
        
    }
}



Would I need to use a if else statement to get numbers that are greater than 70 to be averaged out?

Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Average numbers that are greater than 70 from an Array.

#2 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 615
  • Posts: 2,595
  • Joined: 13-June 14

Re: Average numbers that are greater than 70 from an Array.

Posted 29 February 2016 - 11:34 AM

Well, how do you think you can extract the values greater than 70 so you can just average those?
Was This Post Helpful? 2
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12189
  • View blog
  • Posts: 45,251
  • Joined: 27-December 08

Re: Average numbers that are greater than 70 from an Array.

Posted 29 February 2016 - 11:36 AM

Quote

Would I need to use a if else statement to get numbers that are greater than 70 to be averaged out?


This goes back to something I mentioned in your earlier thread. Programming isn't calculus. You don't learn by having someone tell you where each line of code goes. Programming is about logic. Think about the logic and (most importantly) try it! Otherwise, we won't be able to help you much.
Was This Post Helpful? 2
  • +
  • -

#4 jshakil  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 202
  • Joined: 04-February 16

Re: Average numbers that are greater than 70 from an Array.

Posted 29 February 2016 - 11:36 AM

Grab the index of the elements that contain numbers that are greater than 70.
Was This Post Helpful? 0
  • +
  • -

#5 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 615
  • Posts: 2,595
  • Joined: 13-June 14

Re: Average numbers that are greater than 70 from an Array.

Posted 29 February 2016 - 11:37 AM

OK, that's one way to do it. So, like macosxnerd101 says, try it.
Was This Post Helpful? 0
  • +
  • -

#6 jshakil  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 202
  • Joined: 04-February 16

Re: Average numbers that are greater than 70 from an Array.

Posted 29 February 2016 - 11:38 AM

Okay.
Was This Post Helpful? 0
  • +
  • -

#7 jshakil  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 202
  • Joined: 04-February 16

Re: Average numbers that are greater than 70 from an Array.

Posted 29 February 2016 - 11:54 AM

Is it really this simple?

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

        double[ ] num = { 37, 14, 86, 99, 54 };
        System.out.println("Numbers that are greater than 70 are, 86 and 99. Their average is: " + (num[2] +  + num[3]) / 2);
    }
}


Was This Post Helpful? 0
  • +
  • -

#8 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 615
  • Posts: 2,595
  • Joined: 13-June 14

Re: Average numbers that are greater than 70 from an Array.

Posted 29 February 2016 - 11:56 AM

For this particular case, that works. What about if you wanted to do it for some arbitrary array? What would you do then?

This post has been edited by ndc85430: 29 February 2016 - 11:57 AM

Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12189
  • View blog
  • Posts: 45,251
  • Joined: 27-December 08

Re: Average numbers that are greater than 70 from an Array.

Posted 29 February 2016 - 11:56 AM

What if I gave you an arbitrary array where you (the developer) didn't know the values? Would your logic still work?
Was This Post Helpful? 0
  • +
  • -

#10 jshakil  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 202
  • Joined: 04-February 16

Re: Average numbers that are greater than 70 from an Array.

Posted 29 February 2016 - 11:57 AM

Nope my code wouldn't work. I'd struggle making that program.
Was This Post Helpful? 0
  • +
  • -

#11 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 615
  • Posts: 2,595
  • Joined: 13-June 14

Re: Average numbers that are greater than 70 from an Array.

Posted 29 February 2016 - 11:57 AM

Developers are clearly tuned to thinking a certain way ;).
Was This Post Helpful? 0
  • +
  • -

#12 jshakil  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 202
  • Joined: 04-February 16

Re: Average numbers that are greater than 70 from an Array.

Posted 29 February 2016 - 11:58 AM

I'm going to try it though.
Was This Post Helpful? 0
  • +
  • -

#13 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 615
  • Posts: 2,595
  • Joined: 13-June 14

Re: Average numbers that are greater than 70 from an Array.

Posted 29 February 2016 - 11:59 AM

Why are you struggling? You described the first step in your solution in post 4. What are you having difficulty with in making that work?
Was This Post Helpful? 0
  • +
  • -

#14 jshakil  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 202
  • Joined: 04-February 16

Re: Average numbers that are greater than 70 from an Array.

Posted 29 February 2016 - 12:03 PM

It would be on how to print numbers out that are greater than 70, without declaring the elements in which they're in.

What if I had 10 elements in the array, and had around six elements that were greater than 70. I would want to code something that would automatically grab elements that have numbers greater than 70 and then find the average.
Was This Post Helpful? 0
  • +
  • -

#15 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 615
  • Posts: 2,595
  • Joined: 13-June 14

Re: Average numbers that are greater than 70 from an Array.

Posted 29 February 2016 - 12:07 PM

Here's such an array of numbers then: [0, 14, 76, 91, 2, 88, -1, 18, 201]. Pretend you don't have a computer. Describe what you do to find the values greater than 70.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2