-
October 9th, 2015, 08:46 PM
#1
Creating arrays with a method
I am just practicing a few things, trying to get the hang of c#. I have very little experience and am teaching myself so some help on here would be much appreciated! I am trying to create a method that would ask the user to input a number and then place that number into an index on the array it is creating. It is set up to use a "For" loop to set the number for each index. After all the indices are filled, it should print out the whole array. I can't seem to figure out how to get the user's input though. Here is my code, hopefully someone can show me what I am doing wrong!
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace C_Sharp_Practice
{
class Program
{
static void MakeArray()
{
int[] Array = new int[4];
for (int i = 0; i <= Array.Length; i++)
{Console.WriteLine("Please enter a number for this index.");
Array[i] = Convert.ToInt32(Console.ReadLine);
}
Console.WriteLine(Array.Length);
}
static void Main(string[] args)
{
MakeArray();
}
}
}
-
October 9th, 2015, 09:23 PM
#2
Re: Creating arrays with a method
I see several problems with your method. In your 'for' loop, you are exceeding the bounds of the array. You are looping until i <= Array.Length; When you start with i = 0, you should run as long as i < the array length. Also, I would declare a temp string variable that you fill with Console.ReadLine(), then convert that string variable to an int.
When you call Console.WriteLine(Array.Length);, you are just printing out the array length, or '4'.
Developing using:
.NET3.5 / VS 2010
-
October 9th, 2015, 11:33 PM
#3
Re: Creating arrays with a method
Originally Posted by Jim_Auricman
I see several problems with your method. In your 'for' loop, you are exceeding the bounds of the array. You are looping until i <= Array.Length; When you start with i = 0, you should run as long as i < the array length. Also, I would declare a temp string variable that you fill with Console.ReadLine(), then convert that string variable to an int.
When you call Console.WriteLine(Array.Length);, you are just printing out the array length, or '4'.
Ok so I understand the part about printing out the wrong thing. I didn't think that part through very well. I am a little confused about the loop exceeding the bounds of the array. Should it loop until i>4 so that it gets 5 inputs? And I tried making an "input" variable, then converting it and assigning it to the array, but it was not allowing it.
-
October 10th, 2015, 08:14 AM
#4
Re: Creating arrays with a method
Yes, you could pass the array size into the method as a value parameter.
Loop as long as i < the array length, not i<= array length.
To display the contents of the array, start with this line, then think of a way to do it in a more generic way.
Console.WriteLine(Array[0] + ", " + Array[1] + ", " + Array[2] + ", " + Array[3]);
Developing using:
.NET3.5 / VS 2010
-
April 29th, 2016, 12:36 AM
#5
Re: Creating arrays with a method
I don't mean to take away from the main issue at hand but how do I open up a new post. I have an urgent issue I need help with from all you geniuses.
-
April 29th, 2016, 10:14 AM
#6
Re: Creating arrays with a method
Originally Posted by NMDSPARTAN
I don't mean to take away from the main issue at hand but how do I open up a new post. I have an urgent issue I need help with from all you geniuses.
At the top of this thread, you'll see a breadcrumb like: Forum\C# Programming\C-Sharp Programming\Creating arrays with a method
Click on C-Sharp Programming. In the upper left corner, there is an "Open New Thread" button.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|