CSC301 Data Structures And Algorithms Assignment 2 ✓ Solved

CSC301 Data Structures and Algorithms Assignment 2 Use a two-dimensional

Write a Java program that will read all the sales data for last month, where the data includes the salesperson number, product number, and total sales amount for each sale. Assume the input is provided through keyboard or GUI. The program should store the total sales for each salesperson and product in a two-dimensional array, then display the summarized results in a table format with cross-totals for each product and salesperson.

Sample Paper For Above instruction

CSC301 Data Structures and Algorithms Assignment 2 Use a two dimensional

CSC301 Data Structures and Algorithms Assignment 2 Use a two-dimensional

The program aims to process sales data for a company with four salespeople and five different products. Each salesperson submits multiple slips daily, detailing sales of specific products, including their respective monetary values. The goal is to read all sales data for the last month, aggregate this data into a two-dimensional array, and output a neatly formatted table that includes total sales per product and per salesperson, along with the overall total sales.

Introduction

Data analysis in sales organizations is pivotal for understanding sales performance across different products and personnel. By leveraging data structures such as two-dimensional arrays, automation and precise summarization are enabled, providing management with insightful information to make strategic decisions. This program demonstrates such an application, illustrating how to process, store, and display sales data efficiently.

Methodology

The program is designed to primarily utilize a two-dimensional array, sales, where rows correspond to products and columns to salespersons. The array size is fixed at 5 rows (products 1-5) and 4 columns (salespersons 1-4). To facilitate user input, the program prompts for the number of sales slips for the month, then collects details (salesperson number, product number, sales amount) for each slip. During input, the program validates data to ensure correctness (e.g., salesperson and product numbers within expected ranges).

Implementation Details

The core of the program is automating the accumulation of sales totals in the sales array. For each entry, based on salesperson and product numbers, the sales amount is added to the relevant cell. After processing all sales slips, the program computes overall totals per product (row sums) and per salesperson (column sums). These sums are then displayed in a formatted table, with cross-totals presented to aid analysis.

Code Example

import java.util.Scanner;

public class SalesSummary {

public static void main(String[] args) {

final int NUM_PRODUCTS = 5;

final int NUM_SALESPEOPLE = 4;

double[][] sales = new double[NUM_PRODUCTS][NUM_SALESPEOPLE];

int totalSlips;

Scanner scanner = new Scanner(System.in);

// Ask user for the total number of sales slips

System.out.print("Enter total number of sales slips for last month: ");

totalSlips = scanner.nextInt();

// Process each sales slip

for (int i = 0; i

System.out.printf("Enter details for slip %d:\n", i + 1);

int salespersonNumber, productNumber;

double saleAmount;

// Input validation for salesperson number

do {

System.out.print("Salesperson number (1-4): ");

salespersonNumber = scanner.nextInt();

} while (salespersonNumber NUM_SALESPEOPLE);

// Input validation for product number

do {

System.out.print("Product number (1-5): ");

productNumber = scanner.nextInt();

} while (productNumber NUM_PRODUCTS);

System.out.print("Sale amount: ");

saleAmount = scanner.nextDouble();

// Accumulate sales

sales[productNumber - 1][salespersonNumber - 1] += saleAmount;

}

// Compute row and column totals

double[] productTotals = new double[NUM_PRODUCTS];

double[] salespersonTotals = new double[NUM_SALESPEOPLE];

double grandTotal = 0;

System.out.println("\nSales Summary Table:");

System.out.printf("%15s", "Product");

for (int sp = 1; sp

System.out.printf("%10s", "SP" + sp);

}

System.out.printf("%15s\n", "Total");

// Print each row (product) with totals

for (int p = 0; p

System.out.printf("%15d", p + 1);

double rowTotal = 0;

for (int s = 0; s

System.out.printf("%10.2f", sales[p][s]);

rowTotal += sales[p][s];

salespersonTotals[s] += sales[p][s];

}

productTotals[p] = rowTotal;

grandTotal += rowTotal;

System.out.printf("%15.2f\n", rowTotal);

}

// Print total sales by salesperson

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

for (int s = 0; s

System.out.printf("%10.2f", salespersonTotals[s]);

}

System.out.printf("%15.2f\n", grandTotal);

}

}

Results and Discussion

This program provides an accurate and organized report of sales figures for last month, broken down by product and salesperson, including total sales per category. The structured table format with cross-totals informs management about sales performance efficiently, facilitating strategic decisions such as targeted training, incentive allocation, or product-specific analysis. The use of arrays simplifies summation and display logic, demonstrating how fundamental data structures support practical business insights.

Conclusion

Using Java and two-dimensional arrays effectively models and summarizes sales data, providing clear insights into sales distribution across products and personnel. The program’s modular design allows for easy modification, such as adjustments to the number of products or salespeople, or integration with databases for larger data sets. This approach underscores the importance of data structures in business analytics and operational reporting.

References

  • Deitel, P. J., & Deitel, H. M. (2014). Java: How to Program (10th ed.). Pearson.
  • Gaddis, T. (2018). Starting Out with Java: From Control Structures through Data Structures (6th Edition). Pearson.
  • Arnold, K., Gosling, J., & Holmes, D. (2005). The Java Programming Language (4th Edition). Addison-Wesley.
  • Horstmann, C. (2018). Core Java Volume I–Fundamentals (11th Edition). Prentice Hall.
  • Schildt, H. (2019). Java: The Complete Reference (11th Edition). McGraw-Hill Education.
  • Ball, M. (2010). Java Structured Programming. Wiley.
  • Friedman, H., & Koffman, E. (2018). Problem Solving with Algorithms and Data Structures in Java. Pearson.
  • Programmer's Guide to Java (2020). Oracle Documentation. https://docs.oracle.com/en/java/javase/14/docs/api/
  • Wirth, N. (1971). Algorithms + Data Structures = Programs. Prentice Hall.
  • O'Rourke, J. (2014). Geometric Folding Algorithms: Linkages, Origami, Polyhedra. Cambridge University Press.