Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to write a program that use 2D array to generate a matrix?
    primarykey
    data
    text
    <p>Suppose user enter a integer "3", the matrix should 3 X 3 and some random numbers in it. For example: </p> <pre><code>8 0 2 6 3 4 5 7 1 </code></pre> <p>I need to use 2D array to do it.</p> <p>But I have no idea about how to finish it. Here is my code, what should I do now?</p> <pre><code>#include "stdafx.h" #include "stdlib.h" #include "time.h" int _tmain(int argc, _TCHAR* argv[]) { int num1, num2; printf("enter a integer (3-9): "); scanf("%d", &amp;num1); srand(time(NULL)); switch (num1) { case 3: num2 = (rand() % 8); break; case 4: num2 = (rand() % 15); break; case 5: num2 = (rand() % 24); break; case 6: num2 = (rand() % 35); break; case 7: num2 = (rand() % 48); break; case 8: num2 = (rand() % 63); break; case 9: num2 = (rand() % 80); break; } printf("%d %d\n", num1, num2); int s[][] = num1 * num1; return 0; } </code></pre> <hr> <p>New update</p> <pre><code>#include "stdafx.h" #include "stdlib.h" #include "time.h" int _tmain(int argc, _TCHAR* argv[]) { int num2 = 0; int num=0, i, j; int mtx[9][9] = {0}; while (num &lt; 3 || num &gt; 9) { printf("Enter an integer (3-9): "); scanf("%d", &amp;num); } do { srand(time(NULL)); switch (num) { case 3: num2 = rand() % 8; break; case 4: num2 = rand() % 15; break; case 5: num2 = rand() % 24; break; case 6: num2 = rand() % 35; break; case 7: num2 = rand() % 48; break; case 8: num2 = rand() % 63; break; case 9: num2 = rand() % 80; break; } for (i=0; i &lt; num; ++i) for (j=0; j &lt; num; ++j) mtx[i][j] = num2; } while ( num2 == num2); for (i=0; i &lt; num; ++i) { for (j=0; j &lt; num; ++j) printf("%i ", mtx[i][j]); printf("\n"); } return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload