In: Computer Science
C#
- count =0;
- Create a 2d array "items".
string[,] items = new string[100, 4];
- Create a do while loop, when the user enters "0", stop the
loop.
- Users enter the item name, price, quantity, save this info and
subtotal to array.
- increase "count++"
-convert price(decimal, Convert.ToDecimal() ) and quantity(int)
to do multiplication
for subtotal
- create a for loop (with count ) to cycle through the "items",
display item name, price, quantity,
and subtotal.
accumulate subtotal
- Display total items, subtotal, total at the end.
Guide:
string[,] items = new string[100, 4];
int i, j, count;
/* input each array element's value */
for (i = 0; i < 100; i++) {
items[i,0] = Console.ReadLine("Enter Item Name: ");
if items[i,0] == "0"
break;
else
count++;
items[i,1] = Console.ReadLine("Enter Item Price: ");
items[i,2] = Console.ReadLine("Enter Item Quantity: ");
items[i,3] =
(Convert.ToDouble(items[i,1])*Convert.ToDouble(items[i,2])).toString();
}
/* output each array element's value */
for (i = 0; i < count; i++)
{
for (j = 0; j < 4; j++) {
Console.WriteLine(items[i,j], " ");
}
}
Dear Student ,
As per requirement submitted above kindly find below solution.
Here new console application in C# is created using visual studio 2019 with name "_2DArray". This application contains a class with name "Program.cs".Below is the details of this class.
Program.cs :
//namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//application namespace
namespace _2DArray
{
class Program //C# program
{
//main() method
static void Main(string[] args)
{
//Create a 2d array "items".
string[,] items = new string[100, 4];
//declaring variables
int i = 0, count = 0;
double subtotal = 0;
//do while loop,
do
{
//asking user item name
Console.Write("Enter Item Name: ");
//reading item name
String itemName = Console.ReadLine();
//checking item name
if (itemName == "0")
//if item name is 0 then break the loop
break;
else
//increment count
count++;
items[i, 0] = itemName;
//asking item price
Console.Write("Enter Item Price: ");
items[i, 1] = Console.ReadLine();//reading item price
//asking item quantity
Console.Write("Enter Item Quantity: ");
items[i, 2] = Console.ReadLine();//reading quantity
items[i, 3] = (Convert.ToDouble(items[i, 1]) *
Convert.ToDouble(items[i, 2])).ToString();
subtotal = subtotal + Convert.ToDouble(items[i, 1]) *
Convert.ToDouble(items[i, 2]);
i++;
} while (i < 100);
//using for loop display details
/* output each array element's value */
for (i = 0; i < count; i++)
{
//display details
Console.WriteLine("Item Name :" + items[i, 0]);
Console.WriteLine("Item Price :" + items[i, 1]);
Console.WriteLine("Item Quantity :" + items[i, 2]);
Console.WriteLine("subtotal :" + items[i, 3]);
}
//display subtotal
Console.WriteLine("Total : " + subtotal);
}
}
}
==================================
Output :Run application using F5 and will get the screen as shown below
NOTE :PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.