Increasing number of rows in 2D c-array - c

Can't increase rows in 2d array, but columns is ok.
#include <stdio.h>
#include <stdlib.h>
it is working:
void increasecolumn(int ** mas, int* n, int m){
for (int i = 0; i < m; i++){
int* tmp = realloc(mas[i], sizeof (*mas[i]) * ((*n) + 1));
if (tmp){
mas[i] = tmp;
}
}
(*n) = (*n) + 1;
}
but increasing rows failed
void increaserow(int ** mas, int n, int* m){
int ** tmp = realloc(mas, sizeof(*mas) * ((*m) + 1));
if (tmp){
mas = tmp;
for (int i = 0; i < 1; i++){
mas[(*m) + i] = malloc(sizeof(*mas[(*m) + i]) * n);
}
}
(*m) = (*m) + 1;
}
int main(int argc, char * argv[]) {
int n = 3; // columns
int m = 2; // rows
int** mas = malloc(m*sizeof(*mas));
for(int i = 0; i < m; i++){
mas[i] = malloc(n*sizeof(*(mas[i])));
}
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
mas[i][j] = 0;
printf("%d ", mas[i][j]);
}
printf("\n");
}
printf("\n");
increasecolumn(mas, &n, m);
for (int i = 0; i < m; i++){
mas[i][n-1] = 1;
}
increaserow(mas, n, &m); // problem is here
for (int j = 0; j < n; j++){
mas[m-1][j] = 0;
}
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
printf("%d ", mas[i][j]);
}
printf("\n");
}
system("pause");
return 0;
}
I use this answer Resizing 2D Arrays in C like an example, something wrong.
The GNU Project Debugger on Windows:
warning: FTH: (9152): * Fault tolerant heap shim applied to current process. This is usually due to previous crashes. *
0 0 0
0 0 0
Program received signal SIGSEGV, Segmentation fault.
0x0000000000401821 in main (argc=1, argv=0x7f1990) at D:\III Курс! II СЕМЕСТР\МатМодДослОп\stud\Untitled2.c:47
47: mas[m-1][j] = 0;

Related

Matrix determinant with system calls

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;
} ```

Why am I getting segmentation fault [139]?

The title is pretty clear I think.
I am trying to create a program that calculates a 3x3 linear system using determinants, but I am getting a segmentation fault. Here is the code:
#include<stdio.h>
int determinant(int n, int m, int det[m][n])
{
int res;
res = det[0][0]*det[1][1] - det[0][1]*det[1][0];
return res;
}
int main(void)
{
int arr[3][4], det[2][2], i, j, D; //Dx1, Dx2, Dx3
for(i = 0; i < 3; i++)
{
printf("Eisagete tous suntelestes ths %dhs eksisoshs.", i+1);
scanf("%d %d %d %d", &arr[i][0], &arr[i][1], &arr[i][2], &arr[i][3]);
}
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; i++)
{
det[i][j] = arr[i+1][j+1];
}
}
D = arr[0][0]*determinant(2, 2, det);
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; i++)
{
det[i][j] = arr[i+1][j+((j == 1) ? 1 : 0)];
}
}
D -= arr[0][1]*determinant(2, 2, det);
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; i++)
{
det[i][j] = arr[i+1][j];
}
}
D += arr[0][2]*determinant(2, 2, det);
printf("%d\n", D);
}
I am getting the error right after completing the first for loop in main.
In the block:
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; i++)
{
det[i][j] = arr[i+1][j+1];
}
}
You increment i in both loops, and adding 1 more to it while reading from the array. So at arr[i+1] you are reading to far.
A segmentation fault basically means you are trying to read something you don't have access to.
You shoud never do what you're doing by passing static array sizes m and n as function argument:
int determinant(int n, int m, int det[m][n])
Check https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html for info

Segmentation fault in matrix multiplication using multiple threads

#include <stdio.h>
#include <pthread.h>
int arr[1000][1000];
int brr[1000][1000];
int h;
int f;
void *BMM(void *arg)
{
int* neo = (int*) arg;
int ne = *neo;
int sum = 0;
for(int i = 0; i < n; ++i)
{
sum += arr[x][i]*brr[x][f];
++f;
}
printf("%d\n", sum);
crr[x][h] = sum;
pthread_exit(NULL);
}
int main()
{
pthread_t* ar = malloc(3*sizeof(*ar));
printf("Enter the value of m and n\n");
scanf("%d %d",&m,&n);
for(int i = 0; i < m; ++i)
{
for(int j = 0; j < n; ++j)
{
scanf("%d",&arr[i][j]);
}
}
printf("Enter the value of p and q\n");
scanf("%d %d",&p,&q);
if(p != n)
{
printf("The matrix multiplication is not possible\n");
return 0;
}
int* id;
id = (int *)malloc(4*sizeof(int));
for(int i = 0; i < p; ++i)
{
for(int j = 0; j < q; ++j)
{
scanf("%d",&brr[i][j]);
}
}
for(x = 0; x < m; ++x)
{
for(z = 0; z < q; z+=4)
{
f = z;
h = z;
for(int k = 0; k < 3; ++k)
{
pthread_create(&ar[k],NULL,BMM,NULL);
}
for(int k = 0; k < 3; ++k)
{
pthread_join(ar[k],NULL);
}
}
}
for (int i = 0; i < m; ++i)
{
for(int j = 0; j < q; ++j)
{
printf("%d ",crr[i][j]);
}
printf("\n");
}
}
The above program is supposed to multiply two matrix by multiplying row one of matrix by all the columns of other matrix using 3 threads and then row two by all the other columns and so on and then store the respective values int another matrix but it is giving segmentation fault. Where am I going wrong?
I think your problem is here:
pthread_create(&ar[k],NULL,BMM,NULL);
^^^^
void *arg is NULL
and then:
void *BMM(void *arg)
{
int* neo = (int*) arg;
int ne = *neo; // Dereference NULL --> segmentation fault
Further this looks strange:
void *BMM(void *arg)
{
int* neo = (int*) arg;
int ne = *neo; // ne is never used !!
int sum = 0;
for(int i = 0; i < n; ++i) // Where does n come from ?
Perhaps it should be n instead of ne?
If n, x, f and h are global variables you are into trouble as all threads will work on the same variables. That would be real bad. Each thread needs it own variables.
BTW:
Always check the value returned by scanf - something like:
if (scanf("%d %d",&m,&n) != 2)
{
// Add error handling here
}
and
if (scanf("%d",&arr[i][j]) != 1)
{
// Add error handling here
}

Finding the smallest element in multidimensional array

Im asking for help once again. This time I found myself in the midst of a task which I found on the internet and I cant find a good solution. At first I thought of bubble sort, but I didnt seem to find a way to use it with multidimensional array.
The task asks to find 3 smallest elements which arent repeating on the second array.
So far my code looks like this, though Ive removed a lot of it... Since it was a very wrong and futile aproach.
Everything was written in C.
Thanks for the help.
3
1 2 3
3 3 3
3 3 3
5 4 6
6 4 5
4 5 6
This is the given information for the file
#include stdio.h
#include stdlib.h
int main()
{
FILE *fptr;
fptr = fopen("Duomenys.txt", "r");
int i, j;
int k, l;
int bubbleTmp;
fscanf(fptr, "%d ", &k);
l = k;
int a[k][k];
int b[k][k];
for(i = 0; i < k; i++)
{
for(j = 0; j < l; j++)
{
fscanf(fptr,"%d ",&a[i][j]);
printf("%d ", a[i][j]);
}
printf("\n");
}
printf("\n\n\n");
for(i = 0; i < k; i++)
{
for(j = 0; j < l; j++)
{
fscanf(fptr,"%d ",&b[i][j]);
printf("%d ", b[i][j]);
}
printf("\n");
}
// Bubble Sort
for(i = 0; i < k; i++)
{
for(j = 0; j < l; j++)
{
if(a[i][j] > a[i][j+1])
{
}
}
}
fclose(fptr);
return 0;
}
To find the minimum and its position in the array:
int min = a[0][0], i_min = 0, j_min = 0;
for(i = 0; i < k; i++)
{
for(j = 0; j < l; j++)
{
if(a[i][j] < min)
{
min = a[i][j];
i_min = i;
j_min = j;
}
}
}
After the first try, you repeat it two more times, avoiding the minimums alrerady found. The positions of each minimums are known.
Previously, it was a suggestion.
Now, here is the solution:
//----------------------------------------------------------------------------
#include <iostream>
#include <iomanip>
#include <ctime>
//----------------------------------------------------------------------------
int **CreateArray (int N, int M)
{
int **arr = new int*[N];
for (int i = 0; i<N; i++)
arr[i] = new int [M];
for (int i = 0; i<N; i++)
for (int j = 0; j<M; j++)
arr[i][j] = rand()%10 - rand()%10;
return arr;
}
void DestroyArray (int **arr, int N)
{
for (int i = 0; i<N; i++)
delete [] arr[i];
delete [] arr;
}
void PrintArray (int **arr, int N, int M)
{
for (int i = 0; i<N; i++)
{
for (int j = 0; j<M; j++)
std::cout << std::setw(7) << arr[i][j];
std::cout << "\n";
}
std::cout << "\n";
}
//----------------------------------------------------------------------------
typedef struct _min
{
int value;
int i;
int j;
} TMin;
//---------------------------------------------------------------------------
void FindMin (int **arr, int N, int M, TMin **mins, int min_count)
{
mins[min_count]->value = arr[0][0];
mins[min_count]->i = 0;
mins[min_count]->j = 0;
for (int i = 0; i<N; i++)
for (int j = 0; j<M; j++)
{
if (arr[i][j] < mins[min_count]->value)
{
bool prev = false;
for (int k = 0; k<min_count; k++)
if (i == mins[k]->i && j == mins[k]->j)
{
prev = true;
break;
}
if (!prev)
{
mins[min_count]->value = arr[i][j];
mins[min_count]->i = i;
mins[min_count]->j = j;
}
}
}
}
//----------------------------------------------------------------------------
int main()
{
//array creation, filling, printing
srand ((unsigned int)time (NULL));
int N = 6, M = 7;
int **arr = CreateArray (N, M);
PrintArray (arr, N, M);
//container for 3 minimums
TMin **mins = new TMin*[3];
for (int i = 0; i<3; i++)
mins[i] = new TMin;
//let us find them
int min_count = -1;
while (++min_count < 3)
FindMin (arr, N, M, mins, min_count);
//result
for (int i = 0; i<3; i++)
std::cout << "arr[" << mins[i]->i << "][" <<mins[i]->j << "] = " << mins[i]->value << "\n";
//cleaning memory
for (int i = 0; i<3; i++)
delete [] mins[i];
delete [] mins;
DestroyArray (arr, N);
std::cin.sync();
std::cin.get();
return 0;
}
//-----------------------------------------------------------------------------

How to turn 2d array out of 1d one?

To access any element I use *(Ptr + i).
Is there any way to put 2D array into the allocated memory in order to access any element using array[i][j]?
Here is the code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *Ptr;
Ptr = malloc(M*N*sizeof(int));
for (i = 0; i <= M * N; i++)
*(Ptr + i) = 1 + rand()%10;
return 0;
}
Sample
#include <stdio.h>
#include <stdlib.h>
#define N 4
#define M 3
int main()
{
int *Ptr;
int (*p)[M];
int i,j;
Ptr = malloc(M*N*sizeof(int));
for (i = 0; i < M * N; i++){
*(Ptr + i) = 1 + rand()%10;
// printf("%d ", Ptr[i]);
}
// printf("\n");
p=(int (*)[M])Ptr;//p[N][M]
for(i = 0; i < N ;++i){
for(j = 0; j < M;++j)
printf("%d ", p[i][j]);
printf("\n");
}
return 0;
}
Try this:
int** theArray;
theArray = (int**) malloc(M*sizeof(int*));
for (int i = 0; i < M; i++)
{
theArray[i] = (int*) malloc(N*sizeof(int));
}
Sample:
int M = 5;
int N = 5;
int** theArray = (int**) malloc(M*sizeof(int*));
for (int i = 0; i < M; i++)
{
theArray[i] = (int*) malloc(N*sizeof(int));
for(int j = 0 ; j < N; j++)
{
theArray[i][j] = i+j;
printf("%d ", theArray[i][j]);
}
printf("\n");
}
for (int k = 0; k < M; k++)
{
free(theArray[k]);
}
free(theArray);

Resources