Solved

# Looping through the dataset, vb.net

Posted on 2007-10-12
752 Views
Hello,

I have a dataset and following are the values with two columns:

column1       column2

The customer has the amount of 14,500

I need to loop through the dataset and display to the user saying:

we can accomodate only

This is (14,000 which is less than 14,500)

(we can also say that we can accomodate, gift1, gift3 and gift5, which sums to (14,000 < 14,500), but i have to check if the highest value is less than the given amount(14,500) and assign from there)

Any help would be appreciated

Thank you,
anagm
0
Question by:anagm

LVL 21

Expert Comment

Are you looking at displaying all possibilities of gifts that can be accomodated by 14500?
0

LVL 21

Accepted Solution

I would suggest sorting the dataset based on the amount in descending/descending order first and keep adding the values untill the value is less than 14500. displaying the gifts untill the sum became >= 14500

0

LVL 20

Assisted Solution

I am getting close with the following I have made

class ProbGenerator
{
private List<Int32> values;
private Int32 maxValue;
private StringBuilder sb1;

public ProbGenerator(Int32 _maxValuem, params Int32[] inValues)
{
values = new List<int>(inValues);
maxValue = _maxValuem;
sb1 = new StringBuilder();
}
public void Start()
{
for (int i = 0; i < values.Count; i++)
{
sb1.AppendLine();
int Total = values[i];
sb1.Append(values[i].ToString() + ":");
for (int j = 0; j < values.Count; j++)
{
if (j != i)
{
Total += values[j];
if ((Total) <= maxValue)
{
sb1.Append(values[j].ToString() + ":");
}
else
{
Total -= values[j];
}
}
}
}
Console.WriteLine(sb1.ToString());
}
}
0

LVL 20

Expert Comment

I started like this

ProbGenerator p1 = new ProbGenerator(14500, 1000, 2000, 3000, 4000, 10000);
p1.Start();

And the output is like this

1000:2000:3000:4000
2000:1000:3000:4000
3000:1000:2000:4000
10000:1000:2000
0

Author Comment

Hi surajguptha:

can you send me a sample code to do the above suggested solution.

anagm
0

LVL 20

Expert Comment

To get all possibilities though it would have to get much more complicated as i could also have like you said 1,3 and 5. BUT if it had bypassed 1000, and 2000 and that last run it would have seen that it could use 3000, and then seen it can use 4000.  hmmm. good question this one lol

Andrew :-)
0

LVL 22

Expert Comment

Thanks anagm - This was an interesting problem! Here is one solution.

{
private string mName = string.Empty;
private int mValue = 0;
private bool mBlocked = false;
public bool Blocked
{
get
{
return mBlocked;
}
set
{
mBlocked = value;
}
}
public string Name
{
get
{
return mName;
}
set
{
mName = value;
}
}
public int Value
{
get
{
return mValue;
}
set
{
mValue = value;
}
}
{
mName = name;
mValue = value;
}
}

Then in the form class:

{
StringBuilder sb = new StringBuilder(1024);

// First, sort by value

// Next, skip any individuals greater than the budget
int topIndex = 0;
while ((topIndex < ordered.Count) && (ordered[topIndex].Value > budget))
topIndex++;

// If all are out of reach, let them know.
if (topIndex >= ordered.Count)
return "You cannot afford any of the gifts in the list.";

// Next, generate all distinct combinations of individuals where the total
// value is less than the max.
int currentTotal;
int currentIndex;
bool skippedOne = true;
int firstSkipped = -1;
int firstBlocked = -1;
bool blockedOne = false;
while ((topIndex < ordered.Count) && (skippedOne || blockedOne))
{
currentIndex = topIndex + 1;
firstSkipped = -1;
currentTotal = ordered[topIndex].Value;
sb.Append(ordered[topIndex].Name + ":");
while (currentIndex < ordered.Count)
{
if (!ordered[currentIndex].Blocked)
{

if ((currentTotal + ordered[currentIndex].Value) < budget)
{
currentTotal += ordered[currentIndex].Value;
sb.Append(ordered[currentIndex].Name + ":");
}
else
{
if (firstSkipped == -1)
firstSkipped = currentIndex;
}
}
currentIndex++;
}
sb.Append("\n");  // Finished one
skippedOne = (firstSkipped > -1);
blockedOne = (firstBlocked > -1);
{
if ((firstSkipped - 1) == topIndex) // Set completed - next pass start with the next one down
{
topIndex++;
blockedOne = false;
skippedOne = false;
firstBlocked = -1;
}
else // Block the one prior to the skip
{
ordered[firstSkipped - 1].Blocked = true;
blockedOne = true;
}
}
else // None were skipped
{
// If any were blocked, move the block start up one and clear blocking flags
if (blockedOne)
{
if ((firstBlocked - 1) == topIndex)
{
topIndex++;
blockedOne = false;
firstBlocked = -1;
}
else // block the next one up and clear the blocked flags below that
{
firstBlocked--;
for (int i = firstBlocked + 1; i < ordered.Count; i++)
ordered[i].Blocked = false;
}
}
else // None skipped or blocked
topIndex++;
}
}
return sb.ToString();
}
{
bool inserted = false;
int index = 1;
int orderedIndex = 0;

{
inserted = false;
orderedIndex = 0;
while (!inserted && orderedIndex < orderedList.Count)
{
if (current.Value > orderedList[orderedIndex].Value)
{
orderedList.Insert(orderedIndex, current);
inserted = true;
}
orderedIndex++;
}
if (!inserted)
}
return orderedList;
}
private void Test_Click(object sender, EventArgs e)
{
}

