Listing numerical contents of an array in c











up vote
0
down vote

favorite












I am writing a simple program which takes a list of grades and outputs the passing grades in c and lists all the grades from the list (10 grades).



The function to calculate the amount of passing grades and print them is fine.



Printing the contents of the array using printf is where I am having problems.



This is how I input the array:



int grades[10] = {70, 80, 95, 65, 35, 85, 54, 78, 45, 68};


Currently I am using this (which works):



printf ("These are the grades: %d, %d, %d, %d, %d, %d, %d, %d, %d, %d n", grades[0], grades[1], grades[2], grades[3], grades[4], grades[5], grades[6], grades[7], grades[8], grades[9]);


It lists the contents of the array, but I am sure there has to be a more elegant way to print the list w/o pointing to each element of the array specifically.



Is there a more elegant solution that I am unaware of?



I did search the topics and was unable to find an answer, sorry if this is a duplicate.










share|improve this question
























  • You need to write a function that receives grades as an argument and prints them!
    – Saeid Yazdani
    Nov 10 at 21:31










  • Using a loop springs to mind. And frankly I'm curious how those grades were input without one (otherwise the solution would have been obvious).
    – WhozCraig
    Nov 10 at 21:32















up vote
0
down vote

favorite












I am writing a simple program which takes a list of grades and outputs the passing grades in c and lists all the grades from the list (10 grades).



The function to calculate the amount of passing grades and print them is fine.



Printing the contents of the array using printf is where I am having problems.



This is how I input the array:



int grades[10] = {70, 80, 95, 65, 35, 85, 54, 78, 45, 68};


Currently I am using this (which works):



printf ("These are the grades: %d, %d, %d, %d, %d, %d, %d, %d, %d, %d n", grades[0], grades[1], grades[2], grades[3], grades[4], grades[5], grades[6], grades[7], grades[8], grades[9]);


It lists the contents of the array, but I am sure there has to be a more elegant way to print the list w/o pointing to each element of the array specifically.



Is there a more elegant solution that I am unaware of?



I did search the topics and was unable to find an answer, sorry if this is a duplicate.










share|improve this question
























  • You need to write a function that receives grades as an argument and prints them!
    – Saeid Yazdani
    Nov 10 at 21:31










  • Using a loop springs to mind. And frankly I'm curious how those grades were input without one (otherwise the solution would have been obvious).
    – WhozCraig
    Nov 10 at 21:32













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am writing a simple program which takes a list of grades and outputs the passing grades in c and lists all the grades from the list (10 grades).



The function to calculate the amount of passing grades and print them is fine.



Printing the contents of the array using printf is where I am having problems.



This is how I input the array:



int grades[10] = {70, 80, 95, 65, 35, 85, 54, 78, 45, 68};


Currently I am using this (which works):



printf ("These are the grades: %d, %d, %d, %d, %d, %d, %d, %d, %d, %d n", grades[0], grades[1], grades[2], grades[3], grades[4], grades[5], grades[6], grades[7], grades[8], grades[9]);


It lists the contents of the array, but I am sure there has to be a more elegant way to print the list w/o pointing to each element of the array specifically.



Is there a more elegant solution that I am unaware of?



I did search the topics and was unable to find an answer, sorry if this is a duplicate.










share|improve this question















I am writing a simple program which takes a list of grades and outputs the passing grades in c and lists all the grades from the list (10 grades).



The function to calculate the amount of passing grades and print them is fine.



Printing the contents of the array using printf is where I am having problems.



This is how I input the array:



int grades[10] = {70, 80, 95, 65, 35, 85, 54, 78, 45, 68};


Currently I am using this (which works):



printf ("These are the grades: %d, %d, %d, %d, %d, %d, %d, %d, %d, %d n", grades[0], grades[1], grades[2], grades[3], grades[4], grades[5], grades[6], grades[7], grades[8], grades[9]);


It lists the contents of the array, but I am sure there has to be a more elegant way to print the list w/o pointing to each element of the array specifically.



Is there a more elegant solution that I am unaware of?



I did search the topics and was unable to find an answer, sorry if this is a duplicate.







c arrays numeric






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 21:42

























asked Nov 10 at 21:29









Will McCoy

32




32












  • You need to write a function that receives grades as an argument and prints them!
    – Saeid Yazdani
    Nov 10 at 21:31










  • Using a loop springs to mind. And frankly I'm curious how those grades were input without one (otherwise the solution would have been obvious).
    – WhozCraig
    Nov 10 at 21:32


















  • You need to write a function that receives grades as an argument and prints them!
    – Saeid Yazdani
    Nov 10 at 21:31










  • Using a loop springs to mind. And frankly I'm curious how those grades were input without one (otherwise the solution would have been obvious).
    – WhozCraig
    Nov 10 at 21:32
















You need to write a function that receives grades as an argument and prints them!
– Saeid Yazdani
Nov 10 at 21:31




You need to write a function that receives grades as an argument and prints them!
– Saeid Yazdani
Nov 10 at 21:31












Using a loop springs to mind. And frankly I'm curious how those grades were input without one (otherwise the solution would have been obvious).
– WhozCraig
Nov 10 at 21:32




Using a loop springs to mind. And frankly I'm curious how those grades were input without one (otherwise the solution would have been obvious).
– WhozCraig
Nov 10 at 21:32












2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










Use a control loop (in this case for) to print the arbitrary number of items you're targeting. Given an array of N items to print, the following demonstrate this (and produces your exact desired output):



int grades[N]; // initialized here or filled later

printf("These are the grades: %d", grades[0]);
for (int i = 1; i < N; i++)
printf(", %d", grades[i]);
fputc('n', stdout);


Note that this methodology allows you to under-print the array. You don't have to print everything. For example, suppose you have an array that can hold M items, but only holds N (where 0 <= N <= M holds). Then simply alter the prior algorithm to account for potentially fewer items (including none):



if (N > 0)
{
printf("These are the grades: %d", grades[0]);
for (int i = 1; i < N; i++)
printf(", %d", grades[i]);
fputc('n', stdout);
}


You can find out more about the for-loop here, as well as many other attributes of the C language. Keep that link around; it's worth the bookmark.






share|improve this answer




























    up vote
    0
    down vote













    You need to write a function that receives grades as an argument and prints them!



    void print_array(int* grades, int size) {
    for(int i = 0; i < size; i++) {
    printf("%d", grades[i]);
    }
    }





    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%2f53243593%2flisting-numerical-contents-of-an-array-in-c%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
      0
      down vote



      accepted










      Use a control loop (in this case for) to print the arbitrary number of items you're targeting. Given an array of N items to print, the following demonstrate this (and produces your exact desired output):



      int grades[N]; // initialized here or filled later

      printf("These are the grades: %d", grades[0]);
      for (int i = 1; i < N; i++)
      printf(", %d", grades[i]);
      fputc('n', stdout);


      Note that this methodology allows you to under-print the array. You don't have to print everything. For example, suppose you have an array that can hold M items, but only holds N (where 0 <= N <= M holds). Then simply alter the prior algorithm to account for potentially fewer items (including none):



      if (N > 0)
      {
      printf("These are the grades: %d", grades[0]);
      for (int i = 1; i < N; i++)
      printf(", %d", grades[i]);
      fputc('n', stdout);
      }


      You can find out more about the for-loop here, as well as many other attributes of the C language. Keep that link around; it's worth the bookmark.






      share|improve this answer

























        up vote
        0
        down vote



        accepted










        Use a control loop (in this case for) to print the arbitrary number of items you're targeting. Given an array of N items to print, the following demonstrate this (and produces your exact desired output):



        int grades[N]; // initialized here or filled later

        printf("These are the grades: %d", grades[0]);
        for (int i = 1; i < N; i++)
        printf(", %d", grades[i]);
        fputc('n', stdout);


        Note that this methodology allows you to under-print the array. You don't have to print everything. For example, suppose you have an array that can hold M items, but only holds N (where 0 <= N <= M holds). Then simply alter the prior algorithm to account for potentially fewer items (including none):



        if (N > 0)
        {
        printf("These are the grades: %d", grades[0]);
        for (int i = 1; i < N; i++)
        printf(", %d", grades[i]);
        fputc('n', stdout);
        }


        You can find out more about the for-loop here, as well as many other attributes of the C language. Keep that link around; it's worth the bookmark.






        share|improve this answer























          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          Use a control loop (in this case for) to print the arbitrary number of items you're targeting. Given an array of N items to print, the following demonstrate this (and produces your exact desired output):



          int grades[N]; // initialized here or filled later

          printf("These are the grades: %d", grades[0]);
          for (int i = 1; i < N; i++)
          printf(", %d", grades[i]);
          fputc('n', stdout);


          Note that this methodology allows you to under-print the array. You don't have to print everything. For example, suppose you have an array that can hold M items, but only holds N (where 0 <= N <= M holds). Then simply alter the prior algorithm to account for potentially fewer items (including none):



          if (N > 0)
          {
          printf("These are the grades: %d", grades[0]);
          for (int i = 1; i < N; i++)
          printf(", %d", grades[i]);
          fputc('n', stdout);
          }


          You can find out more about the for-loop here, as well as many other attributes of the C language. Keep that link around; it's worth the bookmark.






          share|improve this answer












          Use a control loop (in this case for) to print the arbitrary number of items you're targeting. Given an array of N items to print, the following demonstrate this (and produces your exact desired output):



          int grades[N]; // initialized here or filled later

          printf("These are the grades: %d", grades[0]);
          for (int i = 1; i < N; i++)
          printf(", %d", grades[i]);
          fputc('n', stdout);


          Note that this methodology allows you to under-print the array. You don't have to print everything. For example, suppose you have an array that can hold M items, but only holds N (where 0 <= N <= M holds). Then simply alter the prior algorithm to account for potentially fewer items (including none):



          if (N > 0)
          {
          printf("These are the grades: %d", grades[0]);
          for (int i = 1; i < N; i++)
          printf(", %d", grades[i]);
          fputc('n', stdout);
          }


          You can find out more about the for-loop here, as well as many other attributes of the C language. Keep that link around; it's worth the bookmark.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 21:48









          WhozCraig

          50.7k858104




          50.7k858104
























              up vote
              0
              down vote













              You need to write a function that receives grades as an argument and prints them!



              void print_array(int* grades, int size) {
              for(int i = 0; i < size; i++) {
              printf("%d", grades[i]);
              }
              }





              share|improve this answer

























                up vote
                0
                down vote













                You need to write a function that receives grades as an argument and prints them!



                void print_array(int* grades, int size) {
                for(int i = 0; i < size; i++) {
                printf("%d", grades[i]);
                }
                }





                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  You need to write a function that receives grades as an argument and prints them!



                  void print_array(int* grades, int size) {
                  for(int i = 0; i < size; i++) {
                  printf("%d", grades[i]);
                  }
                  }





                  share|improve this answer












                  You need to write a function that receives grades as an argument and prints them!



                  void print_array(int* grades, int size) {
                  for(int i = 0; i < size; i++) {
                  printf("%d", grades[i]);
                  }
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 10 at 21:33









                  Saeid Yazdani

                  6,22139132238




                  6,22139132238






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243593%2flisting-numerical-contents-of-an-array-in-c%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

                      Bressuire

                      Vorschmack

                      Quarantine