0

How to divide three into three and remaining number also need to bind. Please find below i have added some logic but it is failed that is not expected value. Please go through for loop. where im doing the wrong. please correct me.

Expected output:

Total Count : 8

int m = 0;    
for (int i = 1; i <=8; i++)
{    
    if (i % 3 == 0)
    {
        Console.WriteLine("Carousel Item");
        for (int j = m; j < i; j++)
        {
            Console.WriteLine("Carousel Card : " + j);
            m++;
        }
    }
}

Expected Output :

Carousel Item
Carousel Card : 0
Carousel Card : 1
Carousel Card : 2
Carousel Item
Carousel Card : 3
Carousel Card : 4
Carousel Card : 5
Carousel Item
Carousel Card : 6
Carousel Card : 7

But my output :

Carousel Item
Carousel Card : 0
Carousel Card : 1
Carousel Card : 2
Carousel Item
Carousel Card : 3
Carousel Card : 4
Carousel Card : 5
Igor
  • 60,821
  • 10
  • 100
  • 175
Hasanshali
  • 61
  • 1
  • 6
  • 2
    Walk through it yourself with the debugger. Now is an excellent time to familiarize yourself with it, if you are not already. – Broots Waymb Sep 24 '20 at 20:05
  • 1
    I recommend you take a step back and learn how to debug your code at run time. Here are a few of the many resources available: [Navigate through code with the Visual Studio debugger](https://learn.microsoft.com/en-us/visualstudio/debugger/navigating-through-code-with-the-debugger), [Learn to debug C# code using Visual Studio](https://learn.microsoft.com/en-us/visualstudio/get-started/csharp/tutorial-debugger), and [Debugging C# Code in Visual Studio | Mosh](https://youtu.be/u-HdLtqEOog). – Igor Sep 24 '20 at 20:05
  • 2
    Your outer loop starts at 1. So it will be divisible by 3 at 3 and 6. Why do you expect it to be divisible by 3 three times? – devlin carnate Sep 24 '20 at 20:07
  • That is the requirement it should be dividable 3 and remaining also need to bind that data – Hasanshali Sep 24 '20 at 20:16
  • 1
    I understand you want three loops. I'm asking why you expect that outer loop to run three times. It never will. There are only 2 numbers divisible by 3 between 1 and 8. – devlin carnate Sep 24 '20 at 20:17
  • If you have logic please provide me or please correct above code im struggling on here – Hasanshali Sep 24 '20 at 20:22
  • 3
    Back to what BrootsWaymb and @Igor said. Use the DEBUGGER, don't rely on someone to do your work for you. If you can't use a debugger properly, you will never be a programmer. – Señor CMasMas Sep 24 '20 at 20:26
  • Change the `8` to a `9` if you want it to run the inner loop one more time. Right now it's only running twice because you're counting from `1` to `8`, and the only numbers in that range where `i % 3 == 0` are `3` and `6`. If you make `9` the max value of `i`, then `9` becomes the third number where that condition is `true`. – Rufus L Sep 24 '20 at 20:55
  • 1
    FYI, you can also get rid of the modulus (`%`) check if you always force `i % 3 == 0` in the `for` loop, by starting it at `3` and always adding `3`. For example: `for (int i = 3; i <= 9; i += 3)` – Rufus L Sep 24 '20 at 21:00

2 Answers2

1

They way that you think about a problem makes a big difference to how you code a solution to it. There are a number of ways to consider this problem and each one leads to a different solution... and different problems. When the solution you have come up with doesn't work it sometimes makes sense to start over with a different way of thinking about the problem.

The solution you have at the moment is essentially:

  • Go through the list item by item
  • After every third item:
    • print group header
    • print the items

The issue, as you've discovered, is that you never print the 3rd set of items because it only has 2 items in it. That 'after every third item' is why. If your list isn't an integer multiple of the set size then you never display the final set.

You can either change your way of thinking or just add an additional condition in there. Instead of just "after every third item" you could also add a check to see if you've reached the end of the list. That way on the last item you output the partial set.

Here are a couple of other ways to approach the problem:

  • Print a header. Display 3 items. Repeat until you run out of items.
  • Count up by 3, display the available items in the range
  • Work from an array (or Enumerable), split it into appropriately-sized groups and work on the groups.

Each of these has issues that you'll have to solve.

There are any number of other ways to think about the problem, each with their own resulting solutions.


Just for the hell of it I threw together a silly, generalized LINQ solution to the third method. This is a suggestion, it's just an example of the kind of thing you can come up with when you try looking at a problem from a different angle.

void PrintCarousel<T>(IEnumerable<T> source, Action<T> printer)
{
    var groups = source.Select((item, index) => (item, index)).GroupBy(r => r.index / 3, r => r.item);
    foreach (var group in groups)
    {
        Console.WriteLine("Carousel Item");
        foreach (var item in group)
            printer(item);
    }
}

And using it to print your 8 numbers:

PrintCarousel(Enumerable.Range(0, 8), i => Console.WriteLine($"Carousel Card : {i}"));

It's a dumb solution to this problem for several reasons. It' needlessly complex for what you're doing, it's expensive in terms of memory and code, it is generic without ever needing to be... and it's more than a little ugly.

Works though.

Corey
  • 15,524
  • 2
  • 35
  • 68
0

Looks like you are trying to solve a similar problem, loot at my sample in the following answer

https://stackoverflow.com/a/64040594/11830900