Question

In: Computer Science

C# - count =0; - Create a 2d array "items". string[,] items = new string[100, 4];...

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], " ");
}
}

Solutions

Expert Solution

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.


Related Solutions

In C, build a connect 4 game with no GUI. Use a 2D array as the...
In C, build a connect 4 game with no GUI. Use a 2D array as the Data structure. First, build the skeleton of the game. Then, build the game. Some guidelines include... SKELETON: Connect Four is a game that alternates player 1 and player 2. You should keep track of whose turn it is next. Create functions: Initialization – print “Setting up the game”. Ask each player their name. Teardown – print “Destroying the game” Accept Input – accept a...
In C create an array of 4 integers. Assign a pointer to the array. Use the...
In C create an array of 4 integers. Assign a pointer to the array. Use the pointer to find the average value of the elements in the array and display the results on the screen.
in C programming language char character [100] = "hello"; a string array variable It is given....
in C programming language char character [100] = "hello"; a string array variable It is given. By writing a function called TranslateString, By accessing the pointer address of this given string, returning the string's address (pointer address) by reversing the string Write the function and use it on the main function. Function void will not be written as. Return value pointer address it will be. Sweat operation on the same variable (character) It will be made. Declaration of the function...
Write a recursive method to determine if a String is a palindrome. Create a String array...
Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. In Java
Challenge 1 – 2D Array Maker Create a function that takes in two parameters and returns...
Challenge 1 – 2D Array Maker Create a function that takes in two parameters and returns a created two-dimension array. var twoD = Make2D(<width>, <height>); Challenge 2 – Fill 2D Array Create a function that takes a single parameter and fills the 2D array with that parameter Use Cases: Fill2D(<twoD array>, <fill value>); //fills twoD twoD = fill2D(<twoD array>, <fill value>); //fills twoD, but also returns reference Challenge 3 – Make 2D Array Write a function that takes 3 parameters...
In C++ Create a dynamic array of 100 integer values named myNums. Use a pointer variable...
In C++ Create a dynamic array of 100 integer values named myNums. Use a pointer variable (like ptr) which points to this array. Use this pointer variable to initialize the myNums array from 2 to 200 and then display the array elements. Delete the dynamic array myNums at the end. You just need to write part of the program.
Write a program in c++ that reads x[4]. Then create the array y[6||6], such that the...
Write a program in c++ that reads x[4]. Then create the array y[6||6], such that the first quarter of y consists of the value of the first element of x. The second quarter of y consists of the value of the second element of x. The third quarter of y consists of the value of the third element of x. The fourth quarter of y consists of the value of the fourth element of x?
write code to count the number of odd integers in an array of 100 random integers...
write code to count the number of odd integers in an array of 100 random integers in the range [0,99].
C++ How do you make a 2d array with a user input. For example, the row...
C++ How do you make a 2d array with a user input. For example, the row and columns of the 2d array will be the same so the program can create a square matrix.
Find all rectangles filled with 0 We have one 2D array, filled with zeros and ones....
Find all rectangles filled with 0 We have one 2D array, filled with zeros and ones. We have to find the starting point and ending point of all rectangles filled with 0. It is given that rectangles are separated and do not touch each other however they can touch the boundary of the array.A rectangle might contain only one element. In Javascript preferred. Examples: input = [ [1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT