-3
#include <stdio.h>
#include <stdlib.h> 
#include <time.h>
#define COL 20
#define ROW 20
void PopulateArray2DUnique(int [][COL],  int, int , int , int );
void DisplayArray2D(int [][COL], int, int );


int main()
{
    int matrix[ROW][COL], r ,c ,i , j;
    int max = 400;
    int min = 0;


    printf("Enter the amount of rows and cols you would like\n Must be under 20!\n");
    scanf("%d%d\n",&r,&c);

    PopulateArray2DUnique(matrix,r,c,min,max);
    DisplayArray2D(matrix,r,c);



    return 0;
}

void PopulateArray2DUnique(int matrix[][COL], int r,int c, int min, int max)
{


  int i, j;



    srand(time(NULL));
    for(i = 0; i<r; i++)
    {
         for(j = 0; j<c; j++)
     {
            matrix[i][j] = rand() % (max + min)+min;
     }
    }

}

void DisplayArray2D(int matrix[][COL], int r,int c)
{
    int i,j;
    for(i = 0; i < r; i++)
{
    for(j = 0; j < c; j++)
    {
        printf("%4d ", matrix[i][j]);
    }
    printf("\n");
}


}

what this program does (so far) is ask user to input the size of the matrix, then the function PopulateArray2DUnique generates random values for the matrix!

asks me to input the size of rows and cols twice

  • well, if you're just going to fill the matrix with random numbers, you'd probably want to NOT prompt the user to end a bunch of numbers that'll just get trashed/replace in the next step anyways... – Marc B Feb 09 '16 at 19:56
  • and what made you think asking the user will be a good way to get random numbers? – Sourav Ghosh Feb 09 '16 at 19:56
  • Instead of just saying "I can't figure it out" it would be better if you explained what it is in particular that you are having difficulty with. Looks like you have made an attempt but have commented it out. So tell us what the behaviour of that attempt is and why you can't get it to work. – kaylum Feb 09 '16 at 19:57
  • 1
    @MarcB im asking user to input the size of matrix – Hassan Rammal Feb 09 '16 at 19:57
  • and then have `scanf()` in a loop to populate it... – Marc B Feb 09 '16 at 19:58
  • 1
    @SouravGhosh im asking user to input size of matrix not numbers – Hassan Rammal Feb 09 '16 at 19:58
  • @kaylum im having trouble getting random numbers into the matrix, im asking user to input the size of rows and cols, – Hassan Rammal Feb 09 '16 at 19:59
  • @HassanRammal is it? what is the nested for loop doing there? – Sourav Ghosh Feb 09 '16 at 20:00
  • Also, I demand an explanation for the upvote in the comment by @HassanRammal previously. What did I miss? :P – Sourav Ghosh Feb 09 '16 at 20:00
  • Yes, but what troubles?? Does it crash? Does it hang? Does it put in the wrong values? Does it not compile? What exactly? – kaylum Feb 09 '16 at 20:00
  • @SouravGhosh in main or 2nd function? – Hassan Rammal Feb 09 '16 at 20:00
  • 2
    @HassanRammal Tell me, is this actually your code? do you know what is there, at all? – Sourav Ghosh Feb 09 '16 at 20:01
  • @kaylum whole bunch of mistakes like to few arguments when calling the function, function expected identifier at line 31, – Hassan Rammal Feb 09 '16 at 20:03
  • @SouravGhosh it is mine, prof gave us the function prototypes, i just need help getting random numbers into my array – Hassan Rammal Feb 09 '16 at 20:05
  • Right, so you have a whole bunch of compile errors and you don't think it is a good idea to tell us what those are exactly but rather just say "I have troubles"? – kaylum Feb 09 '16 at 20:05
  • @kaylum all im asking for is help on how to get random numbers into my matrix – Hassan Rammal Feb 09 '16 at 20:07
  • You already have code to do that that has been commented out. Like others have said it is hard not to come to the conclusion that you don't know what code you have written or you have obtained code from someone else. If that code doesn't compile then you should be asking about that and not a vague "get random numbers into a matrix". – kaylum Feb 09 '16 at 20:09
  • Is this the right question? The naming in the code suggests you want *unique* random numbers in the array, but when `20 * 20 = 400` and the max random number calculated is `99` those numbers are not going to be unique - even if you *did* try a strategy to make them so. – Weather Vane Feb 09 '16 at 20:16
  • @kaylum this website is for asking for help, i asked a question needing help on how to generate a random matrix which the size is asked by user, if you can help then great thank you, but if you cant then just move on, your attitude isnt helping anyone here. – Hassan Rammal Feb 09 '16 at 20:16
  • @WeatherVane good question, let me ask my prof! thank you for pointing that out! – Hassan Rammal Feb 09 '16 at 20:18
  • I'm trying to understand why you don't use the code you already have as it appears to be on the right track. If that code is giving you problems then ask so we can address that specifically. – kaylum Feb 09 '16 at 20:24
  • @kaylum i posted my errors above, i would still like to use my code, just want to know if anyone sees anything wrong with it – Hassan Rammal Feb 09 '16 at 20:27
  • The errors you recently posted, do not relate to the the code you have shown. – Weather Vane Feb 09 '16 at 20:32
  • @WeatherVane im working on it thats why, i will update the errors right now – Hassan Rammal Feb 09 '16 at 20:33
  • Why in `main` do you introduce the (undefined) `rowsize` and `colsize` when you aleady have `r` and `c`? I am with @kaylum: you are making this up as you go. Please prepare a proper syntactically correct attempt, and ask another question. – Weather Vane Feb 09 '16 at 20:43
  • @WeatherVane theres more to this code, i still need to find biggest number in the matrix, also i need to sort it, ascending order. Prof gave us all the functions prototypes that he wants us to use and rowsize and colsize were included and im just trying to see where they fit in all these. i currently got my code to have to errors,(will edit code right now). but its for some reason im getting segmentation fault – Hassan Rammal Feb 09 '16 at 20:50

