title: Taking in multiple inputs at once and then giving out output at once
body: <p>So my question is how to efficiently write a program where in we are able to take multiple inputs (the amount of inputs given is determined by the user) and then give the outputs at once. Lets consider a program which gives gives the sum of its digits. Eg - 12345 = 15. </p>
  
  <pre><code>//Single input single output 
  #include &lt;iostream&gt;
  
  using namespace std;
  
  int main()
  {
      int T, N;
      cout &lt;&lt; "Enter the value of T (No. of test cases)" &lt;&lt; endl;
      cin &gt;&gt; T;
      cout &lt;&lt; "Enter the value of N : " &lt;&lt; endl;
      while (T &gt; 0)
      {
          cin &gt;&gt; N;
  
          int ans = 0,temp1,temp2;
          while(N!=0)
          {
              temp1= N %10;
              N = (N - temp1)/10;
              ans = ans + temp1;
          }
          cout &lt;&lt; ans &lt;&lt; endl;
          T--;
      }
  
      return 0;
  }
  </code></pre>
  
  
  
  <pre><code>// Taking in all inputs then giving out all outputs ( Not working properly)
  
  #include &lt;iostream&gt;
  
  using namespace std;
  int SumCal(int Number, int TestCase, int t);
  int main()
  {
      int N, T;
  
      cout &lt;&lt; " Enter the value of T ( Total number of test cases) " &lt;&lt; endl;
      cin &gt;&gt; T;
      int *Ans(new int[T]);
      if (T &gt;= 1 &amp;&amp; T &lt;= 1000)
      {
          cout &lt;&lt; "Enter the value of N" &lt;&lt; endl;
          for (int i = 1; i &lt;= T; i++)
          {
              cin &gt;&gt; N;
              if (N &gt;= 1 &amp;&amp; N &lt;= 100000)
                  Ans[i] = SumCal(N, i, T);
          }
      }
  
      for (int z = 1; z &lt;= T; z++)
      {
          cout &lt;&lt; Ans[z] &lt;&lt; endl;
      }
  
  
      delete[] Ans;
      return 0;
  }
  
  int SumCal(int Number, int TestCase, int t)
  {
      int temp1, temp2 = 0;
      int *AnsTemp(new int[t]);
  
      temp1 = Number % 10;
  
      temp2 = Number / 10;
      if (temp2 &lt; 10 &amp;&amp; temp2 &gt; 0)
          AnsTemp[TestCase] = (temp1 + temp2);
  
  
      while (temp2 &gt; 10)
      {
          AnsTemp[TestCase] = (AnsTemp[TestCase] + temp1);
          temp2 = temp2 / 10;
          temp1 = temp1 % 10;
      }
  
      return AnsTemp[TestCase];
      delete[] AnsTemp;
  }
  </code></pre>
  
  
  
  <pre><code>// This will work properly for multiple inputs multiple outputs
  
  #include &lt;iostream&gt;
  
  using namespace std;
  int SumCal(int Number, int TestCase);
  int main()
  {
      int N, T;
  
      cout &lt;&lt; " Enter the value of T ( Total number of test cases) " &lt;&lt; endl;
      cin &gt;&gt; T;
      int Ans[1000] = {};
      if (T &gt;= 1 &amp;&amp; T &lt;= 1000)
      {
          cout &lt;&lt; "Enter the value of N" &lt;&lt; endl;
          for (int i = 1; i &lt;= T; i++)
          {
              cin &gt;&gt; N;
              if (N &gt;= 1 &amp;&amp; N &lt;= 100000)
                  Ans[i] = SumCal(N, i);
          }
      }
  
      for (int z = 1; z &lt;= T; z++)
      {
          cout &lt;&lt; Ans[z] &lt;&lt; endl;
      }
  
      return 0;
  }
  
  int SumCal(int Number, int TestCase)
  {
      int temp1, temp2 = 0;
      int ans;
  
      temp1 = Number % 10;
  
      temp2 = Number / 10;
      if (temp2 &lt; 10 &amp;&amp; temp2 &gt; 0)
          ans = (temp1 + temp2);
  
      while (temp2 &gt; 10)
      {
          ans = (ans + temp1);
          temp2 = temp2 / 10;
          temp1 = temp2 % 10;
      }
  
      return ans;
  }
  </code></pre>
  
  <p>These are the codes I could think of. The first one is a simple one, which takes in an input and then gives out a output. In the second one I tried to use dynamic memory allocation but the program gives error. ( I know I haven't made proper use of * and &amp; in it but I already tried using it in various manners and failed). The third program is successful but as we are setting up a large constraint value to the array, (i.e int Ans[1000]) it makes the program a bit inefficient. </p>
  
  <p>So my question is how would one dynamically allocate memory during runtime successfully to take in multiple inputs and then give multiple outputs at once. </p>