Click here to Skip to main content
15,918,471 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi
i have a project in my university
we should programming knapsack algorithm
user should enter capacity and items count of knapsack & weight & value .it must be with GUI. i program this project with c#.but i have some problems with design for this code GUI.
someone please help me..
below its my c sharp code .i want GUI for this but i cant..
sorry for my english

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace KnapsackAlgo
{
    class KnapsackAlgorithm
    {

        public static int KnapSack(int capacity, int[] weight, int[] value, int itemsCount)
        {
            int[,] K = new int[itemsCount + 1, capacity + 1];

            for (int i = 0; i <= itemsCount; ++i)
            {
                for (int w = 0; w <= capacity; ++w)
                {
                    if (i == 0 || w == 0)
                        K[i, w] = 0;
                    else if (weight[i - 1] <= w)
                        K[i, w] = Math.Max(value[i - 1] + K[i - 1, w - weight[i - 1]], K[i - 1, w]);
                    else
                        K[i, w] = K[i - 1, w];
                }
            }

            return K[itemsCount, capacity];
        }

        static void Main(string[] args)
        {
            
            Console.WriteLine("enter itemscount:");
            int itemsCount = int.Parse(Console.ReadLine());
            Console.WriteLine("enter capacity of knapsack:");
            int capacity = int.Parse(Console.ReadLine());
            int[] value = new int[itemsCount];
            int[] weight = new int[itemsCount];
            Console.WriteLine("enter values of items:");

            for (int i = 0;i < itemsCount;i++)
            { 
                
                int x = int.Parse(Console.ReadLine());
                value[i] = x;

            }

            Console.WriteLine("enter weights of items:");
            for (int j=0; j < itemsCount; j++)
            {
                int y = int.Parse(Console.ReadLine());
                weight[j] = y;
            }
            int result = KnapSack(capacity, weight, value, itemsCount);
            Console.WriteLine(result);
        }
    }
}
Posted
Updated 11-Jan-18 9:44am
Comments
PIEBALDconsult 11-Jan-18 18:57pm    
Separate the concerns.

We can't design a GUI for your homework: we have no idea what you might want to look at!

So sit down with the Visual Studio designer, open a new form and start thinking about what you want to input, to show, and when. They look at the controls you have available and start playing with dropping them on the form to see what they look like. If they don't look right, take them off again!

We can;t do your homework for you - partly because we don't know what you want or your tutor will find acceptable, but mostly because this is part of your task!
 
Share this answer
 
What about The Documentation[^]?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900