Answer to Question #331148 in C# for Zarifa

Question #331148

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 #)


1
Expert's answer
2022-04-20T03:45:00-0400
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);
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS