Use a two-dimensional array to solve the problem. A company has four sales people (1 to 4) who sell 5 different products (1 to 5). Once a day, each sales person passes in a slip for each type of products sold. each slip contains:
a. The salesperson number
b. The product number
c. The total dollar value of that product sold that day

Thus, each sales person passes in between 0 and 5 sales slip per day. Assume that the information from all the slips for last month is available. Write an application that will read all this information for last month's sales by sales person and by product. All totals should be stored in a two-dimensional array sales. After processing the information for last month, display the result in tabular format, with each column representing a particular product. Cross-total each row to get the total sales of each product for last month. Cross-total each column to get the total sales by sales person for last month. Your tabular output should include these cross-totals to the right of the total tows and to the bottom of the total columns.

Who can solve this? Please help! Thanks!

Recommended Answers

All 14 Replies

> Who can solve this?

You, of course will try or show us what you have tried/thought so far and the forum members will try to help you out.

a. The salesperson number
b. The product number
c. The total dollar value of that product sold that day

Apparently the rows (1 to 4) will be the salesperson number.
The columns (1 to 5) will be the product number
And the elements of the array will be the total dollar value of that product sold that day

So this:
array[2][3] = 100
Will mean that last month the salesperson 3 sold the product 4 and earned 100 dollars.
You need to populate the array with some values on your own and then use for loops to calculate the statisitics

I tried my best to do it and i ended up with this:

import java.io.*;
import java.util.*;

public class TotalSales
{
       public static void main(String[] args) throws IOException
{
    int array [][] = new int [5][6];
    int x, y;
    int sum = 0;
    
    
    Scanner infile = new Scanner(newFileReader("cj.in.txt"));
    for (int x = 0; x < array.length; x++){
    for(int y = 0; y < array[x].length; y++)
            array[x] [y] = infile.nextInt();
}
        infile.close();
    
    for(x = 0; x < 5; x++){
    for(y = 0; y < 6; y++){
    System.out.print(array[x][y] + "    ");
}
System.out.printf("\n");
}

System.out.printf("\n");


 for(x = 1; x < 5; x++){
    for(y = 1; y < 6; y++){
    sum = sum + array[x][y];
   System.out.print(sum + "   ");
}
System.out.printf("\n");
}

}

It's not yet finished coz i don't know what to do next. Can someone help me? I would really appreciate it.Thanks!

Ei can you help me? what's wrong with my program? i think there's something missing. Please help! Thanks!

Ei can you help me? what's wrong with my program? i think there's something missing. Please help! Thanks!

well... my name 's not Ei, but I'll try to answer for him...
1. tell us clearly what you want that the code you wrote should do (that does not mean repeat your assignment)
2. tell us what your code actually is doing
3. more important, tell us what it isn't doing

show any error messages you get. if you want us to test it, you'll be asking us to do work you already did, not really the most efficient way to work is it? ;)

Ei can you help me? what's wrong with my program? i think there's something missing. Please help! Thanks!

I was busy that's why I couldn't reply.
As for your code:

for(x = 1; x < 5; x++)
    for(y = 1; y < 6; y++)
    sum = sum + array[x][y];

   System.out.print(sum + "   ");

You successfully calculate the sum of all the salesmen for all the products. Now, you will need per salesman and per product.
Therefor you will need to calculate for each row and each column the sum

for(x = 1; x < 5; x++) {
    sum = 0 ;

    for(y = 1; y < 6; y++) {
       sum = sum + array[x][y];
    }

System.out.println("X: "+x+". Sum= "+sum);
}

Try, after running the above code to understand what it does, by comparing the results to the values the array has. As you can see for each row (X) you calculate the sum (inner loop) and the you print it. Then when you go to the next line (outer loop) you reset the sum (sum=0) in order to add only the elements of the next line (X) you are now.

Try running the code by hand. It will help you understand it better.
Then all you need to do is modify the above code so you will get the sum per column

I can't think of a way to do that. I mean, how to calculate the sum per row and per column. I don't know how. Please help me. Thank you.

I can't think of a way to do that. I mean, how to calculate the sum per row and per column. I don't know how. Please help me. Thank you.

Apparently you don't read other people's posts, because I have just explained how to do this, and I have posted 2 pieces of code, which I have told you to run them.

Did you follow the steps I described?

Yes! I've got it! I was able to modify the code that you gave to compute for the sum per column. Now my problem is, I don't know how to include those sums in my array wherein i have to put all the sum per row to the right and all the sum per column at the bottom of each column in my array and display it in a tabular format. How will i do that? Please help me. Thanks!=)

I thought javaAddict posted what you needed to do above . .

you don't know how to display it in tabular format or what?

Yes! I've got it! I was able to modify the code that you gave to compute for the sum per column. Now my problem is, I don't know how to include those sums in my array wherein i have to put all the sum per row to the right and all the sum per column at the bottom of each column in my array and display it in a tabular format. How will i do that? Please help me. Thanks!=)

You need to better explain what you need to do

i need to include those sums in my array and be able to display it. How will i include those sums in my array and put them in their appropriate place? like for example like this kind of output:

[ ] 1 2 3 4 5 T
111 5 1 7 6 2 s
222 3 7 5 6 3 s
333 7 1 8 9 4 s
444 8 9 7 4 1 s
T s s s s s

Save the sums into 2 different 1-D arrays and when you loop the 2-D array to display the elements display next to each row (row i) the value at "i" of the rowSum array.
And that loop finishes, have another loop that prints 1 more line with sums from the other 1-D array (colSum)

ei shockwave, can u give me the final code coz i really nid it....i will patience for ur reply....pls help me...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.