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();
}
}
valuesDisplayCountwhen looking for sevens.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.valuesDisplayCountvariable. 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 valuevaluesDisplayCountwill have at each line/step of your program.