How do you realign array data in comparison to another array when the index is out of sync?












0














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.



here










share|improve this question
























  • 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
















0














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.



here










share|improve this question
























  • 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














0












0








0







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.



here










share|improve this question















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.



here







c# arrays if-statement indexing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












2 Answers
2






active

oldest

votes


















0














cashPrizeArrayR[userCase] selects based on index. Have you considered to use instead of int a KeyValuePair or an Dictionary?






share|improve this answer





























    0














    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>





    share|improve this answer























    • 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











    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    cashPrizeArrayR[userCase] selects based on index. Have you considered to use instead of int a KeyValuePair or an Dictionary?






    share|improve this answer


























      0














      cashPrizeArrayR[userCase] selects based on index. Have you considered to use instead of int a KeyValuePair or an Dictionary?






      share|improve this answer
























        0












        0








        0






        cashPrizeArrayR[userCase] selects based on index. Have you considered to use instead of int a KeyValuePair or an Dictionary?






        share|improve this answer












        cashPrizeArrayR[userCase] selects based on index. Have you considered to use instead of int a KeyValuePair or an Dictionary?







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 3:34









        BolandT

        245




        245

























            0














            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>





            share|improve this answer























            • 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
















            0














            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>





            share|improve this answer























            • 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














            0












            0








            0






            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>





            share|improve this answer














            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>






            share|improve this answer














            share|improve this answer



            share|improve this answer








            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


















            • 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


















            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            Alexandru Averescu

            Trompette piccolo