Question is: Checking two matrices, if one is a sub matrix of another.
The problem I am facing here is a for loop as commented as "//problem" in the code. When for the first time the program runs, the mentioned for loop does not work as it should.
#include <stdio.h>
#define N 10
int
main ()
{
char matrix1[N][N], matrix2[N][N];
int i, j, row1, col1, row2, col2, k, l, m, n, check = 0;
//First Matrix
printf ("Enter the data\n");
printf ("Enter the size of rows\n");
scanf ("%d", &row1);
printf ("Enter the size of columns\n");
scanf ("%d", &col1);
printf ("Now enter the values please\n");
//Putting Values In First Matrix
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
printf ("Please enter the %dth row and %dth column\n", i + 1,
j + 1);
scanf ("%s", &matrix1[i][j]);
}
}
//Second Matrix
printf ("Enter the data\n");
printf ("Enter the size of rows\n");
scanf ("%d", &row2);
printf ("Enter the size of columns\n");
scanf ("%d", &col2);
printf ("Now enter the values please\n");
//Putting Values In Second Matrix
for (i = 0; i < row2; i++)
{
for (j = 0; j < col2; j++)
{
printf ("Please enter the %dth row and %dth column\n", i + 1,
j + 1);
scanf ("%s", &matrix2[i][j]);
}
}
//Checking Both Matrices
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
if (matrix1[i][j] == matrix2[0][0])
{
k = i;
l = j;
for (m = 0; m < row2; m++)
{
for (n = 0; n < col2; n++)
{ //problem
if (matrix1[k][l] == matrix2[m][n])
{
check++;
printf ("Checked\n");
}
l++;
}
l = j;
k++;
}
}
}
printf ("hello\n");
}
if (check == row2 * col2)
{
printf ("It exists\n");
}
else
{
printf ("It doesn't exist\n");
}
}
Here is the output:
Checked
hello
Checked
Checked
Checked
Checked
hello
hello
It doesn't exist
You need to reset check to zero before starting to find sub-matrix.
Also to break once you found it (or have flag to indicate if its found).
As from your output, (assuming you are trying to find 2x2 matrix) it found it Checked printed times continuously, but its value would be 5 counting for 1st print as well, which makes your program to print "It does not exist".
Like:
int is_found = 0;
... //some code
//Checking Both Matrices
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
check = 0; //reset check
if (matrix1[i][j] == matrix2[0][0])
{
... //your code to check matrix.
...
}//if end
if(check == row2*col2)
{
is_found = 1;
}
...
} //for j end
if(is_found)
break;
...
...
if(is_found)
printf("It exists\n");
Related
Newb Here!
Trying some things with C Language,
I tried to print Half left triangle/pyramid and Half right triangle/pyramid together in one print.
heres the code!
#include <stdio.h>
#include <conio.h>
int main()
{
int i, j, rows, k;
printf (" Enter a number to define the rows: \n ");
scanf("%d", &rows);
printf("\n");
for (i = 1; i <= rows; ++i) // outer loop
{
for (j = 1; j <= i; ++j) // inner loop
{
printf ("*"); // print the Star
}
printf ("\n");
}
{
for (i = 1; i <= rows; i++)
{
for (j = i; j < rows; j++)
{
printf(" ");
}
for (k = 1; k <= i; k++)
{
printf("*"); // print the Star
}
printf ("\n");
}
}
}
#include <stdio.h>
#include <conio.h>
int main()
{
int i, j, rows, k;
int hollow_spacing = 2;
int left_padding = 10;
printf (" Enter a number to define the rows: \n ");
scanf("%d", &rows);
printf("\n");
for (i = 0; i < rows; i++)
{
int curr_left_pad = left_padding + rows - i;
while(curr_left_pad-- > 0){
printf (" ");
}
for (int j = i; j >= 0; j--)
printf ("*");
for (int j = 0; j < hollow_spacing; ++j)
printf (" ");
for (int j = i; j >= 0; j--)
printf ("*");
printf ("\n");
}
}
I've been trying to check elements in a row of an 2D array and store the elements in a variable so that I can use the variable for comparing. I'm lost between how to check the elements and store it in a variable.
I've written this much:
#include<stdio.h>
#include<conio.h>
int main()
{
int array1[10][10];
int row, column, num, found = 0, passMark = 60, fail = 0;
printf("How many rows and columns needed: ");
scanf("%d %d", &row, &column);
printf("\nHow many students marks you want to enter: ");
scanf("%d", &num);
printf("\nEnter %d students marks: ", num);
for(int i=0; i<row; i++)
{
for(int j=0; j<column; j++)
{
scanf("%d", &array1[i][j]);
}
}
for(int i=0; i<num; i++)
{
// int passMark = array1[i][0];
for(int j=1; j<num; j++)
{
if(array1[i][j] >= passMark)
{
passMark = array1[i][j];
}
/*else
{
min = array1[i][j];
}*/
}
printf("\nYes!");
// found = 0;
}
return 0;
}
Couple of things wrong with the code.
You ask the user to input row/column but you have already declared an array of static size 10 * 10.
If you want the size to be determined by the user, you should use a dynamic array and make use of malloc() and free().
When you check, you start with j = 1 skipping one mark, you should start from 0.
You seem to be asking again how many student and how many marks per student you want to add, but you already got that information. The rows represent the student and columns represent the number of marks per student.
You should be looking for something like this, but again this is my guess, question is quite unclear.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int **array1;
int row, column, num, found = 0, passMark = 60, fail = 0;
printf("How many students and how many marks per student?\n");
if (scanf("%d %d", &row, &column) != 2)
{
/* conversion failed */
exit(EXIT_FAILURE);
}
array1 = malloc(row * sizeof(int *));
if (array1 == NULL)
{
exit(EXIT_FAILURE);
}
for (int i = 0; i < row; i++)
{
array1[i] = malloc(column * sizeof(int));
if (array1[i] == NULL)
{
/* allocation failed - exit / display error */
exit(EXIT_FAILURE);
}
}
printf("\nEnter %d students marks: ", row);
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
if (scanf("%d", &array1[i][j]) != 1)
{
exit(EXIT_FAILURE);
}
}
}
for (int i = 0; i < row; i++, fail = 0)
{
printf("Student's %d grades:\n", i + 1);
for (int j = 0; j < column; j++)
{
if (array1[i][j] >= passMark)
{
printf("Passed class %d with grade : %d\n", j + 1, array1[i][j]);
}
else
{
printf("Failed class %d with grade : %d\n", j + 1, array1[i][j]);
fail++;
}
}
printf("Student %d failed %d classes", i + 1, fail);
}
return 0;
}
I have been trying to create an NxN square matrix (the size of the square matrix is determined by the user) but seem to have run into an issue. The displayed matrix is actually a symmetric matrix which is not what I am trying to as I want to be able to have each value hold a unique integer. Any help is greatly appreciated. Here's my code:
#include <stdio.h>
int main()
{
int i;
int j;
int size = 1;
int array [size][size];
printf ("Enter the size you would like for the nxn matrix: ");
scanf ("%d", &size);
printf ("Now enter elements into the matrix. \n");
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf ("[%d][%d] = ", i, j);
scanf ("%d", &array [i][j]);
}
}
printf ("------------------------\n\n");
printf ("the matrix is: \n\n");
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf (" %d ", array [i][j]);
}
printf ("\n");
}
return (0);
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
// Create variables
int i, j, size;
// Create 2D array
printf ("Enter the size you would like for the nxn matrix: ");
scanf ("%d", &size);
int *array = (int *)malloc(size * size * sizeof(int));
// Fill 2D array with values
printf ("Now enter elements into the matrix. \n");
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf ("[%d][%d] = ", i, j);
scanf ("%d", &array[i * size + j]);
}
}
printf ("------------------------\n\n");
// Display values of 2D array
printf ("the matrix is: \n\n");
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf ("%d", array[i * size + j]);
}
printf ("\n");
}
free(array);
return (0);
}
Does anyone know how you would rewrite the program I wrote for polynomial multiplication but this time using a function to multiply those polynomials, print the result and a function to load the coefficients of the two polynomials? This is my original code -
#include<stdio.h>
main ()
{
int i, j, sizePoly1, sizePoly2;
printf ("Enter number of terms in Polynomial 1\n");
scanf ("%d", &sizePoly1);
printf ("Enter number of terms in Polynomial 2\n");
scanf ("%d", &sizePoly2);
int a[sizePoly1], b[sizePoly2], prod[sizePoly1 + sizePoly2];
printf ("Enter Elements of Polynomial 1\n");
for (i = 0; i < sizePoly1; i++)
{
printf ("Enter x^%d Co-Efficient of Polynomial 1\n", i);
scanf ("%d", &a[i]);
}
printf ("Enter Elements of Polynomial 2\n");
for (i = 0; i < sizePoly2; i++)
{
printf ("Enter x^%d Co-Efficient of Polynomial 2\n", i);
scanf ("%d", &b[i]);
}
for (i = 0; i < sizePoly1 + sizePoly2; i++)
{
prod[i] = 0;
}
for (i = 0; i < sizePoly1; i++)
{
for (j = 0; j < sizePoly2; j++)
{
if (a[i] != 0 && b[j] != 0)
prod[i + j] += a[i] * b[j];
}
}
for (i = sizePoly1 + sizePoly2 - 1; i >= 0; i--)
{
if (prod[i] != 0)
{
if (i != 0)
{
printf ("%d x^%d + ", prod[i], i);
}
else
{
printf ("%d x^%d\n", prod[i], i);
}
}
}
}
And this is what I came up with. I barely understand functions so that's why it doesn't work really -
#include<stdio.h>
int Coef (int i, int j , int a[sizePoly1], int b[sizePoly2],){
printf ("Enter Elements of Polynomial 1\n");
for (i = 0; i < sizePoly1; i++)
{
printf ("Enter x^%d Co-Efficient of Polynomial 1\n", i);
scanf ("%d", &a[i]);
}
printf ("Enter Elements of Polynomial 2\n");
for (i = 0; i < sizePoly2; i++)
{
printf ("Enter x^%d Co-Efficient of Polynomial 2\n", i);
scanf ("%d", &b[i]);
}
}
int Polyproduct (int i, int j, int sizePoly1, int size Poly2, int a[sizePoly1, int b[sizePoly2], int prod[sizePoly1 + sizePoly2] ){
for (i = 0; i < sizePoly1 + sizePoly2; i++)
{
prod[i] = 0;
}
for (i = 0; i < sizePoly1; i++)
{
for (j = 0; j < sizePoly2; j++)
{
if (a[i] != 0 && b[j] != 0)
prod[i + j] += a[i] * b[j];
}
}
}
int PolyPrint (int i, int sizePoly1, int sizePoly2, int prod[sizePoly1 + sizePoly2]) {
for (i = sizePoly1 + sizePoly2 - 1; i >= 0; i--)
{
if (prod[i] != 0)
{
if (i != 0)
{
printf ("%d x^%d + ", prod[i], i);
}
else
{
printf ("%d x^%d\n", prod[i], i);
}
}
}
}
int main ()
{
int i, j, sizePoly1, sizePoly2;
printf ("Enter number of terms in Polynomial 1\n");
scanf ("%d", &sizePoly1);
printf ("Enter number of terms in Polynomial 2\n");
scanf ("%d", &sizePoly2);
Coef();
Polyproduct();
PolyPrint();
}
See my comments. I elaborate a bit.
Your main could look like:
int main ()
{
int sizePoly1, sizePoly2;
printf ("Enter number of terms in Polynomial 1\n");
scanf ("%d", &sizePoly1);
printf ("Enter number of terms in Polynomial 2\n");
scanf ("%d", &sizePoly2);
int poly1[sizePoly1];
int poly2[sizePoly2];
int prod[sizePoly1 + sizePoly2];
Coef(sizePoly1, sizePoly2, poly1, poly2);
Polyproduct(sizePoly1, sizePoly2, poly1, poly2, prod);
PolyPrint(sizePoly1, sizePoly2, prod);
return 0;
}
Function Coef should be declared as:
int Coef (int sizePoly1, int sizePoly2 , int a[sizePoly1], int b[sizePoly2])
{
int i, j;
// ...
With warnings turned on, you will receive some additional warnings. Good luck!
I am practicing my C program currently and I want to calculate two 2D matrix.
But I get confused when I use printf() to check these arrays. They break the program.
Code as below:
int matric_multi_main()
{
int m1_row = 0;
int m1_col = 0;
int m2_row = 0;
int m2_col = 0;
int **matric1;
int **matric2;
printf("Please enter Matric 1 row: ");
scanf("%d", &m1_row);
printf("Please enter Matric 1 column: ");
scanf("%d", &m1_col);
printf("Please enter Matric 2 row: ");
scanf("%d", &m2_row);
printf("Please enter Matric 2 column: ");
scanf("%d", &m2_col);
if (m1_col != m2_row)
{
printf("Error matric size!!!\n");
return 0;
}
/* Allovate memory for matric 1 */
matric1 = malloc(m1_row * sizeof(int *));
for (int i = 0; i < m1_row; i++)
{
matric1[i] = malloc(m1_col * sizeof(int));
}
/* Allovate memory for matric 2 */
matric2 = malloc(m2_row * sizeof(int *));
for (int i = 0; i < m2_row; i++)
{
matric2[i] = malloc(m2_col * sizeof(int));
}
/* Init matric 1 */
for (int i = 0; i < m1_row; i++)
{
for (int j = 0; j < m1_col; j++)
{
printf("[%d, %d] = ", i, j);
scanf("%d", matric1+i*m1_row+j);
}
}
/* Init matric 2 */
for (int i = 0; i < m2_row; i++)
{
for (int j = 0; j < m2_col; j++)
{
printf("[%d, %d] = ", i, j);
scanf("%d", matric2+i*m2_row+j);
}
}
for (int i = 0; i < m1_row; i++)
{
printf("[");
for (int j = 0; j < m1_col; j++)
{
//printf("%d ", *(matric1 + i*m1_row + j)); <- correctly
//printf("%d ", &matric1[i][j]); <-- display the data with wrong order
printf("%d ", matric1[j][i]); <- break the program
}
printf("]\n");
}
for (int i = 0; i < m2_row; i++)
{
printf("[");
for (int j = 0; j < m2_col; j++)
{
//printf("%d ", *(matric2 + i*m2_row + j));
printf("%d ", matric2[i][j]);
}
printf("]\n");
}
}
I think I can use matric1[i][j] to get the correct data directly.
But the example on the website always use array[][] directly.
I can understand the different between my program and example.