working on this module https://learn.microsoft.com/en-us/training/modules/csharp-arrays/

Billee Mason 20 Reputation points
2024-08-15T14:02:27.0866667+00:00

Using this code my outcome is below:

int[] inventory = { 200, 450, 700, 175, 250 };
int sum = 0;
int bin = 0;
foreach (int items in inventory)
{
    sum += items;
    bin++;
    Console.WriteLine($"Bin {bin} = {items} items (Running total: {sum})");
}

Console.WriteLine($"We have {sum} items in inventory.");

OUTCOME I get is this: Bin 5 = 250 items (Running total: 1775) We have 1775 items in inventory

The module says it should be this:

Bin 1 = 200 items (Running total: 200) Bin 2 = 450 items (Running total: 650) Bin 3 = 700 items (Running total: 1350) Bin 4 = 175 items (Running total: 1525) Bin 5 = 250 items (Running total: 1775) We have 1775 items in inventory.

I was wondering why I was not able to get the correct answer. I have even copied and pasted the code from the module, but I am still getting the wrong answer.

Viva Learning
Viva Learning
A Microsoft Viva module that provides employees with formal and informal learning.
104 questions
Microsoft Teams
Microsoft Teams
A Microsoft customizable chat-based workspace.
10,307 questions
0 comments No comments
{count} votes

Accepted answer
  1. P a u l 10,736 Reputation points
    2024-08-15T17:19:49.3533333+00:00

    Seems to work fine for me - how are you running the app?User's image

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Billee Mason 20 Reputation points
    2024-08-17T07:34:16.6633333+00:00

    Thanks very much for replying to me.

    It seems to work perfectly today - who knows, yesterday, maybe gremlins were about.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.