Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,637 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,071 people online right now. Registration is fast and FREE... Join Now!




array help

 
Reply to this topicStart new topic

array help

bigticket61
post 12 Oct, 2008 - 04:29 PM
Post #1


New D.I.C Head

*
Joined: 12 Oct, 2008
Posts: 13

Im stuck. Here are the requirements. Please help.

write a program to print out the percent of the total rainfall that fell on each month.
To do this, you will need to set up a new array of doubles, say, “percent”.
To compute the percent value for a particular month, take the rainfall amount for that month divide
it by the total yearly rainfall and multiply it by 100.
Do this for each month using a loop.
Then modify the code to print out the monthly percent values too.

CODE

package cs162ic4;

import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.FileReader;

/**
* Reads in a file and computes the total and average rainfall information
* @authorJonathan Furst
*
*/
public class Main {

    /**
     * @param args
     * @throws FileNotFoundException
     */
    public static void main(String[] args) throws FileNotFoundException {
        final String INPUTFILE = "src/cs162ic4/rainfall.txt";
        
        /*
         * Set up everything needed to read a file
         * See pp. 147-158 in book.
         */
        Scanner inFile = new Scanner( new FileReader( INPUTFILE ) );
        String header = inFile.nextLine();
        
        /*
         * Read in the file of rainfall data
         */
        final int NUM_MONTHS = 12;
        double[] data = new double[ NUM_MONTHS ];
        for (int i=0; i<NUM_MONTHS; i++){
            data[i] = inFile.nextDouble();
        }
        inFile.close();
                
        /*
         * Compute total and average
         */
        double total = 0.0;
        for (int i=0; i<data.length; i++)
            total = total + data[i];
        
        double average = total/data.length;
        
        double total = 0.0;
        double [] percent;
        
        percent = new double [ 13 ];
        
        for(double i=0; i<percent.length; i++)
        
        
        
        
    
        
        /*
         * Print results
         */
        System.out.println( header );
        for (int i=0; i<data.length; i++)
            System.out.printf("  %.2f inches%n", data[i] );
        System.out.printf("  Total:   %.2f inches%n", total   );
        System.out.printf("  Average: %.2f inches%n", average );
    }

}



text document



Monthly Rainfall
0.1
0.05
0.7
2.0
7.8
4.7
2.1
0.2
1.2
3.1
0.2
0.05
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 12 Oct, 2008 - 04:48 PM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,062



Thanked 175 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Here is the total fix for your program. You had a few minor problems including declaring total twice (remember to declare it only once) and you had this for loop in there that wasn't really doing anything. So remove that.

Then as you see in the code below I put in the percent calculation in there so you can see how that would work and the result should be a nice little table of values.

java

import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.FileReader;

/**
* Reads in a file and computes the total and average rainfall information
* @authorJonathan Furst
*
*/
public class Main {

/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException {
final String INPUTFILE = "src/cs162ic4/rainfall.txt";

/*
* Set up everything needed to read a file
* See pp. 147-158 in book.
*/
Scanner inFile = new Scanner( new FileReader( INPUTFILE ) );
String header = inFile.nextLine();

/*
* Read in the file of rainfall data
*/
final int NUM_MONTHS = 12;
double[] data = new double[ NUM_MONTHS ];
for (int i=0; i<NUM_MONTHS; i++){
data[i] = inFile.nextDouble();
}
inFile.close();

/*
* Compute total and average
*/
// Initialize total only ONCE
double total = 0.0;
for (int i=0; i<data.length; i++)
total = total + data[i];

double average = total/data.length;


double [] percent;

percent = new double [ 13 ];

System.out.println( header );

// Loop through the array, printing the inches and then its percentage which is the value divided by the total * 100
for (int i=0; i<data.length; i++) {
System.out.printf(" %.2f inches (%.2f percent)%n", data[i], (data[i] / total) * 100 );
}
System.out.printf(" Total: %.2f inches%n", total );
System.out.printf(" Average: %.2f inches%n", average );
}

}


Enjoy!

"At DIC we be rainfall percentage coding ninjas... I guess that makes us meteorologists too. Cool!" decap.gif

This post has been edited by Martyr2: 12 Oct, 2008 - 04:50 PM
User is offlineProfile CardPM

Go to the top of the page

Gloin
post 12 Oct, 2008 - 04:49 PM
Post #3


On MeD.i.Cation

Group Icon
Joined: 4 Aug, 2008
Posts: 717



Thanked 46 times
My Contributions


So you've managed to read the data from the file into an array called data. That was the difficult part of this assignment. All you have to do then is to sum up the values in your array called data. The sum is calculated by using a for loop through the array and add every value to the sum variable which is initially 0. The sum is 21.8, save it into some variable of type double.

Now for each month, calculate the value of the month divided by th sum and then multiply the result by 100 to get the percentage. Again this is done using a for loop.
User is offlineProfile CardPM

Go to the top of the page

pbl
post 12 Oct, 2008 - 05:22 PM
Post #4


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,982



Thanked 190 times

Dream Kudos: 75
My Contributions


You had all the beginning right. What so complicated in computing the percent ?
CODE

        double[] percent = new double [ NUM_MONTHS];
        
        for(int i=0; i<percent.length; i++)
            percent[i] = data[i] / total * 100.0;

User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 04:18AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month