# Arrays

• March 4th, 2013, 10:25 PM
LeeDD
Arrays
Modify the stats class created in the previous assignment to allow it to store and analyze data sets of different sizes. it should now have an array that holds 100 double values instead of 12. An instance variable numElt will keep track of how many elements in the atrray are being used by a particular instance of the class. All of the Stats class functions should be revised to work with numElt elements instead of 12.

Can someone please guide me as to what exactly i need to do. im kinda confused.

Code :

```public class Stats { // records array field created to store twelve values private double [] records = new double [12];   // method to set values in array public void setValue(int rainfall, double actValue ) { records[rainfall] = actValue; }   public void processRainfallStats() { System.out.printf("The total rainfall for the year is %.2f\n\n", getTotal());   System.out.printf("The average rainfall for the year is %.2f\n\n", getAverage());   System.out.printf("The least rainfall for the year is %.2f\n\n", getSmallest());   System.out.printf("The most rainfall for the year is %.2f\n\n", getLargest()); }   // method to calculate total public double getTotal() { double total = 0;   for (double tot : records) {total += tot;} return total; }   // method to calculate average rainfall in records array public double getAverage() { double average;   int total = 0;   for (double avg: records) {total += avg;} average = total / records.length;   return average; }   // method to find largest index value in records array public double getLargest() { double largest = records[0]; // assuming that records[0] is the largest   for (double Lag : records) { if (Lag > largest) {largest = Lag;} } return largest; }   // method to find smallest index value in records array public double getSmallest() { double smallest = records[0]; // assuming that records[0] is the smallest   for (double small: records) { if (small < smallest) {smallest = small;} } return smallest; }     }```

Code :

```package leedanielassignment4;   import java.util.Scanner;   public class StatsTest { public static void main(String[] args) { Scanner input = new Scanner(System.in);   // new object of Stats class created Stats myStats = new Stats();   int rainfall; double actValue;   // new array of months to be store months String [] months = new String[] {"January","February","March","April","May","June","July","August", "September","October","November","December"};   for (int i = 0; i < months.length; i++) { // print method used to set value to month System.out.printf("Please eneter the amount of rainfall for %s:\n ", months[i]);   rainfall = i; actValue = input.nextDouble();   if(actValue < 0) {actValue = 0;} // object calling method in class to initiate values myStats.setValue(rainfall, actValue); } // object calls method of stats class to print info myStats.processRainfallStats(); }```
• March 5th, 2013, 06:32 AM
Norm
Re: Arrays
What part of the assignment are you having problems with?
• March 6th, 2013, 08:45 AM
LeeDD
Re: Arrays
im wondering if i hav to make numelt as a copy of the array and then assign it to all records functions? or do something completely different from what im suggesting i do
• March 6th, 2013, 08:49 AM
Norm
Re: Arrays
Quote:

instance variable numElt will keep track of how many elements in the array are being used
numElt would be an int that has the count of the number of elements in the array that are being used. If the array had 100 elements and there were 5 elements in the array, numElt would be 5
• March 6th, 2013, 01:01 PM
LeeDD
Re: Arrays
sorry but i still doont get it
• March 6th, 2013, 01:07 PM
Norm
Re: Arrays
If the array has 100 slots and the code is only going to use some of them, how do you keep track of how many slots of the array have been used?
• March 6th, 2013, 01:17 PM
LeeDD
Re: Arrays
using a loop
• March 6th, 2013, 01:25 PM
Norm
Re: Arrays
That is not very efficient to use a loop every time you want to know how many slots are used. Why not have the count of the number of used slots in a variable like: numElt?
• March 6th, 2013, 08:55 PM
LeeDD
Re: Arrays
i be tryna figure this thing out trying all kinds of things but still aint gettin it.. but thanks
• March 6th, 2013, 09:06 PM
Norm
Re: Arrays
Try writing a simple program that defines an array and an int variable: numElt. Add some elements to the array but don't fill it and set the value of numElt to the number of elements that were added to the array
• March 6th, 2013, 10:01 PM
LeeDD
Re: Arrays
aight thanks norm!! i was kinda close to this idea

--- Update ---

keeping track means just putting a limit on how much elements the array will use?

--- Update ---

is that what yu are saying is what im tryna do, will both of em giv me the result i want?

Code :

```double [] Stats = new double [12]; // this is an array of 12 elements int numElt = 10;   double [] Stats = {6, 2, 3, 56, 5, 67, 7, 23, 0, 0, 0, 0}; // this is a array of 12 elements, only 8 elements have values higher than 0 int numElt = 8;```
• March 7th, 2013, 07:01 AM
Norm
Re: Arrays
Now consider where the next empty slot is in the array you just defined. You can use the numElt variable to access it.
Use it to add an element to that array and increment numElt after the element is added.
Code :

```theArrray[numELt] = newData; // put data into the array at empty slot numELt++; // move to next empty slot and counts number in array```
• March 9th, 2013, 08:09 PM
LeeDD
Re: Arrays