Bubble sort 2D array in C - c

I need a function to bubble sort this randomly generated 2D array.
Also with rand() method I wanted it to generate numbers between (1, 1000000) but it doesnt give the required range, any suggestion to find out a solution?
int **matrix()
{
int **matrix;
int row, column;
long s, k;
int i,j,f,swap;
srand(time(NULL));
printf("Number of rows: ");
scanf("%d", &row);
printf("Number of column: ");
scanf("%d", &column);
matrix = (int **) calloc(row, sizeof(int));
for(i = 0; i < row; i++)
matrix[i] = (int *) calloc(column, sizeof(int));
for(s = 0; s < row; s++)
{
for(k = 0; k < column; k++)
{
matrix[s][k]=rand()%10000000;
}
}
for(s = 0; s < row; s++)
{
for(k = 0; k < column; k++)
printf("%4d \t\t", matrix[s][k]);
printf("\n");
}
for(i = 0; i < row; i++)
free((void *) matrix[i]);
free((void *) matrix);
return **matrix;
}

Sorting a 2D array with bubble sort is a little different that sorting a single dimensional array. This example may help with that part of your question.
Other issues:
Based on this section:
for(i = 0; i < row; i++)
matrix[i] = (int *) calloc(column, sizeof(int));
The line in the preceding section:
matrix = (int **) calloc(row, sizeof(int));
^
Should be allocating memory for int *
matrix = calloc(row, sizeof(int *));
^
(Note the cast for calloc() has also been removed. In C casting the return of [c][m][re]alloc is not recommended.)
Also with rand() method I wanted it to generate numbers between (1, 1000000) but it doesnt give the required range, any suggestion to find out a solution?
( With credit to this answer )
rand() expanded to provide a pseudo random distribution of 1000000 unique values:
unsigned long rand_ex(void);
int main(void){
srand(clock());
for(int i=0;i<100;i++)
{
printf("%10d: %10lu\n", i, rand_ex());
}
return 0;
}
unsigned long rand_ex(void)
{
unsigned long x;
x = rand();
x <<= 15;
x ^= rand();
x %= 1000001;
return x;
}

Related

I am using pthreads to speed up my matrix multiplication but not getting correct values

I have to calculate the speed of operation for different threads but the matrix size has to be (1000X1 and 1X1000). I have to calculate this using 1,2,4,8,16,32,64,128,256 and 512 threads.
my program is returning same values for all the resultant matrix. Where do I make changes?
I am using random number generator to fill the matrices and dynamically allocating and freeing the matrices.
I first kept N = 512 but I was getting segmentation fault core dumped error so I increased the N. How can I use different number of threads to calculate the matrix?
For a 1000X1 and 1X1000 matrix the resulatant matrix is all 18 and gets segmentation fault core dump error.
For 100X1 and 1X100 all values are 3 and the same happens
to compile it you have to use -lpthreads and -fopenmp
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
// Each thread computes single element in the resultant matrix
void *mult(void *arg)
{
int *data = (int *)arg;
int k = 0, i = 0;
int x = data[0];
for (i = 1; i <= x; i++)
k += data[i] * data[i + x];
int *p = (int *)malloc(sizeof(int));
*p = k;
// Used to terminate a thread and the return value is passed as a pointer
pthread_exit(p);
}
// Driver code
int main()
{
int i, j, k, row1, col1, row2, col2, r, sum, no_of_threads;
printf("Enter the number of rows for matrix 1\n");
scanf("%d", &row1);
printf("Enter the number of columns for matrix 1 \n");
scanf("%d", &col1);
printf("Enter the number of rows for matrix 2 \n");
scanf("%d", &row2);
printf("Enter the number of columns for matrix 2\n");
scanf("%d", &col2);
int **a = (int **)malloc(row1 * sizeof(int *));
for (i = 0; i < row1; i++)
a[i] = (int *)malloc(col1 * sizeof(int));
int **b = (int **)malloc(row2 * sizeof(int *));
for (i = 0; i < row2; i++)
b[i] = (int *)malloc(col2 * sizeof(int));
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
a[i][j] = (rand() % 9) + 1;
}
}
for (i = 0; i < row2; i++)
{
for (j = 0; j < col2; j++)
{
b[i][j] = (rand() % 9) + 1;
}
}
int N = 1000;
// declaring array of threads of size row1*col2
pthread_t *threads;
threads = (pthread_t *)malloc(N * sizeof(pthread_t));
int count = 0;
int *data = NULL;
for (i = 0; i < row1; i++)
for (j = 0; j < col2; j++)
{
// storing row and column elements in data
data = (int *)malloc((N) * sizeof(int));
data[0] = col1;
for (k = 0; k < col1; k++)
data[k + 1] = a[i][k];
for (k = 0; k < row2; k++)
data[k + col1 + 1] = b[k][j];
}
// creating threads
for (int i = 0; i < 512; i++)
{
std:
pthread_create(&threads[i], NULL,
mult, (void *)(data));
}
printf("RESULTANT MATRIX IS :- \n");
for (i = 0; i < N; i++)
{
void *k;
// Joining all threads and collecting return value
pthread_join(threads[i], &k);
int *p = (int *)k;
printf("%d ", *p);
if ((i + 1) % col2 == 0)
printf("\n");
}
for (int i = 0; i < row1; i++)
{
free(a[i]);
}
for (int i = 0; i < row2; i++)
{
free(b[i]);
}
free(a);
free(b);
free(data);
return 0;
}
EDIT:
How do I send multiple arguments through threads as if I read the matrix in my main function, I am not able to pass the values of rows and columns to my mult function. If I read the matrix in mult function then the threads are not working. How do I get it to multiply?
Your approach is flawed:
the destination matrix is not allocated properly
you pass the same argument to all threads
the threads do not receive any information regarding where to store the result of the scalar product.
You should allocate an argument structure for each thread and pass the source arrays and destination location.
With the current approach, there are row1 * col2 scalar products to compute: if you want to use fixed number of threads, you should construct a list of tasks for each thread to process in order to distribute the word among the threads. It is rather easy to do it statically and since all single computations are equivalent in terms of complexity, dynamic distribution does not seem necessary.
Note however that you must wait for all threads to complete before examining and freeing the results. Freeing the arrays as the threads are potentially still accessing the data is among the many causes for undefined behavior in the posted code.

How to specify the number of threads I want to use?

I am trying to Multiply the matrix with use of threads. The code seems to be working. I just need to know how to specify the number of threads so that I can count how much time it's taking. I want to create a table of threads and time it's taking to compute the matrix.
I am dynamically allocating the matrix and filling it with random numbers.
I am creating threads to compute the resultant matrix.
C Program to multiply two matrix using pthreads without use of global variables
#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<stdlib.h>
//Each thread computes single element in the resultant matrix
void *mult(void* arg)
{
int *data = (int *)arg;
int k = 0, i = 0;
int x = data[0];
for (i = 1; i <= x; i++)
k += data[i]*data[i+x];
int *p = (int*)malloc(sizeof(int));
*p = k;
//Used to terminate a thread and the return value is passed as a pointer
pthread_exit(p);
}
//Driver code
int main()
{
int i, j, k, row1, col1, row2, col2, r, sum;
printf("Enter the number of rows for matrix 1\n");
scanf("%d",&row1);
printf("Enter the number of columns for matrix 1 \n");
scanf("%d",&col1);
printf("Enter the number of rows for matrix 2 \n");
scanf("%d",&row2);
printf("Enter the number of columns for matrix 2\n");
scanf("%d",&col2);
int **a = (int **) malloc(row1 * sizeof(int *));
for(i=0;i<row1;i++)
a[i] = (int *) malloc(col1 * sizeof(int));
int **b = (int **) malloc(row2 * sizeof(int *));
for(i=0;i<row2;i++)
b[i] = (int *) malloc(col2 * sizeof(int));
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
a[i][j] = (rand()%9) + 1;
}
}
for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
b[i][j] = (rand()%9) +1;
}
}
int N = row1*col2;
//declaring array of threads of size row1*col2
pthread_t *threads;
threads = (pthread_t*)malloc(N*sizeof(pthread_t));
int count = 0;
int* data = NULL;
for (i = 0; i < row1; i++)
for (j = 0; j < col2; j++)
{
//storing row and column elements in data
data = (int *)malloc((N)*sizeof(int));
data[0] = col1;
for (k = 0; k < col1; k++)
data[k+1] = a[i][k];
for (k = 0; k < row2; k++)
data[k+col1+1] = b[k][j];
//creating threads
pthread_create(&threads[count++], NULL,
mult, (void*)(data));
}
printf("RESULTANT MATRIX IS :- \n");
for (i = 0; i < N; i++)
{
void *k;
//Joining all threads and collecting return value
pthread_join(threads[i], &k);
int *p = (int *)k;
printf("%d ",*p);
if ((i + 1) % col2 == 0)
printf("\n");
}
return 0;
}

finding a Kaprekar numbers inside a 2D array in C

I need a function to find out the Kaprekar numbers inside this 2d array, I searched in web but none of the results worked for 2D array.
This is the array that I made:
int **matrix;
int row, column;
long s, k;
srand(time(NULL));
printf("Number of rows: ");
scanf("%d", &row);
printf("Number of column: ");
scanf("%d", &column);
matrix = (int **) calloc(row, sizeof(int*));
for(i = 0; i < row; i++)
matrix[i] = (int *) calloc(column, sizeof(int));
for(s = 0; s < row; s++)
{
for(k = 0; k < column; k++)
{
matrix[s][k]=(rand()%1000) * (rand()%1000);
}
}
any help or suggestions to convert this code to be able to work for 2D array?
bool iskaprekar(int n)
{
if (n == 1)
return true;
int sq_n = n * n;
int count_digits = 0;
while (sq_n)
{
count_digits++;
sq_n /= 10;
}
sq_n = n*n;
for (int r_digits=1; r_digits<count_digits; r_digits++)
{
int eq_parts = pow(10, r_digits);
if (eq_parts == n)
continue;
int sum = sq_n/eq_parts + sq_n % eq_parts;
if (sum == n)
return true;
}
return false;
}
You have 2D array, you have also the function that verifies the number is Kaprekar or not. The simple way is using the for loop to check all number in 2D array as you did when you initialize the values of array using rand() function.
for(s = 0; s < row; s++) {
for(k = 0; k < column; k++) {
if(iskaprekar(matrix[s][k])) {
printf("%d ", matrix[s][k]);
}
}
}
if you want to store all Kaprekar numbers, you can use pointer then reallocate for it after each time you meet one Kaprekar number. Then remove the duplicated numbers if you want.
int * numbers = 0;
if(!numbers) {return -1;}
int count = 0;
for(s = 0; s < row; s++) {
for(k = 0; k < column; k++) {
if(iskaprekar(matrix[s][k])) {
numbers = realloc(numbers, sizeof(int) (count+1));
if(!numbers) {return -1;}
numbers[count] = matrix[s][k];
count++;
}
}
}
// Remove the duplicated numbers here, if you want

C - How to adapt this code for a very large/dynamic array to a 2 dimensional array?

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?).

segmentation fault when I take in the first value of matrix

If I run my program, and When I typed the dimensions of matrix, after I typed the first value of matrix , the console write out: Segmentation fault
For example:
4
3
Segmentation fault
Process returned 139(0x8B)
void inMatrix(int n, double **matrix)
{
int j, i;
for (i = 0; i < n; i++)
{
for (j= 0; j < n; j++)
{
scanf("%lf", &matrix[i][j]);
}
}
}
void inVector(double *vektor, int n)
{
int k;
for (k = 0; k < n; k++)
{
scanf("%lf", &vektor[k]);
}
}
int main()
{
int n;
// read dimension of matrix and value
scanf("%d", &n);
//matrix
double** matrix = (double **) calloc(n, sizeof ( double*));
//vector
double* vector = (double *) calloc(n, sizeof ( double));
// read values of matrix
inMatrix(n, matrix);
// read values of vector
inVector(vector, n);
outVector(vector, n);
printf("\n");
return 0;
}
You didn't allocate memory for the elements of the matrix, only for the pointers to the individual lines.
You need something like:
for (i = 0; i < n; i++)
matrix[i] = malloc(n * sizeof(double));
Of course, you must free stuff in the same manner when you're done with it.
for (i = 0; i < n; i++)
free(matrix[i]);
free(matrix);

Resources