3 dimensional arrays extract values

I am writing a program that allows the user to enter sales amounts for 3 sellers and 5 products for each day of the month. I use a 3-dimensional array to store data. I would like to print my data in a tabular format with columns for 3 sellers and rows for five products, each sum of which represents the total sales volume of the product per month, i.e. The sum of 31 values. I also need to have cross totals at the end of each 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 cannot figure out how to extract data from a three-dimensional array, as described above, to print a table

+4
source share
2 answers

You need to scroll the array twice. To display the title of the sales list, you need one cycle. To display strings you need a nested loop. You can generate text for the id of the line, day, in the first inner loop. You can also create a line ending there. The inner loop itself can be used to display the totals for this day and seller.

+1
source

Although this does not give a direct answer to your question, it can answer if it is easier for you.

Have you considered using objects, rather than a multidimensional array? This would make it easier to keep track of everything, and it is more common practice for complex structures like this. It will also allow you to abstract your calculations; such as getting the total number of products sold within a month.

From what I understood, there would be 2 classes, a SalesPerson and a Product . Each of them will β€œplace” an array of objects of the next level, and then you just have to have one dimensional array in your main method for SalesPerson[3] . Something like that:

 /// <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 this helps. :)

0
source

Source: https://habr.com/ru/post/1402484/


All Articles