Finding the smallest element in multidimensional array - c

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

Related

Expression must have arithmetic type issue

I need to multiply two square matrixes A and B 15x15.
Unfortunately, I'm getting this kind of error.
I know the problem is in pointers while calculating matrix C.
C[i][j] += *(A + k) * *(B + k)
I hope you can explain me what's wrong. I'm a beginner xD.
Thank you in advance.
#include <stdio.h>
#define N 15
#define _CRT_SECURE_NO_WARNINGS
int main() {
int A[N][N];
int B[N][N];
int C[N][N];
printf("Input matrix A.\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("Enter your element:\n");
scanf_s("%d", &A[i][j]);
}
printf("\n");
}
printf("Input matrix B.\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("Enter your element:\n");
scanf_s("%d", &B[i][j]);
}
printf("\n");
}
printf("Matrix A.\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d\t", A[i][j]);
}
printf("\n");
}
printf("Matrix B.\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d\t", B[i][j]);
}
printf("\n");
}
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
C[i][j] = 0;
for (int k = 0; k < 14; k++) {
C[i][j] += *(A + k) * *(B + k);
k++;
}
}
}
printf("Your result:\n");
printf("Matrix C.\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d\t", C[i][j]);
}
printf("\n");
}
return 0;
}
The problem in the multiplication is that A+k and B+k have type int (*)[15] which means dereferencing it once only makes a pointer out of them; furthermore, you need to take row and column items individually, which means A[i][k] and B[k][j], right? (also, there's no point on using confusing syntax, as the underlying operation is exactly the same).
Here's a fixed and improved version:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define N 15
/* Improvement 1 (type abstraction) */
typedef int NxN_int_matrix[N][N];
/* Improvement 2 (input function & wrapper) */
#define input_matrix(var) input_matrix_ex((var), #var)
static void input_matrix_ex(NxN_int_matrix dst, char *name)
{
printf("Input matrix %s.\n", name);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
/* Improvement 3 (nicer prompt) */
printf("%s[%2d][%2d]: ", name, i, j);
fflush(stdout);
scanf_s("%d", &dst[i][j]);
}
}
printf("\n");
}
/* Improvement 4 (print function) */
#define print_matrix(var) print_matrix_ex(#var, (var))
static void print_matrix_ex(char *name, NxN_int_matrix M)
{
printf("Matrix %s.\n", name);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d\t", M[i][j]);
}
printf("\n");
}
}
/* Improvement 5 (move multiplication to a function too, and fix it) */
static void mult_matrix(NxN_int_matrix dst, NxN_int_matrix a, NxN_int_matrix b)
{
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
/* Improvement 6 (don't write out intermediate values) */
int tmp = 0;
for (int k = 0; k < N; k++)
tmp += a[i][k] * b[k][j];
dst[i][j] = tmp;
}
}
}
int main()
{
NxN_int_matrix A, B, C;
input_matrix(A);
input_matrix(B);
print_matrix(A);
print_matrix(B);
mult_matrix(C, A, B);
printf("Your result:\n");
print_matrix(C);
return 0;
}
/* Possible further improvements:
* - using a transposed B might make multiplication faster
*/

Problem adding two 4x4 matrices and printing the sum result in a new matrix in C

Could someone help me please?
I need to perform the sum of two matrices that the data will be sent by the user and print the result in a new matrix.
I managed to capture the data from the two arrays, but when I try to add the two, the code does not print the sum, where is the error?
Thanks
#include <stdio.h>
#include <stdlib.h>
void sum(int *mat_A, int *mat_B, int *mat_C);
int main() {
int mat_A[4][4];
int mat_B[4][4];
int mat_C[4][4];
int i, j, value;
printf("\nEnter integer values ​​for the elements of matrix A: \n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("\nElement[%d][%d] = ", i, j);
scanf_s("%d", &value);
mat_A[i][j] = value;
}
}
printf("\nEnter integer values ​​for the elements of matrix B: \n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("\nElement[%d][%d] = ", i, j);
scanf_s("%d", &value);
mat_B[i][j] = value;
}
}
calc_soma(*mat_A, *mat_B, *mat_C);
printf("\nSum of matrices A with B: \n\n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
value = mat_C[i][j];
printf("%d", value);
}
printf("\n");
}
return 0;
}
void sum(int *mat_A, int *mat_B, int *mat_C) {
int i, j;
int value;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
value = *mat_A + *mat_B;
*mat_C = value;
}
}
}
You need to declare the parameters to sum() as 2-dimensional arrays, not int *, which is a pointer to a 1-dimensional array. Then use i and j as array indexes.
You're also calling the function with the wrong name calc_soma.
#include <stdio.h>
#include <stdlib.h>
void sum(int mat_A[4][4], int mat_B[4][4], int mat_C[4][4]);
int main() {
int mat_A[4][4];
int mat_B[4][4];
int mat_C[4][4];
int i, j, value;
printf("\nEnter integer values for the elements of matrix A: \n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("\nElement[%d][%d] = ", i, j);
scanf_s("%d", &value);
mat_A[i][j] = value;
}
}
printf("\nEnter integer values for the elements of matrix B: \n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("\nElement[%d][%d] = ", i, j);
scanf_s("%d", &value);
mat_B[i][j] = value;
}
}
sum(mat_A, mat_B, mat_C);
printf("\nSum of matrices A with B: \n\n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
value = mat_C[i][j];
printf("%d", value);
}
printf("\n");
}
return 0;
}
void sum(int mat_A[4][4], int mat_B[4][4], int mat_C[4][4]) {
int i, j;
int value;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
value = mat_A[i][j] + mat_B[i][j];
mat_C[i][j] = value;
}
}
}

What's wrong with this code for multiplying two matrices in C?

The aim of the matrixMultiplier function is to multiply A * B, and then show the resulting matrix C, for any two square matrices A and B. A and B sizes are limited by 10*10.
When the user is asked the size of the matrix they wish to introduce, although A and B are 10*10, the function only works with the submatrices in A and B up to the dimension which the user has specified, say 3*3.
I've tested this out multiplying the 3*3 identity matrix by itself and it's not giving me the correct answer.
#include <stdio.h>
void matrixMultiplier(int A[][10], int B[][10], int C[][10], int n) {
int i, j, k;
for (i = 0; i < n; i++) {
for (k = 0; k < n; k++) {
C[i][k] = 0; /* Initialize output matrix to zero */
}
}
for (i = 0; i < n; i++) {
for (k = 0; k < n; k++) {
for (j = 0; j < n; j++) {
C[i][k] += A[i][j] * B[j][k];
}
}
}
printf("\n");
for (i = 0; i < n; i++) {
for (k = 0; k < n; k++) {
printf("%4d", C[i][k]);
}
printf("\n");
}
}
int main(void) {
int A[][10] = {{0}};
int B[][10] = {{0}};
int C[][10] = {{0}}; /* Initialize output matrix to zero */
int i, j;
int n;
printf("Enter square matrix dimension: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("Assign a value: ");
scanf("%d", &A[i][j]);
}
printf("New row.\n");
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%4d", A[i][j]);
}
printf("\n");
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("Assign a value: ");
scanf("%d", &B[i][j]);
}
printf("New row.\n");
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%4d", B[i][j]);
}
printf("\n");
}
matrixMultiplier(A, B, C, n);
return 0;
}
You need to use statically allocated arrays like this:
int A[10][10] = {{0}};
int B[10][10] = {{0}};
int C[10][10] = {{0}}; /* Initialize output matrix to zero */
As other commentators noticed, in C int A[][10] = {{0}}; is basically an equivalent of int A[1][10] = {{0}};, so only first rows are correctly set.
Take a look at this code:
#include <stdio.h>
#define MAX_SIZE 10
void matrixMultiplier(int A[][MAX_SIZE], int B[][MAX_SIZE], int C[][MAX_SIZE], int n)
{
int i, j, k;
for (i = 0; i < n; i++)
for (k = 0; k < n; k++)
for (j = 0; j < n; j++)
C[i][k] += A[i][j] * B[j][k];
printf("\nProduct Matrix C:\n\n");
for(i = 0; i < n; ++i)
{
for(j = 0; j < n; ++j)
printf("%4d", C[i][j]);
putchar('\n');
}
}
int main(void)
{
//int A[][10] = {{0}}; By initializing like this you are creating this int A[1][10] = {{0}};
int A[MAX_SIZE][MAX_SIZE] = {0};
int B[MAX_SIZE][MAX_SIZE] = {0};
int C[MAX_SIZE][MAX_SIZE] = {0};
int i, j, n;
printf("Enter square matrix dimension: ");
scanf("%d", &n);
printf("Input %d values row-wise, for matrix A: ", n*n);
for(i = 0; i < n; ++i)
for(j = 0; j < n; ++j)
scanf("%d", &A[i][j]);
printf("Input %d values row-wise, for matrix B: ", n*n);
for(i = 0; i < n; ++i)
for(j = 0; j < n; ++j)
scanf("%d", &B[i][j]);
printf("\nGiven Matrix A:\n\n");
for(i = 0; i < n; ++i)
{
for(j = 0; j < n; ++j)
printf("%4d", A[i][j]);
putchar('\n');
}
printf("\nGiven Matrix B:\n\n");
for(i = 0; i < n; ++i)
{
for(j = 0; j < n; ++j)
printf("%4d", A[i][j]);
putchar('\n');
}
matrixMultiplier(A, B, C, n);
return 0;
}
If you need more explanation about 2D arrays refer to this link

Increasing number of rows in 2D c-array

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;

Pointers exit code 6

Well i m struggling with the pointers, why doesn t work the function biggest in the end (yes of that s dummy function)? (exit code 6)
Ty for help
code:
int search(int const a[], int n, int key) {
for (int *i = a; i < a + n; i++) {
if ( key == *i ) return 1;
}
return 0;
}
void print_row(int const a[], int n, int row) {
for (int *i = a + n * row; i < a + n * (row + 1); i++) {
printf("%d ", *i);
}
printf("\n");
}
void biggest(double x, long *int_part, double *frac_part) {
*int_part = (long) x;
*frac_part = x - *int_part;
}
main () {
int tempretures[7][24];
for (int *i = &tempretures[0][0]; i < &tempretures[7][24]; i++) {
static int j = 1;
*i = j;
j+=2;
}
tempretures[6][5] = 32;
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 24; j++) {
printf("%d ", tempretures[i][j]);
}
printf("\n");
}
printf("Is it: %d\n", search(tempretures, 7*24, 32));
for (int i = 0; i < 7; i++) {
print_row(tempretures, 24, i);
}
long a = 0; double b = 0;
biggest(5.67, &a, &b);
printf("%li", a);
}
for (int *i = &tempretures[0][0]; i < &tempretures[7][24]; i++) {
static int j = 1;
*i = j;
j+=2;
}
This runs way past the end of the array, stopping only when it hits every element of row number 7, but there is no row number 7.

Resources