2

So I am currently pretty confused (keep in mind im new to coding). I am currently trying to create a program which creates allows the user to input the amount of numbers they would like to enter in the array (it then creates an array based on that length), asks the user to input the numbers into their desired position. My code currently looks like this:

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

namespace Arrays
{
    class Program
    {
        static void Main(string[] args)
        {
            //Variables
            int[] array1 = new int[0];
            int TotalArray, position, Number;

            //Main Program
            Console.WriteLine("Hello, welcome to creating your own array!");
            Console.WriteLine("How many numbers do you wish to add to the array?: ");
            TotalArray = int.Parse(Console.ReadLine());
            Console.ReadKey();
            {
                Console.WriteLine("What position would you like to add your number to?: ");
                position = int.Parse(Console.ReadLine());
                if (position < TotalArray)
                {
                    Console.WriteLine("What number would you like to add to position " + position);
                    Number = int.Parse(Console.ReadLine());
                    array1[position] = Number;
                    Console.WriteLine("Testing: " + array1[position]);
                    Console.ReadKey();
                }
                else
                {
                    Console.WriteLine("Error! You entered your position higher than your total array!");
                }
            }
        }
    }
}

However, I do not understand how to create an array length based on the users input. I have to tried to do this:

            Console.WriteLine("Hello, welcome to creating your own array!");
            Console.WriteLine("How many numbers do you wish to add to the array?: ");
            TotalArray = int.Parse(Console.ReadLine());
            int i = Convert.ToInt32(TotalArray);
            int[] array1 = new int[i];

But get this error: A local variable or function named 'array1' is already defined in this scope I don't really understand what this piece of code does:

            int i = Convert.ToInt32(TotalArray);
            int[] array1 = new int[i];

However, I saw it mentioned on stackoverflow and thought id try an implement it. I kinda understand the 2nd line but don't really get the whole converting thing.

Any help would greatly be appreciated!

4
  • 2
    Don't declare a new array1. Just create a new array and assign it to the existing reference: array1 = new int[i]; Convert.ToInt32(TotalArray); means "take this string called TotalArray, and try to convert the text in that string into an integer." If the string is "13", that'll return integer 13. If the text is "my cat's breath smells like Fred Flintstone", it'll throw an exception. However, TotalArray is already an integer. You parsed a string from the user on the previous line. So just create new int[TotalArray], and omit i entirely. Commented Sep 25, 2019 at 17:10
  • @EdPlunkett Thanks for the response! That worked! I am currently trying to loop that section where it asks what position you would like to add your number to until the array is full - How would I go about doing this? Commented Sep 25, 2019 at 17:17
  • Do you need to give the user an error if they pick the same position twice? Personally, I'd just have them fill it in from zero to whatever in order, but I assume this is is a class assignment. Commented Sep 25, 2019 at 17:24
  • I'd let them alter fields as many times as they want and leave them to it. They could definitely do some basic input validation though. Commented Sep 25, 2019 at 17:51

2 Answers 2

3

First, you don't need to convert TotalArray to an integer, since you already parsed the user's repsonse as an integer. So omit i. Next, you're declaring a new array1. Instead, just assign a new one to the same reference:

array1 = new int[TotalArray];

As for the loop. Here's the naive version, but if you need to scold the user for picking the same position twice, you'll need to do a lot more work.

int requestCount = TotalArray;

while (requestCount > 0)
{
    requestCount = requestCount - 1;

    Console.WriteLine("What position would you like to add your number to? (0 - " 
        + (TotalArray - 1) + "): ");
    position = int.Parse(Console.ReadLine());
    if (position < TotalArray)
    {
        Console.WriteLine("What number would you like to add to position " + position);
        Number = int.Parse(Console.ReadLine());
        array1[position] = Number;
        Console.WriteLine("Testing: " + array1[position]);
        Console.ReadKey();
    }
    else
    {
        Console.WriteLine("Error! You entered your position higher than your total array!");
    }
}

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

4 Comments

To be honest that code is quite confusing for me. I don't quite understand the whole --requestCount >=0 thing - I think its a bit too complex for me.I thought there was a more basic version. In additon to that, the code doesn't seem to work.Result: Hello, welcome to creating your own array! How many numbers do you wish to add to the array?: 3 What position would you like to add your number to?: 1 What number would you like to add to position 1 3 Testing: 3 0 3 0 What position would you like to add your number to?: 2 Error! You entered your position higher than your total array!
I think I might just create a new post as that would be more appropiate
Please edit your question to show the entire code for the version which you got that error from. Mine isn't doing that. Also, note that arrays in C# count from zero: In an array of three items, the valid indexes are 0, 1, and 2. 3 is out of range.
That simplified version makes much more sense now! I can't believe I didn't think of that - Such a simple thing, thanks. It works!
3

first this is the right way to write your code

Console.WriteLine("Hello, welcome to creating your own array!");
Console.WriteLine("How many numbers do you wish to add to the array?: ");
TotalArray = int.Parse(Console.ReadLine());
array1 = new int[TotalArray];

the third line means read the last line entered into the console then convert it to a value of type int, then store this value in the variable TotalArray

while the fourth line means construct a new array of length TotalArray then store the resulted array into the variable array1

this doesn't work when you write int[] array1 = new int[i]; because it means create a new variable called array1 then create a new array of length i to be stored in the newly created variable array1

and as you can see in your code you've already defined array1

here:

//Variables
int[] array1 = new int[0];

this is why you get the message: A local variable or function named 'array1' is already defined in this scope

but good job for a beginner.

2 Comments

Thank you for explaining the whole thing to me, Much appreciated!
Good explanation, Mostafa.

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.