0

This question is about a homework problem with C# programming. The goal of the program is the have an array of 20 integers between 0 - 9 and then have another 20 item array that increments (by one) at the index in which the first number 7 appears in the first array. In the code below I have an array called 'values' that generates the random numbers. The second array called 'sevens' has 20 zeros that could be incremented by one if the first 7 in the values array is found at the corresponding index. My issue is that I can't get the second array to increment. The values array correctly generates the numbers, but the sevens array remains all zeros. Any suggestions on how to get the index of 'sevens' to increment were the first 7 is found in 'values'?

class Program
{
    static void Main(string[] args)
    {
        int[] values = new int[20];
        int[] sevens = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0 };

        Random random = new Random();
        int randValue;
        int count = 0;

        //Fills values with random integers between 0-9
        while (count < values.Length)
        {
            randValue = random.Next(10);
            values[count] = randValue;
            count++;
        }

        //Displays the contents of values
        int valuesDisplayCount = 0;
        while (valuesDisplayCount < values.Length)
        {
            Console.WriteLine("Array position {0} is {1}", valuesDisplayCount + 1, values[valuesDisplayCount]);
            valuesDisplayCount++;
        }

        while (valuesDisplayCount < values.Length)
        {
            if(values[valuesDisplayCount] == 7)
            {
                sevens[valuesDisplayCount]++;
                break;
            }
            valuesDisplayCount++;
        }

        //Displays the contents of sevens
        int sevensDisplayCount = 0;
        while (sevensDisplayCount < sevens.Length)
        {
            Console.WriteLine("Number of 7s at position {0}: {1} ", sevensDisplayCount + 1, sevens[sevensDisplayCount]);
            sevensDisplayCount++;
        }
        Console.ReadLine();
    }
}
10
  • 9
    Nothing against you, but sometimes i wish these programming courses/lessons would start with a learning unit about how to use the debugger. Starting to teach programming without knowing the tools necessary to write working code is like a driving school that starts with actual driving first before teaching how to behave in a safe manner in road traffic... Sorry for that rant, nothing you are responsible for; i'll look at your question now ;-) Commented Sep 21, 2018 at 17:51
  • This is a really good problem for learning how to use the Step Debugger Commented Sep 21, 2018 at 17:53
  • It's always a good idea to separate your display from your logic. So don't use valuesDisplayCount when looking for sevens. Commented Sep 21, 2018 at 17:54
  • Side note int[] sevens = new int[20]; would have the same effect. All array indices are automatically initialized to the default value for the data type. In this case it would be all zeros. Commented Sep 21, 2018 at 17:55
  • Like @HereticMonkey said, study how your program uses the valuesDisplayCount variable. You can use the debugger for that, but it should be simple enough that you could also trace manually (with your eyes, perhaps some notes on paper) what value valuesDisplayCount will have at each line/step of your program. Commented Sep 21, 2018 at 17:56

1 Answer 1

1

The problem here is that you are not resetting the variable valuesDisplayCount back to 0 before your execute the second while loop that uses it. Because this variable was just used in the previous loop, it's value is already at values.Length, so your second while loop never runs.

To fix this, just add valuesDisplayCount = 0; before your second while loop that uses that variable.


Probably a better way to handle this is to use a for loop instead, because these loops contain the variable initialization in them by default, do the comparison, and increment the variable all in the beginning of the loop.

So your code would then look like this:

int[] values = new int[20];
int[] sevens = new int[20];

Random random = new Random();

//Fills values with random integers between 0-9
for (int count = 0; count < values.Length; count++)
{
    values[count] = random.Next(10);
}

//Display the contents of 'values'
for (int valuesDisplayCount = 0; valuesDisplayCount < values.Length; valuesDisplayCount++)
{
    Console.WriteLine("Array position {0} is {1}", valuesDisplayCount + 1, 
        values[valuesDisplayCount]);
}

// Increment indexes in 'sevens' whose corresponding index in 'values' is 7
for (int valuesDisplayCount = 0; valuesDisplayCount < values.Length; valuesDisplayCount++)
{
    if (values[valuesDisplayCount] == 7)
    {
        sevens[valuesDisplayCount]++;
        break; // Only increment at the first position, so exit the for loop
    }
}

//Display the contents of 'sevens'
for (int sevensDisplayCount = 0; sevensDisplayCount < sevens.Length; sevensDisplayCount++)
{
    Console.WriteLine("Number of 7s at position {0}: {1} ", sevensDisplayCount + 1,
        sevens[sevensDisplayCount]);
}

Console.ReadLine();
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. This worked much better. I think you are correct about using the for loop. Others have mentioned the fact that the value for the counter is likely not what I expected, so the for loop makes that easier.
Glad it helped. Yeah, declaring the variable in the for loop is nice because it's local to the loop - nothing outside the loop can use or change it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.