Thread: Pointers, Arrays, Assigning values

1. Pointers, Arrays, Assigning values

I have zero idea why this is occuring, I have looked at so many examples of pointers and arrays and I just cannot figure this out.

All I am trying to do right now is create an n by m matrix and populate it with random numbers...but all I get are null pointers (ie 0)

Code:
```#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"

int main(int argc, char* argv[]){
int rank;
int p;
int i, j;

double *A;              //Matrix A
double *b;              //Vector b

int m = atoi(argv[1]);
int n = atoi(argv[2]);

//printf("M=%d, N=%d\n", m,n);  //Strictly used for testing purposes

A = malloc((n*m)*sizeof(double));

for (i=0; i<n; i++){
for(j=0; j<n; j++){
A[i*n + j] =  rand() % 100;//assigns *A random numbers between 0 - 99
printf("%d\n",A[i*n +j]);
}
}

//genMatrix(n, m, A);
/*
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &p);
MPI_Finalize();
*/

return 0;
}```
I even tried writing
Code:
```double *A;
...
A = malloc((n*m)*sizeof(double);
...
A[0] = 5;
printf("%d", A[0]);```
And this is returning 0, rather, a null pointer as well. Any help would be greatly appreciated

2. srand() before calling rand() and:
Code:
`for(j=0; j<n; j++);  // change to: for (j = 0; j < m; j++);`

3. I found the solution to my error...

I am supposed to use %.1f....not %d

>.<

4. If you're using gcc as your compiler, then seriously consider adding at least -Wall as a command line option.

One of the many things it can diagnose is mis-matched printf/scanf formats.

Popular pages Recent additions