Write a program with structure Inventory(name, colour, size, cost). Then create 3 objects and write code to print information about Inventory with the highest cost. (FOR C #)
using System;
namespace inventory
{
struct Inventory
{
public string name;
public string colour;
public int size;
public double cost;
public void Print()
{
Console.WriteLine("name: {0}, colour: {1}, size: {2}, cost: {3}", name, colour, size, cost);
}
}
class Program
{
public static void Main(string[] args)
{
Inventory[] inv;
double high;
int n_high;
int n = 3;
inv = new Inventory[3];
for(int i=0; i<n; i++)
{
Console.Write("Enter name: ");
inv[i].name = Console.ReadLine();
Console.Write("Enter colour: ");
inv[i].colour = Console.ReadLine();
Console.Write("Enter size: ");
inv[i].size = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter cost: ");
inv[i].cost = Convert.ToDouble(Console.ReadLine());
}
high = 0;
n_high = 0;
for(int i=0; i<n; i++)
if (inv[i].cost > high)
{
high = inv[i].cost;
n_high = i;
}
Console.WriteLine("\nInventory with the highest cost:");
inv[n_high].Print();
Console.Write("\nPress any key to continue . . . ");
Console.ReadKey(true);
}
}
}
Comments
Leave a comment