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

Randomly generate 0, 1, or 2 in a 100 by 100 2d array. For each entry...
Randomly generate 0, 1, or 2 in a 100 by 100 2d array. For each entry in the 2d array, count the number of 0’s and 1’s in the surrounding entries. Save the result in two 2d arrays: one for the number of 0’s, one for the number of 1’s. For example: 0   0   1 2   1   0 0   0   0 The number of 0’s for a[0][0] is 1; the number of 1’s for a[0][0] is 1; The number of...
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...
Java- Create a new class named Forest that has a 2D array of integers to store...
Java- Create a new class named Forest that has a 2D array of integers to store a forest. Create a private inner class Position to store the row and column of the current cell. Create a Depth First Search Method that uses a stack to remove the current tree and search its neighbors based on the following pseudocode: // given a forest "f" // store positions on fire Stack<Position> cellsToExplore // the forest is aflame! for each tree in the...
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.
Submit a well-commented C program.   Create code. That will build a random 8x8 2D array of...
Submit a well-commented C program.   Create code. That will build a random 8x8 2D array of small-case characters. Demonstrate how it functions by printing out the result on your screen. Add a function will print out a single line of the array. If the user enters a value between 0 and 7 to select to print out a single row of the original 2D array.                void printLine(char *,int);    // This is the prototype declaration             void printLine(char myArr[][])     //...
When you create a C string (an array of chars) without the * symbol for pointer,...
When you create a C string (an array of chars) without the * symbol for pointer, is the pointer towards the 1st char created or not along with the array?
Lab 7 - 2D Arrays (C++) In main, declare and fill a 2D array with one...
Lab 7 - 2D Arrays (C++) In main, declare and fill a 2D array with one hundred rows and fifty columns. Iterate through each element and assign it a random value between -72 and 75 inclusive. Have your random number seed be 25. Create functions that do the following: 1. A function called “sum” that returns the sum of all the elements in your 2D Array. 2. A function called “average” that return the average value of the elements in...
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...
Create and initialize a string array, write and use 3 functions.Write a program. Create a string...
Create and initialize a string array, write and use 3 functions.Write a program. Create a string array in the main(),called firstNameArray, initialize with7 first namesJim, Tuyet, Ann, Roberto, Crystal, Valla, Mathilda Write a first function, named searchArray, that passes two arguments: a single person’s name and the array reference, into a function,This function should then search the array to see if the name is in the firstNameArray. If found, it will return the array index where the name is found,...
Write a C program that generates the following 2D array. 64   32   16   8   4   2  ...
Write a C program that generates the following 2D array. 64   32   16   8   4   2   1 32   32   16   8   4   1   1 16   16   16   8   4   2   1 8   8   8   8   4   2   1 4   4   4   4   4   2   1 2   2   2   2   2   2   1 1   1   1   1   1   1   1
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT