简体   繁体   中英

3 dimensional array retrieve values

I am writing a program which allows the user to enter in sales amounts for 3 salepeople and 5 products for every day of the month. I am using a 3 dimensional array to store the data. I would like to print my data in a tabular format with columns for the 3 salespeople and rows for the five products with each amount being the total sales of the product for the month ie the sum of the 31 values. Also I need to have the cross totals at the end of every column and row

this is my code:

class Program
{
    static void Main(string[] args)
    {

        Slip [,,] sales = new Slip[3, 5, 31];
        for (int day = 1; day <= 31; day++)
        {
            for (int i = 1; i <= 3; i++)
            {
                Console.WriteLine("Enter the salesperson number of #" + i + ":");
                int salesPersonNumber = Convert.ToInt16(Console.ReadLine());
                Console.WriteLine("Enter the information for day " + day + ", salesperson " + salesPersonNumber + " below:");
                for (int j = 1; j <=5; j++)
                {

                    Console.WriteLine("Enter the product number for product " + j + ":"); 
                    int productNumber = Convert.ToInt16(Console.ReadLine());
                    Console.WriteLine("Enter the total dollar value of the product sold day " + day + ":");
                    decimal value = Convert.ToDecimal(Console.ReadLine());
                    Slip slip = new Slip(salesPersonNumber, productNumber, value);
                    sales[i-1, j-1, day-1] = slip;

                }
            }
        }



        for (int i = 0; i < sales.GetLength(0); i++){ 
            for (int j = 0; j < sales.GetLength(1); j++){
                decimal total = 0;
                for (int k = 0; k < sales.GetLength(2); k++){
                    total += sales[i, j, k].ValueSold;
                }

                Console.WriteLine(total);

             }

         }

    }
}

I can't figure out how to retrieve the data from the three dimensional array as I described above to print a table

You need to loop through your array twice. You need a single loop to display the sales people header. You need the nested loop to display your rows. You can generate the text for the row identifier, the day, in the first inner loop. You can also generate the line ending there as well. The inner most loop can be used to display the total counts for that day and sales person.

Although this doesn't directly answer your question, it may make answering if yourself easier.

Have you considered using objects rather than a multi-dimension array? It would make keeping track of everything a lot easier, and is general better practice for complex structures such as this one. It would also allow you to abstract your calculations; such as getting the total number of products sold within a month.

From what I've understood, there would be 2 classes, a SalesPerson and a Product . Each would 'house' the next level array of objects, and then you would simply have a single dimensional array in your main method for SalesPerson[3] . Something like this:

/// <summary>
/// A sales person
/// </summary>
public class SalesPerson
{
    /// <summary>
    /// Gets or sets an array of products
    /// </summary>
    public Product[] Products { get; set; }

    /// <summary>
    /// Constructs a new sales person class, constructing a new products array
    /// </summary>
    public SalesPerson()
    {
        this.Products = new Product[5];
    }
}

/// <summary>
/// A product
/// </summary>
public class Product
{
    /// <summary>
    /// Gets or sets the sales amount array
    /// </summary>
    public int[] SalesAmount { get; set; }

    /// <summary>
    /// Constructs a new product, constructing a new sales amount array
    /// </summary>
    public Product()
    {
        this.SalesAmount = new int[31];
    }

    /// <summary>
    /// Gets the total sold
    /// </summary>
    public int GetTotalSold()
    {
        return this.SalesAmount.Sum();
    }
}

Hope it helps. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM