This week's giveaway is in the Java in General forum.
We're giving away four copies of Java Challengers and have Rafael del Nero on-line!
See this thread for details.
Win a copy of Java Challengers this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

How to find average value of a row in a 2D array?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to make a program to store data of rainfalls for 3 years. Initialized an array of double rainfalls[3][12]...Made one function to find average rainfall values of the 2D array...Found the average rainfall of the total of 3 years but how do i find the average of per year? Like how do i find the average of 1st year, 2nd year and 3rd year individually??Here's the code:

 
Marshal
Posts: 72657
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Thank you for using the code button, but you didn't quite get it right. Please see the link on the left. If I remember, I'll correct that for you and it will look much better
Your question would fit better in another forum, so I shall move you there.

You calculate the arithmetical mean, which is probably the average you want, by adding all the numbers and dividing by how many numbers there are (12 or 36). You seem to be doing that already; please tell us whether your program doesn't work.

[edit]Why are you asking for keyboard input when you already have those figures in the array?
 
Campbell Ritchie
Marshal
Posts: 72657
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can iterate the rows and calculate the averages for each row: simply do the arithmetic inside the outer loop.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic