C++: Flattening a 2d array into a 1d arraySplitting a text file into paragraphs and wordsFind the most...

Disable the ">" operator in Rstudio linux terminal

Can we use the stored gravitational potential energy of a building to produce power?

How would an AI self awareness kill switch work?

Quenching swords in dragon blood; why?

Why is working on the same position for more than 15 years not a red flag?

Pre-1980's science fiction short story: alien disguised as a woman shot by a gangster, has tentacles coming out of her breasts when remaking her body

What is the time complexity of enqueue and dequeue of a queue implemented with a singly linked list?

Dilemma of explaining to interviewer that he is the reason for declining second interview

Placing an adverb between a verb and an object?

Why did Jodrell Bank assist the Soviet Union to collect data from their spacecraft in the mid 1960's?

(easy?) Matrix with wide blocks

How to deal with an incendiary email that was recalled

En passant for beginner

Why would the Pakistan airspace closure cancel flights not headed to Pakistan itself?

What's the most convenient time of year in the USA to end the world?

Using loops to create tables

If I sold a PS4 game I owned the disc for, can I reinstall it digitally?

How to avoid being sexist when trying to employ someone to function in a very sexist environment?

What is this metal M-shaped device for?

Is a debit card dangerous for an account with low balance and no overdraft protection?

How can I deal with a significant flaw I found in my previous supervisor’s paper?

Jumping Numbers

Could flying insects re-enter the Earth's atmosphere from space without burning up?

Luggage storage for 10 day in Tokyo



C++: Flattening a 2d array into a 1d array


Splitting a text file into paragraphs and wordsFind the most efficient combination that equals target number in ArrayListFinding the count of negative sub-arrays for a given arraySorting and slicing array, based on another “weight” array?Template class to demonstrate array manipulationFill 2D array recursivelyJava 2d array Hourglass Sums challenge from HackerrankgroupArray() - Convert flat array into multidimensional array by groupMapping one array onto another where columns from first array become rows in second array2D array inquiries













0












$begingroup$


I started learning c++ and have a problem with one of my starting exercises.



The exercise involves a regular flattening of some kind: in a function, I need to sum the values of every column in every row of a 2d input array and then return the sum calculated for each row in a newly created 1d array.



I filled a 2d array with randomly generated integers:



int expenses[7][10];

for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 10; j++)
{
expenses[i][j] = rand();
std::cout << expenses[i][j] << " ";
}
std::cout << " n";
}


So far, with the help of this article, I have the following function:



int* sumOfDays(int input[7][10])
{
static int res[7];

for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 10; j++)
{
res[i] += *input[i, j];
}
}
return res;
}


which I would like to send to the standard output like this:



std::cout << "the sum of days is: " << "n";

int* r = sumOfDays(expenses);
for (int i = 0; i < 7; i++)
{
std::cout << i+1 << ". " << *(r + i) << "n";
}


However, I get the following output:



result of the function



What am I missing? I'm sure it is a rooky mistake, but I just can't seem to figure it out.









share









$endgroup$

















    0












    $begingroup$


    I started learning c++ and have a problem with one of my starting exercises.



    The exercise involves a regular flattening of some kind: in a function, I need to sum the values of every column in every row of a 2d input array and then return the sum calculated for each row in a newly created 1d array.



    I filled a 2d array with randomly generated integers:



    int expenses[7][10];

    for (int i = 0; i < 7; i++)
    {
    for (int j = 0; j < 10; j++)
    {
    expenses[i][j] = rand();
    std::cout << expenses[i][j] << " ";
    }
    std::cout << " n";
    }


    So far, with the help of this article, I have the following function:



    int* sumOfDays(int input[7][10])
    {
    static int res[7];

    for (int i = 0; i < 7; i++)
    {
    for (int j = 0; j < 10; j++)
    {
    res[i] += *input[i, j];
    }
    }
    return res;
    }


    which I would like to send to the standard output like this:



    std::cout << "the sum of days is: " << "n";

    int* r = sumOfDays(expenses);
    for (int i = 0; i < 7; i++)
    {
    std::cout << i+1 << ". " << *(r + i) << "n";
    }


    However, I get the following output:



    result of the function



    What am I missing? I'm sure it is a rooky mistake, but I just can't seem to figure it out.









    share









    $endgroup$















      0












      0








      0





      $begingroup$


      I started learning c++ and have a problem with one of my starting exercises.



      The exercise involves a regular flattening of some kind: in a function, I need to sum the values of every column in every row of a 2d input array and then return the sum calculated for each row in a newly created 1d array.



      I filled a 2d array with randomly generated integers:



      int expenses[7][10];

      for (int i = 0; i < 7; i++)
      {
      for (int j = 0; j < 10; j++)
      {
      expenses[i][j] = rand();
      std::cout << expenses[i][j] << " ";
      }
      std::cout << " n";
      }


      So far, with the help of this article, I have the following function:



      int* sumOfDays(int input[7][10])
      {
      static int res[7];

      for (int i = 0; i < 7; i++)
      {
      for (int j = 0; j < 10; j++)
      {
      res[i] += *input[i, j];
      }
      }
      return res;
      }


      which I would like to send to the standard output like this:



      std::cout << "the sum of days is: " << "n";

      int* r = sumOfDays(expenses);
      for (int i = 0; i < 7; i++)
      {
      std::cout << i+1 << ". " << *(r + i) << "n";
      }


      However, I get the following output:



      result of the function



      What am I missing? I'm sure it is a rooky mistake, but I just can't seem to figure it out.









      share









      $endgroup$




      I started learning c++ and have a problem with one of my starting exercises.



      The exercise involves a regular flattening of some kind: in a function, I need to sum the values of every column in every row of a 2d input array and then return the sum calculated for each row in a newly created 1d array.



      I filled a 2d array with randomly generated integers:



      int expenses[7][10];

      for (int i = 0; i < 7; i++)
      {
      for (int j = 0; j < 10; j++)
      {
      expenses[i][j] = rand();
      std::cout << expenses[i][j] << " ";
      }
      std::cout << " n";
      }


      So far, with the help of this article, I have the following function:



      int* sumOfDays(int input[7][10])
      {
      static int res[7];

      for (int i = 0; i < 7; i++)
      {
      for (int j = 0; j < 10; j++)
      {
      res[i] += *input[i, j];
      }
      }
      return res;
      }


      which I would like to send to the standard output like this:



      std::cout << "the sum of days is: " << "n";

      int* r = sumOfDays(expenses);
      for (int i = 0; i < 7; i++)
      {
      std::cout << i+1 << ". " << *(r + i) << "n";
      }


      However, I get the following output:



      result of the function



      What am I missing? I'm sure it is a rooky mistake, but I just can't seem to figure it out.







      c++ array





      share












      share










      share



      share










      asked 6 mins ago









      rTECHrTECH

      1814




      1814






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          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: "196"
          };
          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: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2fcodereview.stackexchange.com%2fquestions%2f214607%2fc-flattening-a-2d-array-into-a-1d-array%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • 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.


          Use MathJax to format equations. MathJax reference.


          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%2fcodereview.stackexchange.com%2fquestions%2f214607%2fc-flattening-a-2d-array-into-a-1d-array%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

          Fairchild Swearingen Metro Inhaltsverzeichnis Geschichte | Innenausstattung | Nutzung | Zwischenfälle...

          Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...

          Marineschifffahrtleitung Inhaltsverzeichnis Geschichte | Heutige Organisation der NATO | Nationale und...