tags:

views:

413

answers:

3

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;
}
+3  A: 

I haven't used c in a while so you may need to tweek some of the below, but the basic concept should hold.

#include "stdafx.h"
#include "stdlib.h"
#include "time.h"

int _tmain(int argc, _TCHAR* argv[])
{

    int num1, num2;

    /* This makes sure the number they enter is between 3 and 9 */
    while (!(num>3)&(num<9)) {
        printf("enter a integer (3-9): ");
        scanf("%d", &num1);
    }


    srand(time(NULL));
    int counter1 = num1;
    int counter2 = num1;

    int intArray[num1][num1]; /*Initialize array*/

    while (counter1 > 0) {
        while (counter2 > 0) {
            intArray[counter1][counter2] = rand(); /*This will be your populated 2D array */
            counter2--;
        }
        counter1--;
        counter2 = num1;
    }


    return 0;
}
Jeffrey
I could for-see that the loop may increment too fast for the rand() function therefore the random number might always be the same. If this occurs let me know. This occurs because the rand() function is seeded by time and the time may be the exact same each time it runs the loop (since the program should execute in a few mili seconds).
Jeffrey
@Jeffrey: that absolutely will not happen (assuming that your platform has an even remotely competent implementation of rand()). Your code seeds rand( ) only once, outside of the loop.
Stephen Canon
intArray is pointing at random memory. This program is likely to cause a segment fault or worse.
Omnifarious
Oh. There is error in "intArray[][]; "
Programme Newbie
Try it again, sorry like I said its been awhile.
Jeffrey
Nitpick: when you declare the array `int intArray[num1][num1];` you do not initialize it. You can declare and initialize (aka define) with `int intArray[num1][num1] = {{0}};`
pmg
+1  A: 

There are two varieties of 2D array in C, though the syntax for accessing either is confusingly similar. One variety is to have a contiguous range of memory holding all the array values with the compiler 'folding' the memory into an apparent multi-dimensional shape for you:

int contiguous2DArray[3][4] = { 0, 1, 2, 3,
                                4, 5, 6, 7,
                                8, 9, 10, 11 };
int another2DArray[4][3] = { 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 };
int a3DArray[2][2][3] = { 0, 1, 2,  3, 4, 5,    6, 7, 8,  9, 10, 11 };

/* A series of true statements. */
contiguous2DArray[0][0] == 0;
contiguous2DArray[0][1] == 1;
contiguous2DArray[1][0] == 4;

another2DArray[0][0] == 12;
another2DArray[0][1] == 13;
another2DArray[1][0] == 15;

a3DArray[0][0][0] == 0;
a3DArray[0][0][1] == 1;
a3DArray[0][1][0] == 3;
a3DArray[1][0][0] == 6;

All but the very last array size are a required part of the type. Since these sizes are known at compile time the compiler knows to skip along the required amount when incrementing indexes. The last index always causes the compiler to skip along one space every time it goes up by one. The second the last will skip along by n where n is the last size, the third to the last will skip along by m * n where m and n are the last two sizes.

As you can see, since the sizes are not able to vary at run time, this sort of array isn't a great fit for one all dimensions can vary at run time.

For arrays like this, you need to start using arrays of pointers to arrays instead of multidimensional arrays of contiguous memory.

Omnifarious
+1  A: 

Starting with a slight simplification of what you wrote:

#include "stdafx.h"
#include "stdlib.h"
#include "time.h"

int _tmain (int argc, _TCHAR* argv[]) {
    int num=0, i, j;     // Added initializers and loop counters
    int mtx[9][9] = {0}; // Reserve enough space for the worst-case scenario

    while (num < 3 || num > 9) { // Added input validation loop
        printf("Enter an integer (3-9): ");
        scanf("%d", &num);
    }

    srand(time(NULL));

    // Loop through the matrix elements we want, filling each with a random number    
    for (i=0; i < num; ++i)
        for (j=0; j < num; ++j)
            mtx[i][j] = rand();

    /* Do something with the matrix here (display it, etc) */

    return 0;
}

To display the matrix as you showed above, the loop would look almost identical to the loop that filled in the array.

for (i=0; i < num; ++i) {
    for (j=0; j < num; ++j)
        printf("%i ", mtx[i][j]);
    printf("\n");
}

In fact the loops are so similar that if you don't need to do anything with the matrix except display it, there's no real point in using the array to begin with (just use this second loop and replace mtx[i][j] with rand()).

bta
Thanks, if I want to generate a new random number while the new random number is equal to the previous random number, what should ! do? I have tried, but don't work. I have post those code above.
Programme Newbie
The nature of random numbers makes this test unnecessary. The odds of generating the same number twice in a row are tiny. If you must guarantee this, then you can turn your sentence above into C code rather easily: `prev=rand(); new=rand(); while(new == prev) { new = rand(); }`
bta