How do you realign array data in comparison to another array when the index is out of sync?
I've created a "Choose your Case" game using a cash prize array of all the prize amounts and the case numbers a user can pick. When they pick a number, it tells the user what is in their case. However, at the end it spits out all the cases and what was in each one, and the data is misaligned by one index.
static void Main(string args)
{
int cashPrizeArray = new int[26] { 0, 1, 2, 5, 10, 20, 50, 100, 150, 200, 250, 500, 750, 1000, 2000, 3000, 4000, 5000, 10000, 15000, 20000, 25000, 50000, 75000, 100000, 200000 };
int caseArray = new int[26] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 };
Array.Sort(cashPrizeArray);
Random rnd = new Random();
int cashPrizeArrayR = cashPrizeArray.OrderBy(x => rnd.Next()).ToArray();
foreach (int i in cashPrizeArrayR)
{
Console.Write("{0} ", i);
}
Console.WriteLine("n(please ignore the numbers above)nnnDeal or Not!");
Console.Write("Choose a case: 1-26: ");
int userCase = int.Parse(Console.ReadLine());
if (!caseArray.Contains(userCase))
{
Console.WriteLine("nUnexpected input text.nThis application will now be terminated.nPress ENTER to continue...");
Console.ReadLine();
Environment.Exit(0);
}
else
{
Console.WriteLine("You chose case " + userCase);
Console.ReadLine();
}
Console.WriteLine("This case contains...n$" + cashPrizeArrayR[userCase]);
Console.ReadLine();
Console.WriteLine("nFor reference, below are the case numbers and their values: ");
Console.ReadLine();
var caseAndPrize = cashPrizeArrayR.Zip(caseArray, (p, c) => new { Prize = p, Case = c });
foreach (var pc in caseAndPrize)
{
Console.WriteLine("Case " + pc.Case + " $" + (pc.Prize));
}
Console.ReadLine();
}
It runs fine, but it outputs the value incorrectly, as if the data column is shifted downwards. Can anyone provide a solution where I have gone wrong? Many thanks.
c# arrays if-statement indexing
add a comment |
I've created a "Choose your Case" game using a cash prize array of all the prize amounts and the case numbers a user can pick. When they pick a number, it tells the user what is in their case. However, at the end it spits out all the cases and what was in each one, and the data is misaligned by one index.
static void Main(string args)
{
int cashPrizeArray = new int[26] { 0, 1, 2, 5, 10, 20, 50, 100, 150, 200, 250, 500, 750, 1000, 2000, 3000, 4000, 5000, 10000, 15000, 20000, 25000, 50000, 75000, 100000, 200000 };
int caseArray = new int[26] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 };
Array.Sort(cashPrizeArray);
Random rnd = new Random();
int cashPrizeArrayR = cashPrizeArray.OrderBy(x => rnd.Next()).ToArray();
foreach (int i in cashPrizeArrayR)
{
Console.Write("{0} ", i);
}
Console.WriteLine("n(please ignore the numbers above)nnnDeal or Not!");
Console.Write("Choose a case: 1-26: ");
int userCase = int.Parse(Console.ReadLine());
if (!caseArray.Contains(userCase))
{
Console.WriteLine("nUnexpected input text.nThis application will now be terminated.nPress ENTER to continue...");
Console.ReadLine();
Environment.Exit(0);
}
else
{
Console.WriteLine("You chose case " + userCase);
Console.ReadLine();
}
Console.WriteLine("This case contains...n$" + cashPrizeArrayR[userCase]);
Console.ReadLine();
Console.WriteLine("nFor reference, below are the case numbers and their values: ");
Console.ReadLine();
var caseAndPrize = cashPrizeArrayR.Zip(caseArray, (p, c) => new { Prize = p, Case = c });
foreach (var pc in caseAndPrize)
{
Console.WriteLine("Case " + pc.Case + " $" + (pc.Prize));
}
Console.ReadLine();
}
It runs fine, but it outputs the value incorrectly, as if the data column is shifted downwards. Can anyone provide a solution where I have gone wrong? Many thanks.
c# arrays if-statement indexing
the index is zero based
– JohnB
Nov 23 '18 at 3:29
You might want to look into using classes or structs, to bring the values together into one array, then you won't have a chance of anything going out of sync since you're moving all the data that belongs together, together.
– Lasse Vågsæther Karlsen
Nov 23 '18 at 8:03
add a comment |
I've created a "Choose your Case" game using a cash prize array of all the prize amounts and the case numbers a user can pick. When they pick a number, it tells the user what is in their case. However, at the end it spits out all the cases and what was in each one, and the data is misaligned by one index.
static void Main(string args)
{
int cashPrizeArray = new int[26] { 0, 1, 2, 5, 10, 20, 50, 100, 150, 200, 250, 500, 750, 1000, 2000, 3000, 4000, 5000, 10000, 15000, 20000, 25000, 50000, 75000, 100000, 200000 };
int caseArray = new int[26] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 };
Array.Sort(cashPrizeArray);
Random rnd = new Random();
int cashPrizeArrayR = cashPrizeArray.OrderBy(x => rnd.Next()).ToArray();
foreach (int i in cashPrizeArrayR)
{
Console.Write("{0} ", i);
}
Console.WriteLine("n(please ignore the numbers above)nnnDeal or Not!");
Console.Write("Choose a case: 1-26: ");
int userCase = int.Parse(Console.ReadLine());
if (!caseArray.Contains(userCase))
{
Console.WriteLine("nUnexpected input text.nThis application will now be terminated.nPress ENTER to continue...");
Console.ReadLine();
Environment.Exit(0);
}
else
{
Console.WriteLine("You chose case " + userCase);
Console.ReadLine();
}
Console.WriteLine("This case contains...n$" + cashPrizeArrayR[userCase]);
Console.ReadLine();
Console.WriteLine("nFor reference, below are the case numbers and their values: ");
Console.ReadLine();
var caseAndPrize = cashPrizeArrayR.Zip(caseArray, (p, c) => new { Prize = p, Case = c });
foreach (var pc in caseAndPrize)
{
Console.WriteLine("Case " + pc.Case + " $" + (pc.Prize));
}
Console.ReadLine();
}
It runs fine, but it outputs the value incorrectly, as if the data column is shifted downwards. Can anyone provide a solution where I have gone wrong? Many thanks.
c# arrays if-statement indexing
I've created a "Choose your Case" game using a cash prize array of all the prize amounts and the case numbers a user can pick. When they pick a number, it tells the user what is in their case. However, at the end it spits out all the cases and what was in each one, and the data is misaligned by one index.
static void Main(string args)
{
int cashPrizeArray = new int[26] { 0, 1, 2, 5, 10, 20, 50, 100, 150, 200, 250, 500, 750, 1000, 2000, 3000, 4000, 5000, 10000, 15000, 20000, 25000, 50000, 75000, 100000, 200000 };
int caseArray = new int[26] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 };
Array.Sort(cashPrizeArray);
Random rnd = new Random();
int cashPrizeArrayR = cashPrizeArray.OrderBy(x => rnd.Next()).ToArray();
foreach (int i in cashPrizeArrayR)
{
Console.Write("{0} ", i);
}
Console.WriteLine("n(please ignore the numbers above)nnnDeal or Not!");
Console.Write("Choose a case: 1-26: ");
int userCase = int.Parse(Console.ReadLine());
if (!caseArray.Contains(userCase))
{
Console.WriteLine("nUnexpected input text.nThis application will now be terminated.nPress ENTER to continue...");
Console.ReadLine();
Environment.Exit(0);
}
else
{
Console.WriteLine("You chose case " + userCase);
Console.ReadLine();
}
Console.WriteLine("This case contains...n$" + cashPrizeArrayR[userCase]);
Console.ReadLine();
Console.WriteLine("nFor reference, below are the case numbers and their values: ");
Console.ReadLine();
var caseAndPrize = cashPrizeArrayR.Zip(caseArray, (p, c) => new { Prize = p, Case = c });
foreach (var pc in caseAndPrize)
{
Console.WriteLine("Case " + pc.Case + " $" + (pc.Prize));
}
Console.ReadLine();
}
It runs fine, but it outputs the value incorrectly, as if the data column is shifted downwards. Can anyone provide a solution where I have gone wrong? Many thanks.
c# arrays if-statement indexing
c# arrays if-statement indexing
edited Nov 23 '18 at 8:00
JohnB
1,7501117
1,7501117
asked Nov 23 '18 at 3:21
user3521826
201137
201137
the index is zero based
– JohnB
Nov 23 '18 at 3:29
You might want to look into using classes or structs, to bring the values together into one array, then you won't have a chance of anything going out of sync since you're moving all the data that belongs together, together.
– Lasse Vågsæther Karlsen
Nov 23 '18 at 8:03
add a comment |
the index is zero based
– JohnB
Nov 23 '18 at 3:29
You might want to look into using classes or structs, to bring the values together into one array, then you won't have a chance of anything going out of sync since you're moving all the data that belongs together, together.
– Lasse Vågsæther Karlsen
Nov 23 '18 at 8:03
the index is zero based
– JohnB
Nov 23 '18 at 3:29
the index is zero based
– JohnB
Nov 23 '18 at 3:29
You might want to look into using classes or structs, to bring the values together into one array, then you won't have a chance of anything going out of sync since you're moving all the data that belongs together, together.
– Lasse Vågsæther Karlsen
Nov 23 '18 at 8:03
You might want to look into using classes or structs, to bring the values together into one array, then you won't have a chance of anything going out of sync since you're moving all the data that belongs together, together.
– Lasse Vågsæther Karlsen
Nov 23 '18 at 8:03
add a comment |
2 Answers
2
active
oldest
votes
cashPrizeArrayR[userCase] selects based on index. Have you considered to use instead of int a KeyValuePair or an Dictionary?
add a comment |
change this line:
Console.WriteLine("This case contains...n$" + cashPrizeArrayR[userCase]);
to this:
Console.WriteLine("This case contains...n$" + cashPrizeArrayR[userCase - 1]);
further suggestion:
replace this:
int userCase = int.Parse(Console.ReadLine());
with
bool validKey = int.TryParse(Console.ReadLine(), out var userCase);
if (!validKey|| !caseArray.Contains(userCase))
{
// <snipped>
the index is zero based so you need to account for that.
– JohnB
Nov 23 '18 at 3:35
you should also harden up this line:int userCase = int.Parse(Console.ReadLine());
as it will throw an exception if the user simply presses the enter key
– JohnB
Nov 23 '18 at 3:38
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f53440284%2fhow-do-you-realign-array-data-in-comparison-to-another-array-when-the-index-is-o%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
cashPrizeArrayR[userCase] selects based on index. Have you considered to use instead of int a KeyValuePair or an Dictionary?
add a comment |
cashPrizeArrayR[userCase] selects based on index. Have you considered to use instead of int a KeyValuePair or an Dictionary?
add a comment |
cashPrizeArrayR[userCase] selects based on index. Have you considered to use instead of int a KeyValuePair or an Dictionary?
cashPrizeArrayR[userCase] selects based on index. Have you considered to use instead of int a KeyValuePair or an Dictionary?
answered Nov 23 '18 at 3:34
BolandT
245
245
add a comment |
add a comment |
change this line:
Console.WriteLine("This case contains...n$" + cashPrizeArrayR[userCase]);
to this:
Console.WriteLine("This case contains...n$" + cashPrizeArrayR[userCase - 1]);
further suggestion:
replace this:
int userCase = int.Parse(Console.ReadLine());
with
bool validKey = int.TryParse(Console.ReadLine(), out var userCase);
if (!validKey|| !caseArray.Contains(userCase))
{
// <snipped>
the index is zero based so you need to account for that.
– JohnB
Nov 23 '18 at 3:35
you should also harden up this line:int userCase = int.Parse(Console.ReadLine());
as it will throw an exception if the user simply presses the enter key
– JohnB
Nov 23 '18 at 3:38
add a comment |
change this line:
Console.WriteLine("This case contains...n$" + cashPrizeArrayR[userCase]);
to this:
Console.WriteLine("This case contains...n$" + cashPrizeArrayR[userCase - 1]);
further suggestion:
replace this:
int userCase = int.Parse(Console.ReadLine());
with
bool validKey = int.TryParse(Console.ReadLine(), out var userCase);
if (!validKey|| !caseArray.Contains(userCase))
{
// <snipped>
the index is zero based so you need to account for that.
– JohnB
Nov 23 '18 at 3:35
you should also harden up this line:int userCase = int.Parse(Console.ReadLine());
as it will throw an exception if the user simply presses the enter key
– JohnB
Nov 23 '18 at 3:38
add a comment |
change this line:
Console.WriteLine("This case contains...n$" + cashPrizeArrayR[userCase]);
to this:
Console.WriteLine("This case contains...n$" + cashPrizeArrayR[userCase - 1]);
further suggestion:
replace this:
int userCase = int.Parse(Console.ReadLine());
with
bool validKey = int.TryParse(Console.ReadLine(), out var userCase);
if (!validKey|| !caseArray.Contains(userCase))
{
// <snipped>
change this line:
Console.WriteLine("This case contains...n$" + cashPrizeArrayR[userCase]);
to this:
Console.WriteLine("This case contains...n$" + cashPrizeArrayR[userCase - 1]);
further suggestion:
replace this:
int userCase = int.Parse(Console.ReadLine());
with
bool validKey = int.TryParse(Console.ReadLine(), out var userCase);
if (!validKey|| !caseArray.Contains(userCase))
{
// <snipped>
edited Nov 23 '18 at 3:47
answered Nov 23 '18 at 3:33
JohnB
1,7501117
1,7501117
the index is zero based so you need to account for that.
– JohnB
Nov 23 '18 at 3:35
you should also harden up this line:int userCase = int.Parse(Console.ReadLine());
as it will throw an exception if the user simply presses the enter key
– JohnB
Nov 23 '18 at 3:38
add a comment |
the index is zero based so you need to account for that.
– JohnB
Nov 23 '18 at 3:35
you should also harden up this line:int userCase = int.Parse(Console.ReadLine());
as it will throw an exception if the user simply presses the enter key
– JohnB
Nov 23 '18 at 3:38
the index is zero based so you need to account for that.
– JohnB
Nov 23 '18 at 3:35
the index is zero based so you need to account for that.
– JohnB
Nov 23 '18 at 3:35
you should also harden up this line:
int userCase = int.Parse(Console.ReadLine());
as it will throw an exception if the user simply presses the enter key– JohnB
Nov 23 '18 at 3:38
you should also harden up this line:
int userCase = int.Parse(Console.ReadLine());
as it will throw an exception if the user simply presses the enter key– JohnB
Nov 23 '18 at 3:38
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53440284%2fhow-do-you-realign-array-data-in-comparison-to-another-array-when-the-index-is-o%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
the index is zero based
– JohnB
Nov 23 '18 at 3:29
You might want to look into using classes or structs, to bring the values together into one array, then you won't have a chance of anything going out of sync since you're moving all the data that belongs together, together.
– Lasse Vågsæther Karlsen
Nov 23 '18 at 8:03