3 Answers3

0

The following code will allow the user to enter the matrix dimensions separated by a space, as in 2 2 for a 2-by-2 matrix, and then print the contents of the matrix with random numbers in the range from min to max:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define COL 20
#define ROW 20
//void PopulateArray2DUnique(int [][COL], unsigned int,unsigned int , int , int );

int main()
{
    int matrix[ROW][COL], r ,c ,i , j;
    int max = 100;
    int min = 0;
    int rand_num;
    float fraction;

    printf("Enter the amount of rows and cols you would like\n Must be under 20!\n");
    scanf("%d %d",&r,&c);

    if ((r > ROW) || (r < 1)) {
        r = ROW;
    }
    if ((c > COL) || (c < 1)) {
        c = COL;
    }
    for (i=0;i<r;i++){
        for(j=0;j<c;j++){
            rand_num = rand();
            fraction = ((float) rand_num / (float) RAND_MAX);
            matrix[i][j] = (int) (min + (fraction * max));
        }
    }

    for (i=0;i<r;i++){
        for(j=0;j<c;j++){
        printf("The value for ROW: %d, COLUMN %d IS: %d\n", i, j, matrix[i][j]);
        }
    }
    //PopulateArray2DUnique(matrix,rowsize,colsize,min,max);
    return 0;
}

The output is as follows:

$ ./a.out
Enter the amount of rows and cols you would like
 Must be under 20!
2 2
The value for ROW: 0, COLUMN 0 IS: 84
The value for ROW: 0, COLUMN 1 IS: 39
The value for ROW: 1, COLUMN 0 IS: 78
The value for ROW: 1, COLUMN 1 IS: 79
A B
  • 4,068
  • 1
  • 20
  • 23
0

If your issue is simply creating random integers to plug into the matrix then use the random function with srand and time as explained here

Community
  • 1
  • 1
Ken Clement
  • 748
  • 4
  • 13
0
srand(time(NULL));

for(i = 0; i < rowsize; i++)
{
    for(j = 0; j < colsize; j++)
    {
        matrix[i][j] = (rand() % (max-min)) + min;
    }
}

Given rand() % N will produce numbers from 0 to N-1, so instead of using max we can use (max+1) to make it inclusive, we need to produce numbers from min to max, first we subtract by min to get numbers from min-min to max-min, this will produce numbers from 0 to max-min, then add min to the result, to get a range from min to max

(rand() % ((max+1)-min)) + min

Then finally to print the matrix in a tabular way

for(i = 0; i < rowsize; i++)
{
    for(j = 0; j < colsize; j++)
    {
        printf("%4d ", matrix[i][j]);
    }
    printf("\n");
}

Reading the input from user should be like this, keep asking until a valid input

r=20;
while (r <= 0 || r >= 20)
{
    printf("Enter how many rows (under 20) then press Enter: ");
    scanf("%d", &r);
}

c=20;
while (c <= 0 || c >= 20)
{
    printf("Enter how many columns (under 20) then press Enter: ");
    scanf("%d", &c);
}
Khaled.K
  • 5,828
  • 1
  • 33
  • 51