I am trying to create a data structure to store a matrix and write a routine to generate a square matrix of random numbers.
Here is my code. I am strangely getting only 2 float numbers as output. I am doing all this to implement strassen matrix multiplication, which is why I added rs, re, cs, ce to struct.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct _matrix {
int rs;
int re;
int cs;
int ce;
float a[100][100];
}matrix;
void display(matrix m)
{
int i, j;
for (i=m.rs ; i<=m.re ; i++) {
for (j=m.cs ; j<=m.ce ; j++)
printf("%f", m.a[i][j]);
printf("\n");
}
printf("\n");
return;
}
matrix random_matrix(int n)
{
matrix random;
random.cs = random.rs = 0;
random.rs = random.re = n -1;
int i, j;
for(i=0; i < n; i++){
for(j = 0; j < n; j++)
random.a[i][j] = rand();
}
return random;
}
int main(void)
{
matrix m1 = random_matrix(3);
matrix m2 = random_matrix(3);
display(m1);
display(m2);
return 0;
}
I think, to fit the logic, in your code, in random_matrix() function,
random.rs = random.re = n -1;
should be
random.ce = random.re = n -1;
Otherwise, in display(), for (i=m.rs ; i<=m.re ; i++) does not make sense.
That said, to see the random number generator, you can call srand(time(NULL)); in main(), before the call to the matrix generation functions.
Related
I have a matrix[2][3], a vector[3] and I am computing the result of this with the below code which works perfectly fine.
My approach: Initialize a product matrix of size [3][2] to zero, perform the computation as in the function matrix_x_vector and extract the last row to get the final output(vector).
What I expect: Instead of using a product matrix of size [3][2] and then extracting the vector from it, is there any direct way of multiplying a matrix by a vector and store it directly into a resultant vector.
#include <stdio.h>
int main()
{
// Define Variables
int n1 = 2, n2= 3;
int vector[3] = {2,1,2};
int matrix[2][3] = {
{1,-1,2},
{2,-3,1},
};
int Product_matrix[3][2] = {
{0},
{0},
{0}
};
// Define Functions
void matrix_x_vector(int n1,int n2, int vector[n2], int matrix[n1][n2], int Product[n1][n2]); // Performs the calculation
// Execute Functions
matrix_x_vector(n1, n2, vector, matrix, Product_matrix);
printf("\n");
return 0;
}
void matrix_x_vector(int n1,int n2, int vector[n2], int matrix[n1][n2], int Product[n1][n2])
{
int i, j; // i = row; j = column;
printf("\nProduct Matrix of [x]*[y]\n");
// Load up A[n][n]
for (i=0; i<n2; i++)
{
for (j=0; j<n1; j++)
{
Product[j][0] += matrix[j][i] * vector[i];
printf("%4i", Product[j][0]);
}
printf("\n");
}
printf("Vector: \n");
// Print Vector from Product[n][n]
for (i=0; i<n1; i++){
printf("%4i\n", Product[i][0]);
}
}
Product Matrix of [x]*[y]
2 4
1 1
5 3
Vector:
5
3
If I understood the question correctly, it is asked for a function which uses a vector instead of a matrix for the result type. This function can be formulated as follows.
void matrix_x_vector_new(int n1,
int n2,
int vector[3],
int matrix[2][3],
int result_vector[3])
{
int i, j; // i = row; j = column;
printf("\nProduct Matrix of [x]*[y]\n");
// Load up A[n][n]
for (i = 0; i<n2; i++)
{
for (j = 0; j<n1; j++)
{
result_vector[j] += matrix[j][i] * vector[i];
printf("%4i", result_vector[j]);
}
printf("\n");
}
printf("Vector: \n");
// Print Vector from Product[n][n]
for (i = 0; i<n1; i++)
{
printf("%4i\n", result_vector[i]);
}
}
i defined a matrix into a function. how do i return that matrix for print it when i call it with another function. i mean...
#include<stdio.h>
#include<conio.h>
#include<time.h>
void main() {
int m,n;
printf("type 2 numbers:");
scanf("%i %i",&m,&n);
declaration(m,n);\\HERE IS THE PROBLEM
printing(matrix,m,n);
getch();
}
void declaration(int a,int b) {
srand(time(NULL));
int i,j,matrix[a][b];
for(i=0;i<a;i++){
for(j=0;j<b;j++){
matrix[i][j]=1+rand()%7;
}
}
}
void printing(int c[100][100],int a,int b) {
int i,j;
for(i=0;i<a;i++){
for(j=0;j<b;j++){
printf("%i\t",c[i][j]);
}
printf("\n");
}
}
Define it like:
typedef struct {
int rows;
int cols;
int *data;
} int_matrix_entity, *int_matrix;
int_matrix int_matrix_create(int rows, int cols, bool rand)
{
int_matrix mt;
int i;
if ((mt = malloc(sizeof(int_matrix_entity))) == NULL)
{
return NULL;
}
if ((mt->data = malloc(sizeof(int) * cols * rows)) == NULL)
{
free(mt);
return NULL;
}
if (rand)
{
srand(time(NULL));
for (i = 0; i < cols * rows; i++)
{
mt->data[i] = 1 + rand() % 7;
}
}
else
{
memset(mt->data, 0, sizeof(int) * cols * rows);
}
return mt;
}
void int_matrix_printf(int_matrix mt)
{
int i;
int j;
for (i = 0; i < mt->rows; i++)
{
for (j = 0; j < mt->cols; j++)
{
printf("%5d ", mt[i * cols + j]);
}
printf("\n");
}
}
You have a few points that require a bit more attention;
1 ) read warning and error messages given by your compiler
2 ) again, read warning messages given by your compiler
3 ) use indentation to make your code more readable.
4 ) Always return from main(), that's a good practice
The code below does what you want to achieve; have a look at it and keep on reading...
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
// You either have to declare your functions
// or implement them before main()
void declaration(int a,int b, int m[a][b]);
void printing(int a,int b, int m[a][b]);
int main(){ // always return from main()
int m,n;
printf("type 2 numbers:");
scanf("%i %i",&m,&n);
int matrix[m][n];
declaration(m, n, matrix);
printing(m, n, matrix);
return 0;
}
void declaration(int a,int b, int m[a][b]){
srand(time(NULL));
int i,j;
for(i=0;i<a;i++){
for(j=0;j<b;j++){
m[i][j]=1+rand()%7;
}
}
}
void printing(int a,int b, int m[a][b]){
int i,j;
for(i=0;i<a;i++){
for(j=0;j<b;j++){
printf("%i\t",m[i][j]);
}
printf("\n");
}
}
You need a way to transfer data from one function to another. You cannot simply declare an auto variable in one function and pass it to another as you did in the code below
declaration(m,n);
printing(matrix,m,n); /* where does matrix[][] come from? */
remember, C is a strongly typed language which means you have to declare your variables before using them. This applies to your functions as well. You either have to give your function declarations before main() (or more specifically, before using them), or implement them.
Look into your header files (i.e. .h files) and you will see lots of function declarations.
Since you use variable length arrays, make sure your compiler is at least capable of compiling code confirming C99 standard.
Some extras;
Normally, C passes arguments by value and you have to use a pointer if you want the value of your variable get changed within the function. If you have a close look at the code snippet I gave, I simply used an int m[a][b].In C, the name of an array is a pointer to its first element, hence you can change the value of array elements when actually array's name is passed to your function as an argument.
For further reading, you may want to look at
variable scope
global variables (you can define matrix[][] as a global variable and change the value of matrix elements)
declaration vs definition in C
Another simple way to do it is use double pointer to create 2-dimensional array. Keep it simple.
#include <stdio.h>
#include <stdlib.h>
int** create_matrix(int rows, int cols) {
int **matrix = malloc(rows*(sizeof(int *)));
for(int i = 0; i < rows; i++) {
matrix[i] = malloc(cols*sizeof(int));
}
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
matrix[i][j] = 1 + rand()%7;
}
}
return matrix;
}
void printing(int** matrix, int rows, int cols) {
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main(void) {
int rows, cols;
rows = 3, cols = 3;
int** matrix = create_matrix(rows, cols);
printing(matrix, rows, cols);
free(matrix);
return 0;
}
I'm working on a small program of multithreaded matrix multiplication. My first job is to fill the entry of matrices with a random integer. I met some segment faults after I tried to pass a function pointer to pthread_create. And I think the problem is in function pthread_join.
But there are two issues in general.
The first one is the segment fault does not happen every time. Sometimes the code works, but most of the times it doesn't. So it really confuses me.
The other one is when the code is working, there are always several entries still not initialized, especially for matrix[0][0], it is never initialized. And I don't quite know where to debug that one.
Here's my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#define N 5
#define MAX 10
int A[N][N];
int B[N][N];
int C[N][N];
pthread_t pid[N][N];
typedef struct {
int row, col;
} Pos;
typedef void* (*thread_func)(void*);
void print_matrix(int M[][N]) {
int i, j;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
printf("%3d", M[i][j]);
if (j < N - 1) {
printf(", ");
}
}
printf("\n");
}
}
void join_threads(void) {
int i, j;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
pthread_join(pid[i][j], NULL);
}
}
}
void* fill_entry(void* arg) {
Pos* pos = (Pos*)arg;
A[pos->row][pos->col] = rand() % MAX;
B[pos->row][pos->col] = rand() % MAX;
return NULL;
}
void dispatch_jobs(thread_func job_func) {
int i, j;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
Pos pos;
pos.row = i;
pos.col = j;
if (pthread_create(&pid[i][j], NULL, job_func, (void*)&pos)) {
perror("pthread_create");
exit(-1);
}
}
}
}
int main(void) {
srand(time(NULL));
dispatch_jobs(&fill_entry);
join_threads();
printf("Matrix A:\n");
print_matrix(A);
printf("Matrix B:\n");
print_matrix(B);
return 0;
}
Pos pos;
pos.row = i;
pos.col = j;
if (pthread_create(&pid[i][j], NULL, job_func, (void*)&pos)) {
perror("pthread_create");
exit(-1);
}
You are passing a pointer to a local variable to the threads. Once the thread tries to access the data, i.e. dereferences the pointer, the variable is long gone, reused, and contains garbage data.
I am trying to create a structure that will have two integer values, an array, and two 2-D matrices using my code below. I can initialize the structure with the integers and array just fine, and my 'Gen' function will create the random values I want for the array.
However, when I try adding in the matrix components, I run into a problem. My compiler gives me a warning: "initialization from incompatible pointer type". If I understand what I have read so far, this is because the structure needs to be pointed to an array of pointers that represent each row in the matrix. I don't know the syntax for that.
A quick note: the other topics I've seen that are related to this issue all initialize the structure in a function other than the main() function, so I haven't found those solutions helpful.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <string.h>
// Define structure
typedef struct {
int row;
int col;
int *arr;
int **mat1;
int **mat2;
}container;
// Function headers
void Gen(container Thing);
int main() {
int row = 5;
int col = 6;
int A[row];
int M1[row][col];
int M2[row][col+1];
// Initialize structure
container Object = {row, col, A, M1, M2};
// Run "Gen" function
Gen(Object);
int i, j; // Index variables
// Display the array
for(i = 0; i < row; i++)
{
printf("%i ", Object.arr[i]);
}
printf("\n\n");
// Display the numbers from the matrices
for(j = 0; j < Object.row; j++)
{
for(i = 0; i < Object.col; i++)
{
printf("%i ", Object.mat1[j][i]);
}
printf("\n");
}
printf("\n");
for(j = 0; j < Object.row; j++)
{
for(i = 0; i < Object.col; i++)
{
printf("%i ", Object.mat2[j][i]);
}
printf("\n");
}
return (EXIT_SUCCESS);
}
// Function to generate random values in the array & matrices
void Gen(container Thing)
{
int i, j;
srand(time(NULL));
// Generate random values for the array
for(i = 0; i < Thing.row; i++)
{
Thing.arr[i] = rand() % 5;
}
// Generate random values for the matrix
for(j = 0; j < Thing.row; j++)
{
for(i = 0; i < Thing.col; i++)
{
Thing.mat1[j][i] = rand() % 5;
Thing.mat2[j][i] = rand() % 5;
}
}
} // End of "Gen" function
container Object = {row, col, A, M1, M2};
is wrong since the type of M1 is int[row][col], which can decay to int (*)[col] but not to int**. You have the same problem with M2.
You'll need to rethink your strategy for generating a container.
For example:
int main() {
int row = 5;
int col = 6;
int A[row];
int* M1[row];
int* M2[row];
for ( int i = 0; i < row; ++i )
{
M1[i] = malloc(sizeof(M1[i][0])*col);
M2[i] = malloc(sizeof(M1[i][0])*(col+1));
}
// Initialize structure
container Object = {row, col, A, M1, M2};
...
for ( int i = 0; i < row; ++i )
{
free(M1[i]);
free(M2[i]);
}
return (EXIT_SUCCESS);
}
I want to make a programm that takes the dimension and numbers of vectors to be sorted based on their length.
Most of the code works but the sort part of the program doesnt.
Basically what I want to do there is: compare the output from the bereken_lengte function from 2 places in the array w. But nothing seems to happen.
Also in the function bereken_lengte, I cant take the roots of the sum after the loop ended.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double bereken_lengte(double *array, int dim)
{
int i, j;
double sum = 0.0;
for(i=0; i<dim; ++i)
sum += pow(array[i],2);
return sum;
}
void swap(double **p, double **q)
{
double *tmp;
tmp = *p;
*p = *q;
*q = tmp;
}
void sort_vector(double *w[] , int num , int dik )
{
int i,dim,j;
dim = dik;
for(i=0;i<num;++i)
for(j = 1+i;j<num;++j)
{
if(bereken_lengte(w[i],dim) > bereken_lengte(w[j],dim) )
swap(&w[i], &w[j]);
}
}
int main (void)
{
int dim, num;
int i, j,k,l;
double **w;
scanf ("%d %d", &dim, &num); /* read N and M */
w = calloc (num, sizeof (double *)); /* allocate array of M pointers */
for (i = 0; i < num; i++)
{
/* allocate space for N dimensional vector */
w[i] = calloc (dim, sizeof (double));
/* read the vector */
for (j = 0; j < dim; j++)
{
scanf ("%lf", &w[i][j]);
}
}
sort_vector(w,num,dim);
for(k=0; k<num; ++k)
{
printf("\n");
for(l=0; l<dim; ++l)
printf("%f ", w[k][l]);
}
return 0;
}
double bereken_lengte(double *array, int dim)
{
unsigned int i;
double sum =0.0;
for(i=0; i<dim; ++i)
sum += pow(array[i],2);
return sum;
}
Just initialise the sum to zero before summing.
BTW I changed i to unsigned. It is IMnsvHO a good habit to use unsigned types for index && size variables (they won't underflow, and if the do, you'll notice it)
UPDATE:
This tries to avoid the int indices and sized, and uses qsort. (rather ugly, because the compare function takes only two elements; don't try this in a multithreaded program ...) Note, I may have rows and columns interchanged, but thats a way of life... gewoon, omdat het kan!:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double bereken_lengte(double *array, size_t dim)
{
size_t i;
double sum=0.0;
for(i=0; i<dim; ++i)
sum += pow(array[i],2);
return sum;
}
/* this is ugly: qsort only allows only two arguments */
static size_t ze_third_argument=0;
int srt_pdbl(void *l, void *r)
{
double **dl = l, **dr = r;
double diff;
diff = bereken_lengte( *dl, ze_third_argument) - bereken_lengte( *dr, ze_third_argument) ;
return (int) diff;
}
void sort_vector(double *w[] , size_t num , size_t dik )
{
ze_third_argument = dik;
qsort(w, num, sizeof *w, srt_pdbl );
}
int main (void)
{
size_t dim, num;
size_t i, j,k,l;
double **w;
scanf ("%zu %zu", &dim, &num); /* read N and M */
w = calloc (num, sizeof *w); /* allocate array of M pointers */
for (i = 0; i < num; i++)
{
/* allocate space for N dimensional vector */
w[i] = calloc (dim, sizeof *w[i] );
/* read the vector */
for (j = 0; j < dim; j++)
{
scanf ("%lf", &w[i][j]);
}
}
sort_vector(w,num,dim);
for(k=0; k<num; ++k)
{
printf("\n");
for(l=0; l<dim; ++l)
printf("%f ", w[k][l]);
}
return 0;
}