2 Replies - 5841 Views - Last Post: 11 December 2009 - 11:52 PM Rate Topic: -----

#1 polska03   User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 302
  • Joined: 28-November 09

2d array

Post icon  Posted 11 December 2009 - 05:53 PM

The program let's the user enter four quarterly sales figures for six divisions of a company. the figures should be stored in a two-dimensional array. Once the figures are entered, the program should display the following data for each quarter:
-----A list of the sales figures by division-------
-----Each division's increase or decrease from the previous quarter (not be displayed for 1st quarter)----
-----The total sales for the quarter-----
-----The company's increase or decrease from the previous quarter (not be displayed for 1st quarter)-----
-----The average sales for all divisions that quarter----

My question is I did the 2d array, but the part in bold I have no idea how to do,I have attempted to do the total sales, but it gives the total of ALL of them not each quarter, please help!


 import java.util.Scanner;
import java.text.DecimalFormat;


public class CorpSales
{
   public static void main(String[] args)
   {
	  final int DIVS = 3; // Three divisions in the company
	  final int QTRS = 4; // Four quarters
	  double totalSales = 0.0;  // Accumulator

	  // Create an array to hold the sales for each
	  // division, for each quarter.
	  double[][] sales = new double[DIVS][QTRS];

	  // Create a Scanner object for keyboard input.
	  Scanner keyboard = new Scanner(System.in);

	  // Display an introduction.
	  System.out.println("This program will calculate the " +
						 "total sales of");
	  System.out.println("all the company's divisions. " +
						 "Enter the following sales data:");

	  // Nested loops to fill the array with quarterly
	  // sales figures for each division.
	  for (int div = 0; div < DIVS; div++)
	  {
		 for (int qtr = 0; qtr < QTRS; qtr++)
		 {
			System.out.print("Division " + (div + 1) +
							 ", Quarter " + (qtr + 1) +
							 ": $");
			sales[div][qtr] = keyboard.nextDouble();
		 }
		 System.out.println();   // Print blank line.
	  }

	  // Nested loops to add all the elements of the array.
	  for (int div = 0; div < DIVS; div++)
	  {
		 for (int qtr = 0; qtr < QTRS; qtr++)
		 {
			totalSales += sales[div][qtr];
		 }
	  }

	  // Create a DecimalFormat object to format output.
	  DecimalFormat dollar = new DecimalFormat("#,##0.00");

	  // Display the total sales.
	  System.out.println("The total sales for the company " +
						 "are $" + dollar.format(totalSales));
   }
}


final int DIVS = 3; // Three divisions in the company
	  final int QTRS = 4; //



sorry that should be 6 and 4

Is This A Good Question/Topic? 0
  • +

Replies To: 2d array

#2 Sara_21   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 21-November 09

Re: 2d array

Posted 11 December 2009 - 11:41 PM

  final int DIVS = 3; // Three divisions in the company



Should be 6 divisions
B)
Was This Post Helpful? 0
  • +
  • -

#3 Sara_21   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 21-November 09

Re: 2d array

Posted 11 December 2009 - 11:52 PM

Try this .. I hope it will be helpful


	  for (int div = 0; div < DIVS; div++)
	  {
	   
	   
			totalSales += sales[div][1];
   

	  }

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1