Alphatron
Alphatron

Reputation: 401

Use an array to display a unique set of values

I'm doing a homework problem where I need to write an application that inputs five numbers, each of which is between 10 and 100, inclusive. As each number is read, display it only if it is not a duplicate of a number already read. Provide for the worst case, in which all five numbers are different. Use the smallest array possible in order to solve this problem and display the complete set of unique values after the user inputs each new value.

What I have so far works correctly. Only, I keep getting an unhandled error once the program runs if all five numbers are unique. It tells me that the index goes out of bounds of the array. Only I'm not sure why it does this and I can't see any errors in my for loop.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ten_Seven_Thirteen
{
class Program
{
    static void Main(string[] args)
    {
        const int SIZE = 5;
        int[] nums = new int[SIZE];
        int a;
        int b;
        int c = 0; //c is the amount of numbers currently entered
        int number;
        Console.WriteLine("Input five numbers between 10 and 100 (inclusive): \n");

        for (int count = 0; count < SIZE; count++)
        {

            number = Convert.ToInt32(Console.ReadLine());
            if (number >= 10 && number <= 100) //Verify whether or not the number meets the criteria
            {
                for (a = 0; a < c; a++)
                {

                    // The following if condition checks for duplicate entrees.
                    if (number == nums[a])
                    {
                        Console.WriteLine("Duplicate number.\n");
                        break; //Breaking the loop prevents the duplicate number from entering the array
                    } 

                } // end for

                // if number is not a duplicate, enter it in array
                if (number != nums[a])
                {
                    nums[c++] = number;
                } // end if - not duplicate

                Console.WriteLine("The non-duplicate values are:\n");

                //display array of unique numbers
                for (b = 0; nums[b] != 0 && b < SIZE; b++)
                {
                    Console.WriteLine(nums[b]);

                } // end for loop and display array 
            } // end if - validate and test
            else
                Console.WriteLine("invalid number.");
            Console.WriteLine("\n");

        } // end for - get 5 numbers 



      }
   }
}// end Ten_Seven_Thirteen

Upvotes: 0

Views: 1081

Answers (1)

Millie Smith
Millie Smith

Reputation: 4604

That's because of this code..

for (b = 0; nums[b] != 0 && b < SIZE; b++)
{
    Console.WriteLine(nums[b]);
}

You can't check nums[b] there. You need to figure out a better way to cut it off (I'll leave that to you since it's homework, or you can ask in the comments). b will go to SIZE and then it will check nums[SIZE] and the last index in nums is SIZE - 1, so you get an index out of range exception..

Also, this code won't work because it only prompts you 5 times, regardless of whether or not they are unique. Try a different type of loop.

Also... this is slightly off topic, but since you're learning: Name your variables something meaningful. a, b, c make it cryptic and hard to read. You won't know what you're doing after sleeping. Better naming means you need less comments because the code documents itself. There aren't that many comments in professional code, even though high school teachers tell you there are.

Upvotes: 4

Related Questions