Answer to Question #271162 in C for vindy

Question #271162

Use a double-subscripted array to solve the following problem. A company has four salespeople ( 1 to 4) who sell five different products ( 1 to 5). Once a day, each salesperson passes in a slip for each different type of product sold. Each slip contains: a) The salesperson number b) The product number c) The total dollar value of that product sold that day Assume that the information from all the slips for a day is available. Write a program that will read all this information for the day and store in a double-subscripted array sales. For each product find and display the total sales. 


1
Expert's answer
2021-11-25T00:53:11-0500
import java.util.Scanner;

public class Sales2 {
    public void calculateSales() {
        Scanner input = new Scanner( System.in );
        // sales array holds data on number of each product sold
        // by each salesperson
        double sales[][] = new double[ 5 ][ 4 ];

        System.out.print( "Enter salesperson number (-1 to end): " );
        int person = input.nextInt();

        while ( person != -1 ) {
            System.out.print( "Enter product number: " );
            int product = input.nextInt();
            System.out.print( "Enter sales amount: " );
            double amount = input.nextDouble();

            // error-check the input
            if ( person >= 1 && person < 5 &&
                    product >= 1 && product < 6 && amount >= 0 )
                sales[ product - 1 ][ person - 1 ] += amount;
            else
                System.out.println( "Invalid input!" );

            System.out.print( "Enter salesperson number (-1 to end): " );
            person = input.nextInt();
        } // end while

        // total for each salesperson
        double salesPersonTotal[] = new double[ 4 ];

        // display the table
        for ( int column = 0; column < 4; column++ )
            salesPersonTotal[ column ] = 0;

        System.out.printf( "%8s%14s%14s%14s%14s%10s\n",
                "Product", "Salesperson 1", "Salesperson 2",
                "Salesperson 3", "Salesperson 4", "Total" );

        // for each column of each row, print the appropriate
        // value representing a person's sales of a product
        for ( int row = 0; row < 5; row++ ) {
            double productTotal = 0.0;
            System.out.printf( "%8d", ( row + 1 ) );

            for ( int column = 0; column < 4; column++ ) {
                System.out.printf( "%14.2f", sales[ row ][ column ] );
                productTotal += sales[ row ][ column ];
                salesPersonTotal[ column ] += sales[ row ][ column ];
            } // end for

            System.out.printf( "%10.2f\n", productTotal );
        } // end for

        System.out.printf( "%8s", "Total" );

        for ( int column = 0; column < 4; column++ )
            System.out.printf( "%14.2f", salesPersonTotal[ column ] );

        System.out.println();
    } // end method calculateSales
} // end class Sales2

public class Sales2Test {
    public static void main( String args[] ) {
        Sales2 application = new Sales2();
        application.calculateSales();
    } // end main
} // end class Sales2Test

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS