0

I want to create 2 matrices and fill them with reandom numbers 0-9. I just don't understand why my function doesn`t work like this. If I define a and b with e.g. #define a = 3 it works. So the problem occurs at:

void fillM(int array[a][b])

and

void printM(int array[a][b])

Original code

#include <stdio.h> #include <float.h> #include <stdbool.h> #include <stdlib.h> #include <time.h> //fill random void fillM(int array[a][b]) { for (int x=0; x < a; x++) { for (int y=0; y < b; y++) { array[x][y] = rand()%10; } } } //print void printM(int array[a][b]){ for (int x=0; x < a; x++){ for (int y=0; y < b; y++) { printf("%d ", array[a][b]); } printf("\n"); } } int Main(){ //do I really need this? srand (time(NULL)); //set size int n; printf("please set size of n x n Matrix: \n"); scanf("%d", &n); int m = n; //initialise int m1[n][m]; int m2[n][m]; fillM (m1); fillM (m2); return 0; } 

Revised code

#include <float.h> #include <stdbool.h> #include <stdlib.h> #include <time.h> #include <stdio.h> //fill random void fillM(size_t a, size_t b, int array[a][b]) { for (int x=0; x < a; x++) { for (int y=0; y < b; y++) { array[x][y] = rand()%10; } } } //print void printM(size_t a, size_t b, int array[a][b]){ for (int x=0; x < a; x++){ for (int y=0; y < b; y++) { printf("%d ", array[a][b]); } printf("\n"); } printf("\n"); } int main(){ srand (time(NULL)); //set size int n; printf("please set size of n x n Matrix: \n"); scanf("%d", &n); printf("\n"); int m = n; //initialise int m1[n][m]; int m2[n][m]; fillM (n, m, m1); fillM (n, m, m2); printM (n, m, m1); printM (n, m, m2); return 0; } 

But one more question. If I run the program now, it doesn´t fill the matrix with random numbers everywhere. It puts the same random number in every place. Do you know how to fix this?

5
  • 1
    Aside: "Do I really need this? srand (time(NULL));?" While developing it's better to comment it out so that you get a repeatable sequence. Finally, randomize the PRNG to obtain a different sequence on each run. Commented Oct 22, 2022 at 19:04
  • thanks. But one more question. If I run the program now it doesn´t fill the matrix with random numbers everywhere. It puts the same random number in everyplace. Do you know how to fix this? Commented Oct 23, 2022 at 13:45
  • You should not rewrite the question after you get an answer. Commented Oct 23, 2022 at 13:58
  • You are printing the same out of bounds element on each iteration. Use array[x][y]. Commented Oct 23, 2022 at 13:59
  • I see, sorry. And thanks for the hint. Commented Oct 23, 2022 at 15:17

1 Answer 1

1

At the point where you use a and b, they are not defined. You need something more like:

void fill(size_t a, size_t b, int array[a][b] 

Your calls will pass the array size as well.


In your revised code, you get the same answer for every printed value because you attempt to print the same element of the array, array[a][b] — except that it isn't an element of the array but is a long way out of bounds because the array indexes run from 0..a-1 and 0..b-1. Use array[x][y] instead.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.