This is the code i am talking about. It checks if a matrix is sparse or not . If it is then it changes it to sparse form and transposes it. When I run it with Vscode it runs and displays till it is sparse matrix but doesn't show its sparse form and transpose form but on online compiler it runs successfully till the end.
#include <stdio.h>
int checksparse();
int changematrix();
int main()
{
int a[100][100];
int i, j, r, c;
printf("Enter number of rows and columns in the matrix\n");
scanf("%d%d", &r, &c);
printf("\nEnter the elements in the matrix");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("\na[%d][%d] = ", i, j);
scanf("%d", &a[i][j]);
}
}
int check = checksparse(a, r, c);
if (check == 1)
{
printf("\nIts a sparse matrix\n");
changematrix(a, r, c);
}
else if (check == 0)
{
printf("\nIts not a sparse matrix");
}
return 0;
}
int checksparse(int array[100][100], int r, int c)
{
int counter = 0, flag = 1;
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
if (array[i][j] == 0)
counter++;
}
}
if (counter > ((r * c) / 2))
{
return flag;
}
else
{
flag = 0;
return flag;
}
}
int changematrix(int array[100][100], int r, int c)
{
int b[100][100], temp, counter = 0;
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
if (array[i][j] != 0)
{
b[temp][0] = i;
b[temp][1] = j;
b[temp][2] = array[i][j];
temp++;
counter++;
}
}
}
printf("\nSparse Matrix : \n");
for (int i = 0; i < counter; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ", b[i][j]);
printf("\t");
}
printf("\n");
}
printf("\nTranspose of Sparse Matrix : \n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < counter; j++)
{
printf("%d ", b[j][i]);
printf("\t");
}
printf("\n");
}
return 0;
}
Related
#include<stdio.h>
int a, b;
float arr[10][10];
void iden(float x[a][b]);
void diag(float x[a][b]);
void pri_sec(float x[a][b]);
int main() {
int i, j, n;
arr[a][b];
for (i = 0;; i++) {
printf("Enter the no. of rows and columns:\n");
scanf("%d %d", &a, &b);
if (a == b) {
printf("Enter the values in an array:\n");
for (i = 0; i < a; i++) {
for (j = 0; j < b; j++)
scanf("%f", &(arr[i][j]));
}
printf("Enter 1 to check it is an identity matrix or not, 2 to check it is diagonal matrix or not\
,3 to check principal and secondary diagonal are equal or not and any other no. to exit:\n");
scanf("%d", &n);
if (n == 1)
iden(arr);
if (n == 2)
diag(arr);
if (n == 3)
pri_sec(arr);
else
return 0;
} else {
printf("It is not a square matrix:\n");
return 0;
}
}
}
void iden(float x[a][b]) {
int i, j, flag = 1;
for (i = 0; i < a; i++) {
for (j = 0; j < b; j++) {
if (x[i][j] != 1 && x[j][i] != 0)
flag = 0;
}
}
if (flag == 1)
printf("It is an identity matrix:\n");
else
printf("It is not an identity matrix:\n");
}
void diag(float x[a][b]) {
int i, j, flag = 0;
for (i = 0; i < a; i++) {
for (j = 0; j < a; j++) {
if ((i == j && x[i][j] == 0) || (i != j && x[i][j] != 0)) {
flag = 1;
break;
}
}
}
if (flag == 0)
printf("It is a diagonal matrix:\n");
else
printf("It is not a diagonal matrix:\n");
}
void pri_sec(float x[a][b]) {
int i, k, flag = 0;
for (i = 0; i < a; i++) {
k = a - 1 - i;
if (x[i][i] != x[i][k]) {
flag = 1;
}
}
if (flag == 1)
printf("The principal and secondary diagonals are not equal:\n");
else
printf("The principal and secondary diagonals are equal:\n");
}
When I changed the indices as 10 10 for the functions it worked but for the other case it didn't.
#include <stdio.h>
int x[10][10];
int ROWS, COLS;
void func_vla(int array[10][10]) {
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
array[i][j] = i * j;
}
}
}
int main() {
scanf("%d %d", &ROWS, &COLS);
int x[ROWS][COLS], i, j;
func_vla(x);
for (i = 0; i < ROWS; i++)
for (j = 0; j < COLS; j++)
printf("%d\n", x[i][j]);
}
the Question is: to get from the user sodoku board and if there is a solution to print it, if not to print no solution!
the solution of the soduko: two identical numbers mmust not appear on the same line;
two identical numbers must not appear in the same colum.
I worte a program that works perfectly when I put the soduko board and the size (global parametes-as shown un my code) but when I tried to receive from the user it took so much time to run the solution and sometimes it didn't retun anything. I would like to understand why?!
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
int matrix[5][5] = {
{4,2,0,0,5},
{2,0,0,1,3},
{5,0,1,2,0},
{0,0,3,0,2},
{0,0,0,0,0},
};
void print_sudoku()
{
int i,j;
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
{
printf("%d ",matrix[i][j]);
}
printf("\n");
}
}
int number_unassigned(int *row, int *col)
{
int num_unassign = 0;
int i,j;
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
{
if(matrix[i][j] == 0)
{
*row = i;
*col = j;
num_unassign = 1;
return num_unassign;
}
}
}
return num_unassign;
}
int is_safe(int n, int r, int c)
{
int i;
for(i=0;i<SIZE;i++)
{
if(matrix[r][i] == n)
return 0;
}
for(i=0;i<SIZE;i++)
{
if(matrix[i][c] == n)
return 0;
}
return 1;
}
int solve_sudoku()
{
int row;
int col;
if(number_unassigned(&row, &col) == 0)
return 1;
int i;
for(i=1;i<=SIZE;i++)
{
if(is_safe(i, row, col))
{
matrix[row][col] = i;
if(solve_sudoku())
return 1;
matrix[row][col]=0;
}
}
return 0;
}
int main()
{
if (solve_sudoku())
print_sudoku();
else
printf("No solution!\n");
return 0;
}
and this is whe code that I used to ask the user to enter a sodoku board:
int** ReadSoduko(int n) {
int** matrix = (int**) malloc((sizeof(int*)) * n);
for(int i = 0; i < n; ++i) {
matrix[i] = malloc(sizeof(int) * n);
}
printf("\nEnter your soduko board:\n");
for(int i = 0; i < n; ++i) {
printf("Enter row [%d]: ", i);
for(int j = 0; j < n; ++j) {
scanf("%d", &matrix[i][j]);
}
}
return matrix;
}
I suspect your problem is related to one thing: #define SIZE that you are probably forgetting to update when reading dynamically. Since you use SIZE in your loops, if that is not the real size of your matrix, then it probably won't work. I've changed 2 lines and added 3 other (the lines with comments at the end). Try it now.
#include <stdio.h>
#include <stdlib.h>
int SIZE; //changed here
int** matrix; //changed here
void print_sudoku()
{
int i, j;
for (i = 0; i < SIZE; i++)
{
for (j = 0; j < SIZE; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int number_unassigned(int* row, int* col)
{
int num_unassign = 0;
int i, j;
for (i = 0; i < SIZE; i++)
{
for (j = 0; j < SIZE; j++)
{
if (matrix[i][j] == 0)
{
*row = i;
*col = j;
num_unassign = 1;
return num_unassign;
}
}
}
return num_unassign;
}
int is_safe(int n, int r, int c)
{
int i;
for (i = 0; i < SIZE; i++)
{
if (matrix[r][i] == n)
return 0;
}
for (i = 0; i < SIZE; i++)
{
if (matrix[i][c] == n)
return 0;
}
return 1;
}
int solve_sudoku()
{
int row;
int col;
if (number_unassigned(&row, &col) == 0)
return 1;
int i;
for (i = 1; i <= SIZE; i++)
{
if (is_safe(i, row, col))
{
matrix[row][col] = i;
if (solve_sudoku())
return 1;
matrix[row][col] = 0;
}
}
return 0;
}
int** ReadSoduko(int n) {
int** matrix = (int**)malloc((sizeof(int*)) * n);
for (int i = 0; i < n; ++i) {
matrix[i] = (int*) malloc(sizeof(int) * n);
}
printf("\nEnter your soduko board:\n");
for (int i = 0; i < n; ++i) {
printf("Enter row [%d]: ", i);
for (int j = 0; j < n; ++j) {
scanf("%d", &matrix[i][j]);
}
}
return matrix;
}
int main()
{
printf("Size of matrix: "); //added this
scanf("%d", &SIZE); //added this
matrix = ReadSoduko(SIZE); //added this
if (solve_sudoku())
print_sudoku();
else
printf("No solution!\n");
return 0;
}
And don't forget that the matrix that you have declared statically doesn't have a solution! I have tested the same matrix, just replacing the 2 in first line by a 0 and it worked here.
Hi everyone I made some changes in the code so that it's easy to understand, looks what I have so far, I kept the function's prototype at the beginning of the code and it works fine, but it just works fine just when I try a 2x2 matrix because if I try a 3x3, 4x4 or 6x6 matrix it does not works fine the determinant is not calculated right, I guess that's the problem the determinant but I don't know how to solve it. Here is the code.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<math.h>
float determinant(int tam,float [][tam]);
void cofactor(int tam,float [][tam]);
void transpose(int tam,float [][tam],float [][tam]);
int validate(){
int val;
char *buf = (char *) malloc(10);
memset(buf,0,10);
while(fgets(buf, 10, stdin) != NULL ){
if(buf[0]!='\n') {
val = atoi(buf);
break;
}
}
free(buf);
return val;
}
float determinant(int tam, float matrix[][tam])
{
float s = 1, det = 0, b[tam][tam];
int i, j, m, n, c;
if (tam == 1)
{
return (matrix[0][0]);
}
else
{
det = 0;
for (c = 0; c < tam; c++)
{
m = 0;
n = 0;
for (i = 0;i < tam; i++)
{
for (j = 0 ;j < tam; j++)
{
b[i][j] = 0;
if (i != 0 && j != c)
{
b[m][n] = matrix[i][j];
if (n < (tam - 2))
n++;
else
{
n = 0;
m++;
}
}
}
}
det = det + s * (matrix[0][c] * determinant( tam - 1,b));
s = -1 * s;
}
}
return (det);
}
void cofactor( int tam,float num[][tam])
{
float b[tam][tam], fac[tam][tam];
int p, q, m, n, i, j;
float x = 0;
for (q = 0;q < tam; q++)
{
for (p = 0;p < tam; p++)
{
m = 0;
n = 0;
for (i = 0;i < tam; i++)
{
for (j = 0;j < tam; j++)
{
if (i != q && j != p)
{
b[m][n] = num[i][j];
if (n < (tam - 2))
n++;
else
{
n = 0;
m++;
}
}
}
}
x = pow(-1, q + p) * determinant( tam - 1,b);
fac[p][q] = x;
}
}
transpose(tam,num, fac);
}
void transpose(int tam,float num[][tam], float fac[][tam])
{
int i, j;
float b[tam][tam], inverse[tam][tam], d;
for (i = 0;i < tam; i++)
{
for (j = 0;j < tam; j++)
{
b[i][j] = fac[j][i];
}
}
d = determinant(tam,num);
for (i = 0;i < tam; i++)
{
for (j = 0;j < tam; j++)
{
inverse[i][j] = b[i][j] / d;
}
}
printf("\n\n\nThe inverse of matrix is : \n");
for (i = 0;i < tam; i++)
{
for (j = 0;j < tam; j++)
{
printf("\t%f", inverse[i][j]);
}
printf("\n");
}
}
int verify_Size(int line){
if((line > 0) && (line <= 6))
{
return 1;
}
return 0;
}
int create_Matrix(int LINE){
int matrix[LINE][LINE];
float aux[LINE][LINE];
printf("\n\n\nPle:", (LINE * LINE));
printf("\n--------------------------------\n");
for(int i=0;i<LINE;i++)
{
for(int j=0;j<LINE;j++)
{
printf("Value[%d][%d]: ",i,j);
matrix[i][j] = validate();
}
}
printf("\n\nYour Bidimensional Matrix is:");
printf("\n--------------------------------\n");
for(int i=0;i < LINE;i++)
{
for(int j=0; j< LINE;j++)
{
printf("\t%2d",matrix[i][j]);
aux[i][j] = (float) matrix[i][j];
}
printf("\n");
}
float d = determinant(LINE,aux);
printf("\n\nDeterminante Main: %f \n",d);
if (d == 0)
printf("\nInverse of Entered Matrix is not possible\n");
else
cofactor(LINE,aux);
return 0;
}
int main(){
int flag,line;
do{
printf("Enter the order of the Matrix:\n");
printf("-------------------------------\n");
printf("Lines: ");
line = validate();
if(verify_Size(line)== 1)
{
create_Matrix(line);
}else{
printf("\nMatrix must to be till 6 X 6!\n");
flag = 0;
}
}while(flag != 1);
return 0;
}
It looks like you are finding the inverse matrix by Cramer's rule. While it works Ok for 2x2 or 3x3 matrix sizes, the hard part about implementing Cramer's rule generally is evaluating determinants. If you compute an NxN determinant following the definition, the computation is recursive and has factorial O(N!) computational complexity (Wikipedia).
I suggest switching to another algorithm. LUP factorization is fast and relatively simple, and Wikipedia has an example implementation.
There are two 2D arrays and r and c are their row and column, i and j are used for integer variables.
Transpose of matrix is running well till the value r and c are entered.
#include <stdio.h>
int main() {
// your code goes here
int a[50][50], b[50][50], i, j, r, c;
printf("Enter the value of R and c");
scanf("%d%d", &r, &c);
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
scanf("%d", &a[i][j]);
}
}
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
b[i][j] = a[i][j];
}
}
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
printf("%d\n", b[i][j]);
}
printf("\n");
}
return 0;
}
You are not "transposing" the matrix. You are simply copying one array to another. To transpose a matrix, you need to convert rows to columns:
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
b[j][i]=a[i][j];
}
}
and reverse c and r in the loop condition when printing:
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
{
printf("%d\n",b[i][j]);
}
printf("\n");
}
Note that your arrays are capable of handling 50x50. So you need to ensure the input values of r and c don't exceed these limits.
Your code is
#include <stdio.h>
int main() {
// your code goes here
int a[50][50], b[50][50], i, j, r, c;
printf("Enter the value of R and c\n");
scanf("%d%d", &r, &c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%3d",a[i][j]);
}
printf("\n");
}
printf("\n");
for(i = 0; i < c; i++)
{
for(j = 0; j < r; j++)
{
printf("%3d", a[j][i]);
}
printf("\n");
}
return 0;
}
I've been working on a program that generates an array of random numbers, splits the array into two equal parts, sorts each part, and then recombines the two parts back into a singular sorted array. When attempting an insert sort I get the following:
http://i.imgur.com/70J4eLG.png
Can you guys see a reason why the first half array is sorted correctly but the second half is not?
int cmpfunc (const void *a, const void *b) {
return ( *(int*)a - *(int*)b );
}
void insertion_sort (int ar[], int size) {
int c, d, t;
for (c = 1; c <= size - 1; c++){
d = c;
while(d > 0 && ar[d] < ar[d - 1]) {
t = ar[d];
ar[d] = ar[d - 1];
ar[d - 1] = t;
d--;
}
}
}
void check_sort (int ara[], int size_t) {
int b;
int c_i;
c_i = 0;
for (b = 1; b < size_t; b++) {
if (ara[b - 1] > ara[b]) {
printf("Array is not sorted correctly\n");
break;
} else {
c_i++;
}
}
if (c_i == size_t - 1) {
printf("Array is sorted correctly\n");
}
}
void combine_array(int a_ar[], int b_ar[], int c_ar[], int size_1, int size_2) {
int i, j, k;
i, j, k = 0;
while (i < size_1 && j < size_2) {
if (a_ar[i] < b_ar[j]) {
c_ar[k] = a_ar[i];
i++;
} else {
c_ar[k] = b_ar[j];
j++;
}
k++;
}
if (i >= size_1) {
while (j < size_2) {
c_ar[k] = b_ar[j];
j++;
k++;
}
}
if (j >= size_2) {
while (i < size_1) {
c_ar[k] = a_ar[i];
i++;
k++;
}
}
}
int main (int argc, char *argv[]) {
int a_size, t_num;
char s_type;
int i, j, k;
int two_s[1];
a_size = atoi(argv[1]);
t_num = atoi(argv[2]);
s_type = argv[3][0];
int array_m[a_size];
for (i = 0; i < a_size; i++) {
array_m[i] = rand();
}
for (i = 0; i < a_size; i++) {
printf("%d \n", array_m[i]);
}
printf("\n");
if (t_num == 2) {
two_s[0] = ((a_size/2));
two_s[1] = (a_size);
int array_s1[two_s[0]];
int array_s2[two_s[0]];
printf("First half \n");
for (j = 0; j < two_s[0]; j ++) {
array_s1[j] = array_m[j];
printf("%d \n", array_s1[j]);
}
printf("Second half \n");
for (k = two_s[0]; k < two_s[1]; k++) {
array_s2[k] = array_m[k];
printf("%d \n", array_s2[k]);
}
printf("Size of second array: %d", (two_s[1] - two_s[0]));
printf("\n");
check_sort(array_m, a_size);
if (s_type == 'I') { //Insertion sort
insertion_sort(array_s1, two_s[0]);
insertion_sort(array_s2, two_s[0]);
printf("Sorted first half \n");
for (i = 0; i < two_s[0]; i++) {
printf("%d \n", array_s1[i]);
}
printf("Sorted second half \n");
for (i = 0; i < two_s[0]; i++) {
printf("%d \n", array_s2[i]);
}
//combine_array(array_s1, array_s2, array_m, two_s[0], two_s[0]);
printf("\n");
printf("Combined and sorted \n");
for (i = 0; i < a_size; i++) {
printf("%d \n", array_m[i]);
}
check_sort(array_m, a_size);
}
if (s_type == 'Q') { //Quick sort
qsort(array_m, a_size, sizeof(int), cmpfunc);
printf("\n");
for (i = 0; i < a_size; i++) {
printf("%d \n", array_m[i]);
}
}
}
}
In your example, array_s2 is an array with 5 elements. In the following for loop, you're writing to memory outside the bounds of the array:
for (k = two_s[0]; k < two_s[1]; k++) {
array_s2[k] = array_m[k];
printf("%d \n", array_s2[k]);
}
k has an initial value of 5 and a final value of 9, which is outside the bounds of array_s2. Try the following to correctly index the array:
for (k = two_s[0]; k < two_s[1]; k++) {
array_s2[k - two_s[0]] = array_m[k];
printf("%d \n", array_s2[k - two_s[0]]);
}