Here is the code for matrix multiplication of random numbers using rand() function. The output of the program gives very large values as matrix elements.
Why not any small random numbers not generated??
This is the output when N=3
Enter the value of N : 3
Final Matrix :
259448206-96933429-936226671
-409898077185182340844598571
-1916994436-653447116470937338
Program:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{
time_t t;
int **ptr1, **ptr2, **ptr3;
int N, col1, row2, col2;
srand ((unsigned) time (&t));
int i, j, k;
printf ("\nEnter the value of N : ");
scanf ("%d", &N);
ptr1 = (int **) malloc (sizeof (int *) * N);
ptr2 = (int **) malloc (sizeof (int *) * N);
ptr3 = (int **) malloc (sizeof (int *) * N);
for (i = 0; i < N; i++)
ptr1[i] = (int *) malloc (sizeof (int) * N);
for (i = 0; i < N; i++)
ptr2[i] = (int *) malloc (sizeof (int) * N);
for (i = 0; i < N; i++)
ptr3[i] = (int *) malloc (sizeof (int) * N);
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
ptr1[i][j] = rand ();
}
}
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
ptr2[i][j] = rand ();
}
}
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
ptr3[i][j] = 0;
for (k = 0; k < N; k++)
ptr3[i][j] = ptr3[i][j] + ptr1[i][k] * ptr2[k][j];
}
}
/* Printing the contents of third matrix. */
printf ("\n\nFinal Matrix :");
for (i = 0; i < N; i++) {
printf ("\n\t");
for (j = 0; j < N; j++)
printf ("%4d", ptr3[i][j]);
}
printf ("\n");
return (0);
}
Use the modulo operator to get smaller values (e.g. rand()%100). While it is slightly non-uniformly distributed that way this should not be an issue here. – Ctx
Related
This question already has answers here:
Correctly allocating multi-dimensional arrays
(2 answers)
Closed 3 months ago.
void alloc_matrix(int ***mat, int *m, int *n) {
mat = (int **)malloc(*m * sizeof(int *));
for(int i = 0; i < *m; i++)
mat[i] = (int *)malloc(*n * sizeof(int));
for(int i = 0; i < *m; i++)
for(int j = 0; j < *n; j++)
scanf("%d", &mat[i][j]);
for(int i = 0; i < *m; i++)
for(int j = 0; j < *n; j++) {
printf("%d ", mat[i][j]);
printf('\n');
}
}
i wanted to read and allocate the matrix in the same function, but when i call it, nothing will print, i think there is something wrong with the way i used the pointers, but i cant figure out what is the problem
The expressions like &mat[i][j] or mat[i][j] used in the for loops and the expression mat used in the statement that allocates memory for an array of pointers
mat = (int **)malloc(*m * sizeof(int *));
for(int i = 0; i < *m; i++)
mat[i] = (int *)malloc(*n * sizeof(int));
for(int i = 0; i < *m; i++)
for(int j = 0; j < *n; j++)
scanf("%d", &mat[i][j]);
for(int i = 0; i < *m; i++)
for(int j = 0; j < *n; j++) {
printf("%d ", mat[i][j]);
printf('\n');
}
are incorect.
Instead you have to write
*mat = (int **)malloc(*m * sizeof(int *));
for(int i = 0; i < *m; i++)
( * mat )[i] = (int *)malloc(*n * sizeof(int));
for(int i = 0; i < *m; i++)
for(int j = 0; j < *n; j++)
scanf("%d", &( *mat )[i][j]);
for(int i = 0; i < *m; i++)
{
for(int j = 0; j < *n; j++) {
printf("%d ", ( *mat )[i][j]);
}
printf( "\n" );
}
That is the parameter mat has the type int ***. This means that the original pointer of the type int ** is passed to the function by reference indirectly through a pointer to it. Thus you need to dereference the parameter to get an access to the original pointer.
And this call of printf
printf('\n');
where you are incorrectly using the integer character constant '\n' instead of the string literal "\n" should be placed after the inner for loop.
Also there is no sense to declare m and n as pointers. The function could be declared at least like
void alloc_matrix(int ***mat, int m, int n) {
Here is a demonstration program
#include <stdio.h>
#include <stdlib.h>
void alloc_matrix( int ***mat, int m, int n )
{
*mat = ( int ** )malloc( m * sizeof( int * ) );
for (int i = 0; i < m; i++)
( * mat )[i] = ( int * )malloc( n * sizeof( int ) );
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
scanf( "%d", &( *mat )[i][j] );
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++) {
printf( "%d ", ( *mat )[i][j] );
}
putchar( '\n' );
}
}
int main( void )
{
enum { M = 2, N = 3 };
int **mat = NULL;
alloc_matrix( &mat, M, N );
for (int i = 0; i < M; i++)
{
free( mat[i] );
}
free( mat );
}
Its output might look like
1 2 3
4 5 6
1 2 3
4 5 6
The first two lines is the user input and the next two lines is the output of elements of the dynamically allocated arrays.
The code below works fine when M <= 4, but seems to bugger up if you put in a whole number that's any bigger (in this case, I actually need M to be 10). Does anyone know why this is happening and what can be done about it? Thanks.
/*
"Read all 100 numbers from the text file and store it in a 10x10 array."
*/
#include <stdio.h>
#include <stdlib.h>
FILE *fptr;
int n;
int M = 4; // Length and width of array
int main()
{
// Allocating memory
int **myArray = (int **)malloc(M * sizeof(int));
for (int j = 0; j < M; j++) {
myArray[j] = (int *)malloc(M * sizeof(int));
}
// Loading data into array
fptr = fopen("List of Numbers.txt","r");
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
fscanf(fptr,"%d",&n);
// printf("%d ",n);
myArray[i][j] = n;
}
}
fclose(fptr);
// Printing
for (int i = 0; i < M; i++) {
for(int j = 0; j < M; j++) {
printf("%d ",myArray[i][j]);
}
printf("\n");
}
return 0;
}
This line:
int **myArray = (int **)malloc(M * sizeof(int));
should be:
int **myArray = (int **)malloc(M * sizeof(int *));
^
You are allocating an array of pointers, not an array of ints.
So, this is my program that calculates matrix determinant using system calls, not good at all, but, the trouble is that when i put in a number bigger than 8 for dimension of matrix, it crashes somehow and i can't figure why it keeps happening. Please, give me some ideas.
The task was to calculate determinant using multithreading. Maybe, the problem is that I exceed max threads? valgrind says that
Use --max-threads=INT to specify a larger number of threads
and rerun valgrind
valgrind: the 'impossible' happened:
Max number of threads is too low
compile it with gcc -g -pthread
#include <stdlib.h>
#include <pthread.h>
#include <math.h>
#include <time.h>
#include <malloc.h>
pthread_mutex_t mutex;
typedef struct {
int **matrix;
int size;
} T_MS;
void* determinant(void *npt) {
T_MS* tmp = (T_MS*) npt;
int i,j;
double det = 0;
pthread_t *array = malloc(sizeof(pthread_t) * tmp->size);
T_MS *mtarr = malloc(sizeof(T_MS) * tmp->size);
if (tmp->size == 1) {
det = tmp->matrix[0][0];
} else if (tmp->size == 2) {
det = tmp->matrix[0][0] * tmp->matrix[1][1] - tmp->matrix[0][1] * tmp->matrix[1][0];
} else {
for (i = 0; i < tmp->size; ++i) {
mtarr[i].matrix = (int **)malloc(sizeof(int *) * tmp->size);
mtarr[i].size = tmp->size - 1;
for (j = 0; j < tmp->size - 1; ++j) {
if (j < i)
mtarr[i].matrix[j] = tmp->matrix[j];
else
mtarr[i].matrix[j] = tmp->matrix[j + 1];
}
pthread_create(&array[i], NULL, determinant, mtarr + i);
}
for (i = 0; i < tmp->size; ++i) {
void *res;
for (j = 0; j < tmp->size - 1; ++j) {
}
pthread_join(array[i], &res);
double x = *(double *)&res;
det += (-1 + 2 * !(i % 2)) * x * tmp->matrix[i][tmp->size - 1];
double answer = *(double*)&det;
free(mtarr[i].matrix);
}
}
free(mtarr);
free(array);
void* ans = *(void **)&det;
return ans;
}
int main(int argc, char const *argv[]) {
srand(time(NULL));
int **matrix;
int n = 0;
int a;
pthread_t tid;
pthread_attr_t attr;
pthread_attr_init(&attr);
printf("Insert the demention of matrix:\n");
scanf("%d", &n);
matrix = (int**)malloc(n * sizeof(int*));
for (int i=0; i<n; ++i)
matrix[i] = (int*)malloc(n * sizeof(int));
printf("Insert matrix:\n");
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
matrix[i][j]=rand()%15;
//matrix[i][j] = i;
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
T_MS* npt = (T_MS*)malloc(sizeof(T_MS));
npt->matrix = matrix;
npt->size = n;
void *det;
pthread_mutex_init(&mutex, NULL);
pthread_create(&tid, NULL, determinant, npt);
pthread_join(tid, &det);
double answer = *(double*)&det;
printf("Det is: %f\n", answer);
for (int i = 0; i < n; ++i)
free(matrix[i]);
free(matrix);
free(npt);
return 0;
} ```
Given the following piece of code, I don't understand why do we have to initialize every single row of the matrix when we have already created enough space in the stack.
#include <stdio.h>
#include <stdlib.h>
main() {
int **w;
int i, j;
int m, n;
printf("Number of rows in the matrix: ");
scanf("%d", &m);
printf("Number of columns in the matrix: ");
scanf("%d", &n);
w = (int **)malloc(m * n * sizeof(int));
for (i = 0; i < m; i++)
w[i] = (int *)malloc(n * sizeof(int));
for (i = 0; i < m; i++)
for (j = 0; j < n; j++) {
printf("Element [%d][%d]: ", i + 1, j + 1);
scanf("%d", &w[i][j]);
}
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
printf("Element [%d][%d]: %d\n", i + 1, j + 1, w[i][j]);
}
There are many issues in your code:
space is not allocated on the stack, but from the heap.
in both cases, memory allocated for the objects is uninitialized, which means it is not initialized to anything in particular and can have any value whatsoever. Relying on any particular contents is undefined behavior.
the matrix dimensions and all the matrix elements are read from standard input with scanf(). Yet you do not check for scanf() failure to convert integers from the characters read from stdin, so any invalid or missing input is going to cause undefined behavior at some point in the program.
your matrix is actually structured as an array of pointers to arrays of int, which is fine, but inconsistent with the size arguments used to allocate the first array: w = (int **)malloc(m * n * sizeof(int)); should be
w = malloc(m * sizeof(*w));
you could easily get objects pre-initialized to 0 by using calloc() instead of malloc():
for (i = 0; i < m; i++)
w[i] = calloc(n, sizeof(int));
you should also check for malloc() failure and exit with an appropriate diagnostic message.
main() is an obsolete prototype for the main function. You should either use int main(), int main(void) or int main(int argc, char *argv[])...
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
int get_int(void) {
int n;
if (scanf("%d", &n) != 1) {
printf("invalid input\n");
exit(EXIT_FAILURE);
}
return n;
}
void xalloc(size_t size) {
void *p = calloc(size, 1);
if (p == NULL) {
printf("out of memory for %zu bytes\n", size);
exit(EXIT_FAILURE);
}
return p;
}
int main() {
int **w;
int i, j;
int m, n;
printf("Number of rows in the matrix: ");
m = get_int();
printf("Number of columns in the matrix: ");
n = get_int();
w = xalloc(m * sizeof(*w));
for (i = 0; i < m; i++) {
w[i] = xalloc(n * sizeof(int));
}
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("Element [%d][%d]: ", i + 1, j + 1);
w[i][j] = get_int();
}
}
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("Element [%d][%d]: %d\n", i + 1, j + 1, w[i][j]);
}
}
for (i = 0; i < m; i++) {
free(w[i]);
}
free(w);
return 0;
}
int main()
{
double *array;
long int n;
n=10000000;//10^7
array = (double *) malloc(n*sizeof(double));
return 0;
}
basically, I want to use this code for a really big aray into a 2 dimensional array, which will have dimensions [very large][4].
If you want a 2D array, then allocate a 2D array. It's that simple.
double (*pArr)[4] = malloc(10000000 * sizeof pArr[0]);
Notes:
do not cast the return value of malloc().
use sizeof pArr[0] instead of sizeof(TheDataType) for defensive programming reasons.
This seems working on Wandbox.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
double (* array)[4];
long int n;
int i, j;
n=10000000;//10^7
array = (double (*)[4]) malloc(n*sizeof(double[4]));
printf("%u\n",(unsigned int)sizeof(array[0]));
printf("%u\n",(unsigned int)sizeof(double[4]));
for (i = 0; i <n; i++) {
for (j = 0; j < 4; j++) array[i][j] = (double)i * j;
}
for (i = 0; i < 10; i++) {
for (j = 0; j < 4; j++) printf("%f ", array[i][j]);
putchar('\n');
}
for (i = n - 10; i < n; i++) {
for (j = 0; j < 4; j++) printf("%f ", array[i][j]);
putchar('\n');
}
free(array);
return 0;
}
int n = 100000;
double** array = malloc(sizeof(double*)*n);
for (int i = 0; i < n; ++i)
{
array[i] = malloc(4*sizeof(double));
}
Also note that we don't cast the malloc's result(Do I cast the result of malloc?).