C# Remove unused index in an array with a declared size
up vote
-2
down vote
favorite
I have an assignment to make a method. The method requires to ask for a user input and store the user input in an Array, the Array initializes with sentinel values until the array is filled with user input.
The method will print all the value that user input.
So far, the program works except when the user does not fill all the array.
It means the array still use the initialized values(sentinel values), It will show a bunch of zeros to the unused index of the array.
For example:
int sample = new int[5]; // max size of array
// user input 3 numbers with loop (for ex. 1, 2, 3 and then exits with 555)
how do I make it display as
1
2
3
instead of
1
2
3
0
0
c# arrays
|
show 1 more comment
up vote
-2
down vote
favorite
I have an assignment to make a method. The method requires to ask for a user input and store the user input in an Array, the Array initializes with sentinel values until the array is filled with user input.
The method will print all the value that user input.
So far, the program works except when the user does not fill all the array.
It means the array still use the initialized values(sentinel values), It will show a bunch of zeros to the unused index of the array.
For example:
int sample = new int[5]; // max size of array
// user input 3 numbers with loop (for ex. 1, 2, 3 and then exits with 555)
how do I make it display as
1
2
3
instead of
1
2
3
0
0
c# arrays
Must you use an array, rather than aList<>
? You could have an array of nullable integers,new int?[5]
.
– Jeppe Stig Nielsen
Nov 12 at 21:46
You can either use a List or you can count the number of inputs and only display up to that count
– Steve
Nov 12 at 21:46
@JeppeStigNielsen i wouldnt suggest an absolute beginner to use nullable types just yet. there are way better ways to solve it.
– Steve
Nov 12 at 21:47
1
I believe homework questions are generally frowned upon, but the reason you get1 2 3 0 0
is because you initialize an array of 5int
values. The default initialized value of an integer is 0 in C#. when you say display are you trying to display just the final value? if so you can loop through the array and build a result variableresult += sample[i].ToString()
andbreak
out of the loop if you hit your sentinel value.
– Andrei
Nov 12 at 21:47
There are 50 different ways you could do it as you are already finding out. If you have to use an array and you don't have to specify a max size to begin with, you can use the Array.Resize method to change the size each time the user enters something valid....then you don't have to worry about erroneous data in the array at all.
– user1011627
Nov 12 at 21:50
|
show 1 more comment
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I have an assignment to make a method. The method requires to ask for a user input and store the user input in an Array, the Array initializes with sentinel values until the array is filled with user input.
The method will print all the value that user input.
So far, the program works except when the user does not fill all the array.
It means the array still use the initialized values(sentinel values), It will show a bunch of zeros to the unused index of the array.
For example:
int sample = new int[5]; // max size of array
// user input 3 numbers with loop (for ex. 1, 2, 3 and then exits with 555)
how do I make it display as
1
2
3
instead of
1
2
3
0
0
c# arrays
I have an assignment to make a method. The method requires to ask for a user input and store the user input in an Array, the Array initializes with sentinel values until the array is filled with user input.
The method will print all the value that user input.
So far, the program works except when the user does not fill all the array.
It means the array still use the initialized values(sentinel values), It will show a bunch of zeros to the unused index of the array.
For example:
int sample = new int[5]; // max size of array
// user input 3 numbers with loop (for ex. 1, 2, 3 and then exits with 555)
how do I make it display as
1
2
3
instead of
1
2
3
0
0
c# arrays
c# arrays
edited Nov 12 at 22:38
tonzter
233
233
asked Nov 12 at 21:40
otilolito
11
11
Must you use an array, rather than aList<>
? You could have an array of nullable integers,new int?[5]
.
– Jeppe Stig Nielsen
Nov 12 at 21:46
You can either use a List or you can count the number of inputs and only display up to that count
– Steve
Nov 12 at 21:46
@JeppeStigNielsen i wouldnt suggest an absolute beginner to use nullable types just yet. there are way better ways to solve it.
– Steve
Nov 12 at 21:47
1
I believe homework questions are generally frowned upon, but the reason you get1 2 3 0 0
is because you initialize an array of 5int
values. The default initialized value of an integer is 0 in C#. when you say display are you trying to display just the final value? if so you can loop through the array and build a result variableresult += sample[i].ToString()
andbreak
out of the loop if you hit your sentinel value.
– Andrei
Nov 12 at 21:47
There are 50 different ways you could do it as you are already finding out. If you have to use an array and you don't have to specify a max size to begin with, you can use the Array.Resize method to change the size each time the user enters something valid....then you don't have to worry about erroneous data in the array at all.
– user1011627
Nov 12 at 21:50
|
show 1 more comment
Must you use an array, rather than aList<>
? You could have an array of nullable integers,new int?[5]
.
– Jeppe Stig Nielsen
Nov 12 at 21:46
You can either use a List or you can count the number of inputs and only display up to that count
– Steve
Nov 12 at 21:46
@JeppeStigNielsen i wouldnt suggest an absolute beginner to use nullable types just yet. there are way better ways to solve it.
– Steve
Nov 12 at 21:47
1
I believe homework questions are generally frowned upon, but the reason you get1 2 3 0 0
is because you initialize an array of 5int
values. The default initialized value of an integer is 0 in C#. when you say display are you trying to display just the final value? if so you can loop through the array and build a result variableresult += sample[i].ToString()
andbreak
out of the loop if you hit your sentinel value.
– Andrei
Nov 12 at 21:47
There are 50 different ways you could do it as you are already finding out. If you have to use an array and you don't have to specify a max size to begin with, you can use the Array.Resize method to change the size each time the user enters something valid....then you don't have to worry about erroneous data in the array at all.
– user1011627
Nov 12 at 21:50
Must you use an array, rather than a
List<>
? You could have an array of nullable integers, new int?[5]
.– Jeppe Stig Nielsen
Nov 12 at 21:46
Must you use an array, rather than a
List<>
? You could have an array of nullable integers, new int?[5]
.– Jeppe Stig Nielsen
Nov 12 at 21:46
You can either use a List or you can count the number of inputs and only display up to that count
– Steve
Nov 12 at 21:46
You can either use a List or you can count the number of inputs and only display up to that count
– Steve
Nov 12 at 21:46
@JeppeStigNielsen i wouldnt suggest an absolute beginner to use nullable types just yet. there are way better ways to solve it.
– Steve
Nov 12 at 21:47
@JeppeStigNielsen i wouldnt suggest an absolute beginner to use nullable types just yet. there are way better ways to solve it.
– Steve
Nov 12 at 21:47
1
1
I believe homework questions are generally frowned upon, but the reason you get
1 2 3 0 0
is because you initialize an array of 5 int
values. The default initialized value of an integer is 0 in C#. when you say display are you trying to display just the final value? if so you can loop through the array and build a result variable result += sample[i].ToString()
and break
out of the loop if you hit your sentinel value.– Andrei
Nov 12 at 21:47
I believe homework questions are generally frowned upon, but the reason you get
1 2 3 0 0
is because you initialize an array of 5 int
values. The default initialized value of an integer is 0 in C#. when you say display are you trying to display just the final value? if so you can loop through the array and build a result variable result += sample[i].ToString()
and break
out of the loop if you hit your sentinel value.– Andrei
Nov 12 at 21:47
There are 50 different ways you could do it as you are already finding out. If you have to use an array and you don't have to specify a max size to begin with, you can use the Array.Resize method to change the size each time the user enters something valid....then you don't have to worry about erroneous data in the array at all.
– user1011627
Nov 12 at 21:50
There are 50 different ways you could do it as you are already finding out. If you have to use an array and you don't have to specify a max size to begin with, you can use the Array.Resize method to change the size each time the user enters something valid....then you don't have to worry about erroneous data in the array at all.
– user1011627
Nov 12 at 21:50
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
0
down vote
When you create a new array int sample = new int[5];
it will populate it with zeros if you don't define a default value to initialise it.
The other problem is that if you define an array of 5 elements and user continues to insert data, you won't be able to store that data.
You are better to work with List as you don't need an initial size and you can insert data into it without limite.
List<int> usersInts = new List<int>();
userInts.Add(//userInputsHere);
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
When you create a new array int sample = new int[5];
it will populate it with zeros if you don't define a default value to initialise it.
The other problem is that if you define an array of 5 elements and user continues to insert data, you won't be able to store that data.
You are better to work with List as you don't need an initial size and you can insert data into it without limite.
List<int> usersInts = new List<int>();
userInts.Add(//userInputsHere);
add a comment |
up vote
0
down vote
When you create a new array int sample = new int[5];
it will populate it with zeros if you don't define a default value to initialise it.
The other problem is that if you define an array of 5 elements and user continues to insert data, you won't be able to store that data.
You are better to work with List as you don't need an initial size and you can insert data into it without limite.
List<int> usersInts = new List<int>();
userInts.Add(//userInputsHere);
add a comment |
up vote
0
down vote
up vote
0
down vote
When you create a new array int sample = new int[5];
it will populate it with zeros if you don't define a default value to initialise it.
The other problem is that if you define an array of 5 elements and user continues to insert data, you won't be able to store that data.
You are better to work with List as you don't need an initial size and you can insert data into it without limite.
List<int> usersInts = new List<int>();
userInts.Add(//userInputsHere);
When you create a new array int sample = new int[5];
it will populate it with zeros if you don't define a default value to initialise it.
The other problem is that if you define an array of 5 elements and user continues to insert data, you won't be able to store that data.
You are better to work with List as you don't need an initial size and you can insert data into it without limite.
List<int> usersInts = new List<int>();
userInts.Add(//userInputsHere);
answered Nov 12 at 21:51
YouneS
1576
1576
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270506%2fc-sharp-remove-unused-index-in-an-array-with-a-declared-size%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Must you use an array, rather than a
List<>
? You could have an array of nullable integers,new int?[5]
.– Jeppe Stig Nielsen
Nov 12 at 21:46
You can either use a List or you can count the number of inputs and only display up to that count
– Steve
Nov 12 at 21:46
@JeppeStigNielsen i wouldnt suggest an absolute beginner to use nullable types just yet. there are way better ways to solve it.
– Steve
Nov 12 at 21:47
1
I believe homework questions are generally frowned upon, but the reason you get
1 2 3 0 0
is because you initialize an array of 5int
values. The default initialized value of an integer is 0 in C#. when you say display are you trying to display just the final value? if so you can loop through the array and build a result variableresult += sample[i].ToString()
andbreak
out of the loop if you hit your sentinel value.– Andrei
Nov 12 at 21:47
There are 50 different ways you could do it as you are already finding out. If you have to use an array and you don't have to specify a max size to begin with, you can use the Array.Resize method to change the size each time the user enters something valid....then you don't have to worry about erroneous data in the array at all.
– user1011627
Nov 12 at 21:50