[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# Looping through the dataset, vb.net

Posted on 2007-10-12
Medium Priority
756 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
• 3
• 2
• 2
• +2

LVL 21

Expert Comment

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

LVL 21

Accepted Solution

surajguptha earned 672 total points
ID: 20066397
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

REA_ANDREW earned 664 total points
ID: 20066555
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

ID: 20066572
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

ID: 20066576
Hi surajguptha:

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

anagm
0

LVL 20

Expert Comment

ID: 20066587
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

ID: 20068590
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

JimBrandley earned 664 total points
ID: 20071527
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

ID: 20285258
Forced accept.

Computer101
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Screencast - Getting to Know the Pipeline
###### Suggested Courses
Course of the Month18 days, 13 hours left to enroll