CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2010
    Posts
    26

    I have question relating to my list<> and switch statement in my code

    I want to thank in advance anyone who's able to help me here. Hi my question is relating to my code for my list and Case . The method that i've used RemoveRange only removes the numbers that i have added to my list at certain indexes. Do i need to use a different method or how do i remove it at randomly without removing it at certain indexes. meaning currently if i curently generate a random index and a random number to remove. current if i generate a random index of 2 and random number to remove of 3. it will remove index 3, 4, 5. how do i randomly say. randomly remove at any position. For exampl.. i have numbers 1,2,3,4,5,6. i have a random count of 3 i want to randomly remove 1, 4, 6 instead of removing at certain index. The next question deals with my case statement. currently i have my switch statement related to the count on my list. If the count is equal to 2 i send it to one of my case statement. What i want to do is have the count related to multiple Case statements. Meaning if I return a count of 2 have my application randomly choose one the multiple case statement that i have set for that count. I don't know how to set this case statment properly or if i should be using some other statement such as if or if my doing my switch statemtent all wrong.. ..

    Here is my code for my question..

    List<int> firstlist = new List<int>();
    Random listRand = new Random();

    // Predicate<int> mypred = new Predicate<int>();

    //We add these number from the textbox to a list
    firstlist.Add(numTxt1);
    firstlist.Add(numTxt2);
    firstlist.Add(numTxt3);
    firstlist.Add(numTxt4);
    firstlist.Add(numTxt5);
    firstlist.Add(numTxt6);

    int size = listRand.Next(0,2);
    int item = listRand.Next(3,5);
    firstlist.RemoveRange(size,item);
    //int size = firstlist.Count;
    //while (size > 0)
    //{

    // size--;
    // int index = (new Random()).Next(0, size);
    // int elem = firstlist[index];
    // firstlist[index] = firstlist[size];

    // MessageBox.Show(elem.ToString());


    //}
    //listRand.Next(1, 3);

    //(4 & (1 + mynumb.Next())) - 1
    int listint01 = 1,
    listint02 = 2,
    listint03 = 3;
    listint01 = firstlist.Count;
    listint02 = firstlist.Count;
    listint03 = firstlist.Count;


    switch (firstlist.Count)
    {
    case 1:
    firstlist[0] = firstlist[0] + 1;
    break;

    //case 1:
    // firstlist[0] = firstlist[0] - 1;
    // break;

    //case 1:
    // firstlist[0] = firstlist[0];
    // break;

    case 2:
    // Add one
    firstlist[0] = firstlist[0] + 1;
    firstlist[1] = firstlist[1] - 1;
    break;

    //case 2:
    // // Add one
    // firstlist[0] = firstlist[0] - 1;
    // firstlist[1] = firstlist[1] + 1;
    // break;

    //case 2:
    // // Add one
    // firstlist[0] = firstlist[0] - 1;
    // firstlist[1] = firstlist[1];
    // break;

    //case 2:
    // // Add one
    // firstlist[0] = firstlist[0] + 1;
    // firstlist[1] = firstlist[1];
    // break;

    case 3:
    // Add one
    firstlist[0] = firstlist[0] + 1;
    firstlist[1] = firstlist[1] - 1;
    firstlist[2] = firstlist[2];
    break;

    //case 3:
    // // Subtract one
    // firstlist[0] = firstlist[0] - 1;
    // firstlist[1] = firstlist[1] + 1;
    // firstlist[2] = firstlist[2];
    // break;

    //case 3:
    // // Leave as is
    // firstlist[0] = firstlist[0];
    // firstlist[1] = firstlist[1];
    // firstlist[2] = firstlist[2] - 1;

    // break;
    //case 3:
    // firstlist[0] = firstlist[0];
    // firstlist[1] = firstlist[1];
    // firstlist[2] = firstlist[2] + 1;
    // break;

    }

  2. #2
    Join Date
    Sep 2010
    Posts
    2

    Re: I have question relating to my list<> and switch statement in my code

    List<int> list = new List<int>();
    Random random = new Random();

    list.Add(1);
    list.Add(2);
    list.Add(3);
    list.Add(4);
    list.Add(5);
    list.Add(6);

    int numberOfElementsToRemove = random.Next(3,5);

    while (numberOfElementsToRemove > 0)
    {

    list.RemoveAt(random.next(list.Count() - 1);

    numberOfElementsToRemove--;
    }

  3. #3
    Join Date
    Sep 2010
    Posts
    2

    Re: I have question relating to my list<> and switch statement in my code

    With a case statement the value in the switch determines which case statement will be the first to execute. It will continue executing each subsequent case until a break statement is encountered.

    Example:
    int i = 3;

    switch(i)
    {
    case 1:
    //some statements

    case 2:
    //some statements

    case 3:
    //some statements

    case 4:
    //some statements

    case 5:
    //some statements
    break;
    case 6:
    //some statements

    }

    In this example the switch parameter equals 3 and the cases for 3, 4, 5 are executed. This is because no break statement is in case 3 and 4. The break is encountered in case 5 so execution stops after that. If i equaled 1 cases 1, 2, 3, 4, 5 would execute.

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: I have question relating to my list<> and switch statement in my code

    Quote Originally Posted by mbower81 View Post
    With a case statement the value in the switch determines which case statement will be the first to execute. It will continue executing each subsequent case until a break statement is encountered.
    In C++, not in C#. This is wrong. C# requires a break in any case statement unless the statement itself is empty. Your code would not even compile (assuming you replace those comments with actually code of course).

  5. #5
    Join Date
    Aug 2010
    Posts
    26

    Re: I have question relating to my list<> and switch statement in my code

    Thanks Mike for helping out with this code..

  6. #6
    Join Date
    Aug 2010
    Posts
    26

    Re: I have question relating to my list<> and switch statement in my code

    I figured out the second part. Here is my code for the second part in case someone needs help with this logic... Thanks..again for all that tried to help.

    int numberpicked = 0;

    if (firstlist.Count == 1)
    numberpicked = listRand.Next(1, 3);
    else if
    (firstlist.Count == 2)
    numberpicked = listRand.Next(4, 7);
    else if
    (firstlist.Count == 3)
    numberpicked = listRand.Next(7, 11);


    switch (numberpicked)
    {
    case 1:
    firstlist[0] = firstlist[0] + 1;

    break;

    case 2:
    firstlist[0] = firstlist[0] - 1;

    break;

    case 3:

    firstlist[0] = firstlist[0];
    break;

    case 4:
    // Add one
    firstlist[0] = firstlist[0] + 1;
    firstlist[1] = firstlist[1] - 1;
    break;

    case 5:
    // Add one
    firstlist[0] = firstlist[0] - 1;
    firstlist[1] = firstlist[1] + 1;
    break;

    case 6:
    // Add one
    firstlist[0] = firstlist[0] - 1;
    firstlist[1] = firstlist[1];
    break;

    case 7:
    // Add one
    firstlist[0] = firstlist[0] + 1;
    firstlist[1] = firstlist[1];
    break;

    case 8:
    // Add one
    firstlist[0] = firstlist[0] + 1;
    firstlist[1] = firstlist[1] - 1;
    firstlist[2] = firstlist[2];
    break;

    case 9:
    // Subtract one
    firstlist[0] = firstlist[0] - 1;
    firstlist[1] = firstlist[1] + 1;
    firstlist[2] = firstlist[2];
    break;

    case 10:
    // Leave as is
    firstlist[0] = firstlist[0];
    firstlist[1] = firstlist[1];
    firstlist[2] = firstlist[2] - 1;
    break;

    case 11:
    firstlist[0] = firstlist[0];
    firstlist[1] = firstlist[1];
    firstlist[2] = firstlist[2] + 1;
    break;

    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured