<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Kopieren eines 2D Arrays ueber ein anders 2D Array...]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>Bin momentan in einer Beginner C++ Klasse und bin gerade dabei diese Aufgabe zu loesen. Der Code selbst hat keinen bestimmten zweck. Er soll uns bloss als uebung dienen.</p>
<p>Mein Problem ist, dass ich keine Ahnung habe, wie ich die Funktion CopyArray2D schreiben soll. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /><br />
Mir ist nicht klar wie ich das multidimensionale Array initialisieren soll und wie ich dann das Array innerhalb der Funktion kopiere...</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;strstream&gt;
using namespace std;

int ** NewArray2D( int nRow, int nCol ) { 

   int** newArray = new int*[nRow];

   for(int i = 0; i &lt; nRow; i++){
      newArray[i] = new int[nCol];
   }
   return newArray;
}

void InitArray2D( int ** array2D, int nRow, int nCol, int val ){

   for(int i =0; i&lt;nRow; i++){
      for(int j=0; j&lt;nCol; j++){
         array2D[i][j]=val;
      }
   }
}

void CopyArray2D( int ** a2D, int ** b2D, int nRow, int nCol ){ 

   // This function copies array b2D over array a2D, which means that all   
   // elements of b2D are copied over corresponding elements of a2D.   
}

string ToString( int ** array2D, int nRow, int nCol ) { 

   strstream ss;

   for (int i=0; i&lt;nRow; i++){

      for(int j=0; j&lt;nCol; j++){
         ss &lt;&lt; array2D[i][j] &lt;&lt; &quot;\t&quot;; 
      }
      ss &lt;&lt; &quot;\n&quot;;
   }    
   ss &lt;&lt; '\0';

   return ss.str();
} 

int main(){

   const int R=4, C=4;
   int val = 8;

   int** a1 = NewArray2D(R, C);
   int** a2; // keine Ahnung wie ich dieses Array initialisieren soll...

   InitArray2D(a1, R, C, val);

   CopyArray2D(a1, a2, R, C); 

   cout &lt;&lt; &quot;a2D = \n&quot; &lt;&lt; ToString(a1, R, C ) &lt;&lt; endl;

   return 0;
}
</code></pre>
<p>Bin natuerlich ueber jede Hilfe dankbar.</p>
<p>Dornenhexe</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/178842/kopieren-eines-2d-arrays-ueber-ein-anders-2d-array</link><generator>RSS for Node</generator><lastBuildDate>Mon, 21 Sep 2026 03:19:13 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/178842.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 15 Apr 2007 18:16:16 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Kopieren eines 2D Arrays ueber ein anders 2D Array... on Sun, 15 Apr 2007 18:16:16 GMT]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>Bin momentan in einer Beginner C++ Klasse und bin gerade dabei diese Aufgabe zu loesen. Der Code selbst hat keinen bestimmten zweck. Er soll uns bloss als uebung dienen.</p>
<p>Mein Problem ist, dass ich keine Ahnung habe, wie ich die Funktion CopyArray2D schreiben soll. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /><br />
Mir ist nicht klar wie ich das multidimensionale Array initialisieren soll und wie ich dann das Array innerhalb der Funktion kopiere...</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;strstream&gt;
using namespace std;

int ** NewArray2D( int nRow, int nCol ) { 

   int** newArray = new int*[nRow];

   for(int i = 0; i &lt; nRow; i++){
      newArray[i] = new int[nCol];
   }
   return newArray;
}

void InitArray2D( int ** array2D, int nRow, int nCol, int val ){

   for(int i =0; i&lt;nRow; i++){
      for(int j=0; j&lt;nCol; j++){
         array2D[i][j]=val;
      }
   }
}

void CopyArray2D( int ** a2D, int ** b2D, int nRow, int nCol ){ 

   // This function copies array b2D over array a2D, which means that all   
   // elements of b2D are copied over corresponding elements of a2D.   
}

string ToString( int ** array2D, int nRow, int nCol ) { 

   strstream ss;

   for (int i=0; i&lt;nRow; i++){

      for(int j=0; j&lt;nCol; j++){
         ss &lt;&lt; array2D[i][j] &lt;&lt; &quot;\t&quot;; 
      }
      ss &lt;&lt; &quot;\n&quot;;
   }    
   ss &lt;&lt; '\0';

   return ss.str();
} 

int main(){

   const int R=4, C=4;
   int val = 8;

   int** a1 = NewArray2D(R, C);
   int** a2; // keine Ahnung wie ich dieses Array initialisieren soll...

   InitArray2D(a1, R, C, val);

   CopyArray2D(a1, a2, R, C); 

   cout &lt;&lt; &quot;a2D = \n&quot; &lt;&lt; ToString(a1, R, C ) &lt;&lt; endl;

   return 0;
}
</code></pre>
<p>Bin natuerlich ueber jede Hilfe dankbar.</p>
<p>Dornenhexe</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1266671</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1266671</guid><dc:creator><![CDATA[Dornenhexe]]></dc:creator><pubDate>Sun, 15 Apr 2007 18:16:16 GMT</pubDate></item><item><title><![CDATA[Reply to Kopieren eines 2D Arrays ueber ein anders 2D Array... on Sun, 15 Apr 2007 19:26:54 GMT]]></title><description><![CDATA[<p>Funktioniert im Prinzip wie InitArray2D() nur das du zum Füllen nicht &quot;val&quot; sondern den jeweiligen Wert des anderen Arrays benutzt:</p>
<pre><code class="language-cpp">for(int i = 0; i &lt; nRow; ++i)
{
    for(int j = 0; j &lt; nCol; ++j)
         a2D[i][j] = b2D[i][j];
}
</code></pre>
<p>MfG SideWinder</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1266724</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1266724</guid><dc:creator><![CDATA[SideWinder]]></dc:creator><pubDate>Sun, 15 Apr 2007 19:26:54 GMT</pubDate></item><item><title><![CDATA[Reply to Kopieren eines 2D Arrays ueber ein anders 2D Array... on Sun, 15 Apr 2007 19:28:19 GMT]]></title><description><![CDATA[<p>Achja, a2 muss natürlich ebenfalls initialisiert werden, warum weißt du nicht wie du das machen sollst? Ganz genau wie a1 auch...</p>
<p>Und du vergisst, soweit ich das sehe, komplett auf das freigeben des mit new vom Heap geholten Speichers und erzeugst damit Speicherlecks!</p>
<p>MfG SideWinder</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1266735</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1266735</guid><dc:creator><![CDATA[SideWinder]]></dc:creator><pubDate>Sun, 15 Apr 2007 19:28:19 GMT</pubDate></item></channel></rss>