2

I am getting the following error. Use of unassigned local variable markduplicate. I don't understand why? This program finds a duplicate in an array. I been trying to figure it out and I feel like im so close. Thanks for the help.

using System;

class duplicate 
{
    static void Main() 
    {
    const int Array_Size = 5;
    int [] number = new int [Array_Size];
    int i;

    for ( i = 0; i < Array_Size; i++) 
    {
        Console.Write("Element " + i + ":    ");
        number[i] = Int32.Parse(Console.ReadLine());
        if (number[i] < 9 || number[i] > 101)
        {
            Console.WriteLine("Enter Number between 10 - 100");
            number[i] = Int32.Parse(Console.ReadLine());
        }
    }

    bool markduplicate;
    for (i = 0; i < Array_Size; i++)
    {
        for (int j = 0; j < Array_Size; j++) 
        {
            if (i != j)
            {
                if (number[j] == number[i])
                {
                    markduplicate = true;
                }
            }

            if (markduplicate != true) 
            {
                Console.WriteLine("Element " + i + "    " + number[i]);
            }
        }
    }
}

}

  • 2
    Because not all paths through the loop assign at value to `markduplicate`. simply initialize it with `false`? – Peter Ritchie Apr 02 '13 at 01:03
  • markDuplicate is not assignemd a value prior to entering the loop. Assign it an initial value of False and you should be fine. – Pieter Geerkens Apr 02 '13 at 01:04
  • 1
    This is an aside, but is there a reason you're declaring i outside of the scope of the loops? – jwrush Apr 02 '13 at 01:05
  • Also, you might want to rework your loops a bit. Suppose you have the numbers 1,1, and 1. You will print out "Element 0 1" twice, then "Element 1 1" twice, then "Element 2 1" twice. I's declare `markduplicate` in the outer loop, and put a `break` statement after setting `markduplicate` in the inner loop. I'd move the printing to the outer loop too, and change the sense of the if clause. – Eric Jablow Apr 02 '13 at 01:05
  • 1
    Please [edit] your question to provide the **exact** error message you're getting, and mark the line that's causing it with a comment. You have that specific information right in front of you; it's much easier if you provide it. :-) – Ken White Apr 02 '13 at 01:05

5 Answers5

4

This is because the C#'s code analyzer detected that there are paths through you program when the markduplicate's value would be referenced before any assignments to it are made. Specifically, this is going to happen during the very first iteration of the nested loop, when both i and j are zero: the if (i != j) block containing the assignment is not going to execute, so the value of the markduplicate is going to get retrieved in the very next if statement.

To fix this problem, change

bool markduplicate;

to

bool markduplicate = false;

in the declaration.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

The compiler thinks it is possible for you to hit the if (markduplicate != true) line before markduplicate has been set.

If you think the compiler is wrong, give it a value when declaring e.g. bool markduplicate = true;. If you analyze and think the compiler is correct, adjust you code accordingly.

Also: if (markduplicate != true) is considered poor style. Use if(!markduplicate) instead.

John3136
  • 28,809
  • 4
  • 51
  • 69
0

You've declared the boolean variable markduplicate but you haven't given it an initial value - the compiler doesn't know if it should be initially true or false.

In this case it's clear you want it to be initially false, so put this:

bool markduplicate = false;

it will now compiled.

Dai
  • 141,631
  • 28
  • 261
  • 374
0

In your code, (i != j) is false before it is true and therefore you are checking the value of your variable which has had nothing assigned to it.

You need to either assign a value to markduplicate at its declaration or you need to make sure that any path that leads to a conditional check on its value has a value assigned to it first.

seangwright
  • 17,245
  • 6
  • 42
  • 54
0

You have to assign markdefault to either true or false in your declaration. Otherwise, if number [j] != number [i], then markdefault will not be assigned and if markduplicate != true cannot be evaluated.

Azmi Kamis
  • 891
  • 5
  • 20