-1

I am a Beginner in C# and I was wondering what I got wrong with this code.

  1. I want the user to input the amount of numbers
  2. Then an array with that amount gets created
  3. Then finally I want to display all the numbers in the array.

The Code:

using System;
using System.Threading;
using System.Collections.Generic;

namespace Console_Project_alpha
{
    
class Program 
{
    
    static void Main(string[] args)
    {
            Console.Write("Enter the amount of numbers: ");
            int amount = Convert.ToInt32(Console.ReadLine());
            
            int[] numbers = new int[amount];
            string nth = "";

            
            
            for( int i = 1; i <= amount ; i++)
            {
                        if(i == 1)
                        {
                            nth = i + "st";    
                        }
                        else if( i == 2)
                        {
                           nth = i + "nd";
                        }
                        else if( i == 3)
                        {
                            nth = i + "rd";   
                        }else{
                            nth = i + "th";
                        }
                
                Console.Write("\nEnter " + nth + " Number:");
                int num = Convert.ToInt32(Console.ReadLine());
               
               for(int j = 0; j <= numbers.Length; j++)
                {
                    numbers[j] = num;
                    
                }
                
            }
            System.Console.WriteLine(numbers);

            

    }

   }
}

Any help is highly appreciated. Thanks in advance

3
  • It would help of you added the problem that you see. However, here for(int j = 0; j <= numbers.Length; j++) you index past the end of the array. The last index is length-1. Commented Oct 5, 2021 at 12:45
  • If you have an array of length N then the indexes go from 0 to N-1. Replace the <= by < in both loop conditions to fix the problem. Commented Oct 5, 2021 at 12:57
  • Thanks! That was the missing part Commented Dec 20, 2021 at 21:26

1 Answer 1

1
  1. In your code you all time overwriting the same value to all index in array.
  2. If you want to display values from array in console just iterate after array

Properly worked example based on your code:

class Program
{

    static void Main(string[] args)
    {
        Console.Write("Enter the amount of numbers: ");
        int amount = Convert.ToInt32(Console.ReadLine());

        int[] numbers = new int[amount];
        string nth = "";
        int index = 0;

        for (int i = 1; i <= amount; i++)
        {
            if (i == 1)
            {
                nth = i + "st";
            }
            else if (i == 2)
            {
                nth = i + "nd";
            }
            else if (i == 3)
            {
                nth = i + "rd";
            }
            else
            {
                nth = i + "th";
            }

            Console.Write("\nEnter " + nth + " Number:");
            int num = Convert.ToInt32(Console.ReadLine());

            numbers[index] = num;
            index++;
        }

        for (int i = 0; i <= numbers.Length - 1; i++)
        {
            Console.WriteLine(numbers[i]);
        }

        Console.ReadLine();
    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

now I come back and look at it.. this was an easy question I wonder, how in the hell did I ask this.. xD Thanks anyways ;)

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.