Clicking the button produced the following:

Jim
0

LVL 22

Assisted Solution

After lookig at this a bit more, I noticed that my main method skipped one combination. Here's the corrected method.

Jim

{
StringBuilder sb = new StringBuilder(1024);

// First, sort by value

// Next, skip any individuals greater than the budget
int topIndex = 0;
while ((topIndex < ordered.Count) && (ordered[topIndex].Value > budget))
topIndex++;

// If all are out of reach, let them know.
if (topIndex >= ordered.Count)
return "You cannot afford any of the gifts in the list.";

// Next, generate all distinct combinations of individuals where the total
// value is less than the max.
int currentTotal;
int currentIndex;
bool skippedOne = true;
int firstSkipped = -1;
int firstBlocked = -1;
bool blockedOne = false;
while ((topIndex < ordered.Count) && (skippedOne || blockedOne))
{
currentTotal = ordered[topIndex].Value;
currentIndex = topIndex + 1;
// Need to block any at the top of the list that cannot be added
while ((currentTotal + ordered[currentIndex].Value) > budget)
{
if (firstBlocked == -1)
firstBlocked = currentIndex;
blockedOne = true;
ordered[currentIndex++].Blocked = true;
}
firstSkipped = -1;
sb.Append(ordered[topIndex].Name + ":");
while (currentIndex < ordered.Count)
{
if (!ordered[currentIndex].Blocked)
{

if ((currentTotal + ordered[currentIndex].Value) <= budget)
{
currentTotal += ordered[currentIndex].Value;
sb.Append(ordered[currentIndex].Name + ":");
}
else
{
if (firstSkipped == -1)
firstSkipped = currentIndex;
}
}
currentIndex++;
}
sb.Append(" Total for set = " + currentTotal + "\n");  // Finished one
skippedOne = (firstSkipped > -1);
blockedOne = (firstBlocked > -1);
{
if ((firstSkipped - 1) == topIndex) // Set completed - next pass start with the next one down
{
topIndex++;
blockedOne = false;
firstBlocked = -1;
for (int i = firstBlocked + 1; i < ordered.Count; i++)
ordered[i].Blocked = false;
}
else // Block the one prior to the skip
{
if (firstBlocked == -1)
{
ordered[firstSkipped - 1].Blocked = true;
firstBlocked = firstSkipped - 1;
}
else // Block the next one down that's not blocked
{
currentIndex = firstBlocked;
while (ordered[currentIndex].Blocked)
currentIndex++;
ordered[currentIndex].Blocked = true;
}
blockedOne = true;
}
}
else // None were skipped
{
// If any were blocked, move the block start up one and clear blocking flags
if (blockedOne)
{
if ((firstBlocked - 1) == topIndex)
{
topIndex++;
skippedOne = true; // This keeps us in the main loop for another try.
blockedOne = false;
firstBlocked = -1;
}
else // block the next one up and clear the blocked flags below that
{
firstBlocked--;
ordered[firstBlocked].Blocked = true;
}
for (int i = firstBlocked + 1; i < ordered.Count; i++)
ordered[i].Blocked = false;
}
else // None skipped or blocked
topIndex++;
}
}
return sb.ToString();
}
0

LVL 1

Expert Comment

Forced accept.

Computer101
0

## Featured Post

### Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…