Suppose user enter a integer "3", the matrix should 3 X 3 and some random numbers in it. For example:
8 0 2
6 3 4
5 7 1
I need to use 2D array to do it.
But I have no idea about how to finish it. Here is my code, what should I do now?
#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", &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;
}
New update
#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 < 3 || num > 9) {
printf("Enter an integer (3-9): ");
scanf("%d", &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 < num; ++i)
for (j=0; j < num; ++j)
mtx[i][j] = num2;
}
while ( num2 == num2);
for (i=0; i < num; ++i) {
for (j=0; j < num; ++j)
printf("%i ", mtx[i][j]);
printf("\n");
}
return 0;
}