How to set the last element of an array as a certain number?











up vote
2
down vote

favorite












Say I have an array that sets values for 5 elements. How do I make the last one to print out as 100?



int n[5];
for(i=0; i<5; i++){
printf("Please enter value %d: n",i+1);
scanf(" %d", &n[i]);
}









share|improve this question


















  • 3




    Set your loop condition to i < 4 and manually set n[4] = 100; outside of your loop
    – Govind Parmar
    Nov 19 at 21:11










  • @GovindParmar under int n[5] I'd write n[4]=100;
    – Henry Gibbs
    Nov 19 at 21:15










  • @GovindParmar Anyway you can write that as an answer so I can give your props?
    – Henry Gibbs
    Nov 19 at 21:20










  • @danglingpointer Wouldn't that be out of bounds?
    – Osiris
    Nov 19 at 21:25










  • @danglingpointer looks like a good way to get yourself shot in the foot.
    – SergeyA
    Nov 19 at 21:31















up vote
2
down vote

favorite












Say I have an array that sets values for 5 elements. How do I make the last one to print out as 100?



int n[5];
for(i=0; i<5; i++){
printf("Please enter value %d: n",i+1);
scanf(" %d", &n[i]);
}









share|improve this question


















  • 3




    Set your loop condition to i < 4 and manually set n[4] = 100; outside of your loop
    – Govind Parmar
    Nov 19 at 21:11










  • @GovindParmar under int n[5] I'd write n[4]=100;
    – Henry Gibbs
    Nov 19 at 21:15










  • @GovindParmar Anyway you can write that as an answer so I can give your props?
    – Henry Gibbs
    Nov 19 at 21:20










  • @danglingpointer Wouldn't that be out of bounds?
    – Osiris
    Nov 19 at 21:25










  • @danglingpointer looks like a good way to get yourself shot in the foot.
    – SergeyA
    Nov 19 at 21:31













up vote
2
down vote

favorite









up vote
2
down vote

favorite











Say I have an array that sets values for 5 elements. How do I make the last one to print out as 100?



int n[5];
for(i=0; i<5; i++){
printf("Please enter value %d: n",i+1);
scanf(" %d", &n[i]);
}









share|improve this question













Say I have an array that sets values for 5 elements. How do I make the last one to print out as 100?



int n[5];
for(i=0; i<5; i++){
printf("Please enter value %d: n",i+1);
scanf(" %d", &n[i]);
}






c






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 21:10









Henry Gibbs

224




224








  • 3




    Set your loop condition to i < 4 and manually set n[4] = 100; outside of your loop
    – Govind Parmar
    Nov 19 at 21:11










  • @GovindParmar under int n[5] I'd write n[4]=100;
    – Henry Gibbs
    Nov 19 at 21:15










  • @GovindParmar Anyway you can write that as an answer so I can give your props?
    – Henry Gibbs
    Nov 19 at 21:20










  • @danglingpointer Wouldn't that be out of bounds?
    – Osiris
    Nov 19 at 21:25










  • @danglingpointer looks like a good way to get yourself shot in the foot.
    – SergeyA
    Nov 19 at 21:31














  • 3




    Set your loop condition to i < 4 and manually set n[4] = 100; outside of your loop
    – Govind Parmar
    Nov 19 at 21:11










  • @GovindParmar under int n[5] I'd write n[4]=100;
    – Henry Gibbs
    Nov 19 at 21:15










  • @GovindParmar Anyway you can write that as an answer so I can give your props?
    – Henry Gibbs
    Nov 19 at 21:20










  • @danglingpointer Wouldn't that be out of bounds?
    – Osiris
    Nov 19 at 21:25










  • @danglingpointer looks like a good way to get yourself shot in the foot.
    – SergeyA
    Nov 19 at 21:31








3




3




Set your loop condition to i < 4 and manually set n[4] = 100; outside of your loop
– Govind Parmar
Nov 19 at 21:11




Set your loop condition to i < 4 and manually set n[4] = 100; outside of your loop
– Govind Parmar
Nov 19 at 21:11












@GovindParmar under int n[5] I'd write n[4]=100;
– Henry Gibbs
Nov 19 at 21:15




@GovindParmar under int n[5] I'd write n[4]=100;
– Henry Gibbs
Nov 19 at 21:15












@GovindParmar Anyway you can write that as an answer so I can give your props?
– Henry Gibbs
Nov 19 at 21:20




@GovindParmar Anyway you can write that as an answer so I can give your props?
– Henry Gibbs
Nov 19 at 21:20












@danglingpointer Wouldn't that be out of bounds?
– Osiris
Nov 19 at 21:25




@danglingpointer Wouldn't that be out of bounds?
– Osiris
Nov 19 at 21:25












@danglingpointer looks like a good way to get yourself shot in the foot.
– SergeyA
Nov 19 at 21:31




@danglingpointer looks like a good way to get yourself shot in the foot.
– SergeyA
Nov 19 at 21:31












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










You can rewrite your loop to not scan for the final element in your array, and fill that out with the hard-coded value of 100 in your code:



int n[5];
n[4] = 100;
for(i = 0; i < 4; i++)
{
printf("Please enter value %d: n",i+1);
scanf(" %d", &n[i]);
}


Note that since your for loop no longer asks for a value for n[4] from the user, it doesn't matter whether the line n[4] = 100; comes before or after your loop.






share|improve this answer

















  • 1




    Thank you very much.
    – Henry Gibbs
    Nov 19 at 21:25


















up vote
1
down vote













In general, I would solve this problem like this:



#define ARRAY_SIZE 5

void myFunc()
{
int n[ARRAY_SIZE];
int i;
for (i=0; i<ARRAY_SIZE-1; i++)
{
printf("Please enter value %d: n",i+1);
scanf(" %d", &n[i]);
}
// now, i == ARRAY_SIZE-1
n[i] = 100;
}


This will work for any size array.






share|improve this answer





















    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',
    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%2f53382675%2fhow-to-set-the-last-element-of-an-array-as-a-certain-number%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








    up vote
    1
    down vote



    accepted










    You can rewrite your loop to not scan for the final element in your array, and fill that out with the hard-coded value of 100 in your code:



    int n[5];
    n[4] = 100;
    for(i = 0; i < 4; i++)
    {
    printf("Please enter value %d: n",i+1);
    scanf(" %d", &n[i]);
    }


    Note that since your for loop no longer asks for a value for n[4] from the user, it doesn't matter whether the line n[4] = 100; comes before or after your loop.






    share|improve this answer

















    • 1




      Thank you very much.
      – Henry Gibbs
      Nov 19 at 21:25















    up vote
    1
    down vote



    accepted










    You can rewrite your loop to not scan for the final element in your array, and fill that out with the hard-coded value of 100 in your code:



    int n[5];
    n[4] = 100;
    for(i = 0; i < 4; i++)
    {
    printf("Please enter value %d: n",i+1);
    scanf(" %d", &n[i]);
    }


    Note that since your for loop no longer asks for a value for n[4] from the user, it doesn't matter whether the line n[4] = 100; comes before or after your loop.






    share|improve this answer

















    • 1




      Thank you very much.
      – Henry Gibbs
      Nov 19 at 21:25













    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    You can rewrite your loop to not scan for the final element in your array, and fill that out with the hard-coded value of 100 in your code:



    int n[5];
    n[4] = 100;
    for(i = 0; i < 4; i++)
    {
    printf("Please enter value %d: n",i+1);
    scanf(" %d", &n[i]);
    }


    Note that since your for loop no longer asks for a value for n[4] from the user, it doesn't matter whether the line n[4] = 100; comes before or after your loop.






    share|improve this answer












    You can rewrite your loop to not scan for the final element in your array, and fill that out with the hard-coded value of 100 in your code:



    int n[5];
    n[4] = 100;
    for(i = 0; i < 4; i++)
    {
    printf("Please enter value %d: n",i+1);
    scanf(" %d", &n[i]);
    }


    Note that since your for loop no longer asks for a value for n[4] from the user, it doesn't matter whether the line n[4] = 100; comes before or after your loop.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 19 at 21:22









    Govind Parmar

    6,69653053




    6,69653053








    • 1




      Thank you very much.
      – Henry Gibbs
      Nov 19 at 21:25














    • 1




      Thank you very much.
      – Henry Gibbs
      Nov 19 at 21:25








    1




    1




    Thank you very much.
    – Henry Gibbs
    Nov 19 at 21:25




    Thank you very much.
    – Henry Gibbs
    Nov 19 at 21:25












    up vote
    1
    down vote













    In general, I would solve this problem like this:



    #define ARRAY_SIZE 5

    void myFunc()
    {
    int n[ARRAY_SIZE];
    int i;
    for (i=0; i<ARRAY_SIZE-1; i++)
    {
    printf("Please enter value %d: n",i+1);
    scanf(" %d", &n[i]);
    }
    // now, i == ARRAY_SIZE-1
    n[i] = 100;
    }


    This will work for any size array.






    share|improve this answer

























      up vote
      1
      down vote













      In general, I would solve this problem like this:



      #define ARRAY_SIZE 5

      void myFunc()
      {
      int n[ARRAY_SIZE];
      int i;
      for (i=0; i<ARRAY_SIZE-1; i++)
      {
      printf("Please enter value %d: n",i+1);
      scanf(" %d", &n[i]);
      }
      // now, i == ARRAY_SIZE-1
      n[i] = 100;
      }


      This will work for any size array.






      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        In general, I would solve this problem like this:



        #define ARRAY_SIZE 5

        void myFunc()
        {
        int n[ARRAY_SIZE];
        int i;
        for (i=0; i<ARRAY_SIZE-1; i++)
        {
        printf("Please enter value %d: n",i+1);
        scanf(" %d", &n[i]);
        }
        // now, i == ARRAY_SIZE-1
        n[i] = 100;
        }


        This will work for any size array.






        share|improve this answer












        In general, I would solve this problem like this:



        #define ARRAY_SIZE 5

        void myFunc()
        {
        int n[ARRAY_SIZE];
        int i;
        for (i=0; i<ARRAY_SIZE-1; i++)
        {
        printf("Please enter value %d: n",i+1);
        scanf(" %d", &n[i]);
        }
        // now, i == ARRAY_SIZE-1
        n[i] = 100;
        }


        This will work for any size array.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 21:27









        yano

        1,424720




        1,424720






























            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%2f53382675%2fhow-to-set-the-last-element-of-an-array-as-a-certain-number%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

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python