how do you not calculate first row of a multidimensional array?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: how do you not calculate first row of a multidimensional array?

  1. #1
    Join Date
    Nov 2003
    Posts
    4

    Question how do you not calculate first row of a multidimensional array?

    I am trying to use a double array to calculate sales totals per saleperson and overall averages but do not want to use the salesperson number in the calculations. Can someone show me the correct code?

    Below is how I start:
    Code:
    import java.awt.*;
    
    public class SalesArray extends JApplet { 
    int Sales[][] = {{1180,45,75,94,65},
                             {1181,21,88,89,20},
                             {1182,79,51,55,67}};
    
    int salesperson, items;
    String output;
    JTextArea outputArea;
    
    {rest of code}
    I am trying to not use the first column in my calculation but to output it only under saleperson and get the following output:
    Code:
    Salesperson [1] [2] [3] [4]  Average:
           1180      45 75  94  65   
           1181      21 88  89  20
           1182      79 51  55  67
    
    Lowest Sales earned = 
    Highest Sales earned = 
    Overall Average = 
    Mediam of overall Sales =
    [ArchAngel added CODE tags]

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    Firstly, this is not a good way to store your data. But I'm assuming that this is a coursework, so your lecturer has decided that data is to be stored like this. Tell him I think that's rubbish design.

    Anyway, I can't see any effort you've made to solve this problem. Post the code you have so far and we'll see what we can do.

    However, the simple answer is "start your calculations from index 1, not 0".
    ArchAngel.
    O:-)

  3. #3
    Join Date
    Nov 2003
    Posts
    4
    So your saying that my problem is starting with element 0? I am using elements 1 through (SalesArray.length - 1) in my code. Element 0 is the salesperson.

    I am printing elements 0 through length-1 to produce column titles.

  4. #4
    Join Date
    Mar 2003
    Posts
    834
    So what's the problem?
    ArchAngel.
    O:-)

  5. #5
    Join Date
    Nov 2003
    Posts
    4
    Sorry it took so long to respond - vacation.

    Here is the code I have so far...I cannot figure out how to ignore the first element of the array in my calculations (especially total average of all sales) and how to display the results in the proper format.

    Can someone help?

    import java.awt.*;
    import javax.swing.*;

    public class SalesArray extends JApplet {
    int Sales[][] = {{1180,45,75,94,65},
    {1181,21,88,89,20},
    {1182,79,51,55,67}};

    int salesperson, items;
    String output;
    JTextArea outputArea;


    public void init()
    {
    salesperson=sales.length;
    items = sales[ 1 ].length;

    outputArea = new JTextArea();
    Container container = getContentPane();
    container.add(outputArea);

    output = "The array is:\n";
    buildString();

    output += "\n\nLowest sale: " + minimum() +
    "\nHighest sale: " + maximum() + "\n";

    for (int counter = 0; counter < salesperson; counter++)
    output += "\nAverage for Salesperson" + counter + "is"+
    average(Sales[counter]);

    outputArea.setFont(
    new Font("Courier", Font.PLAIN, 12));

    outputArea.setText(output);
    }

    public int minimum()
    {
    int lowSales = Sales[0][1];

    for (int row=0; row < salesperson; row++)
    for (int column = 1; column < items; column++)

    if (Sales[row][column]<lowSales)
    lowSales = Sales[row][column];
    return lowSales;
    }

    public int maximum()
    {
    int highSales = Sales[0][1];

    for (int row=0; row < salesperson; row++)
    for (int column = 1; column < items; column++)

    if (Sales[row][column] > highSales)
    highSales = Sales[row][column];

    return highSales;
    }

    public double average(int setOfsales[])
    {
    int total = 0;

    for (int count = 0; count < setOfsales.length-1; count++)
    total += setOfsales[count];

    return(double)total / setOfsales.length;
    }

    public static double mean(int setOfsales[])
    {
    double sum = 0; // sum of all the elements
    for (int count= 0; count < setOfsales.length-1; count++) {
    sum += setOfsales[count];
    }
    return sum / setOfsales.length;
    }//end method mean

    public void buildString()
    {
    output +=" ";

    for (int counter = 0; counter < items; counter++)
    output +="Salesperson" + "[ " + counter + " ]" + "Average\n";

    for(int counter=0; counter<items;counter++)
    output+="Total Average" + "mean";

    for (int row = 0; row < salesperson; row++) {
    output += "[" + row + "] ";

    for(int column = 1; column < items; column++)
    output += Sales[row][column] + " ";
    }
    }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center