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!
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 thought of declaring the variables first, and then finding the gcd and lcm. But when I tried to run, my code is not working. And the VS code is not even showing the errors. I am posting my code here:
#include <stdio.h>
int gcd(int a, int b)
{
for (int j = 1; j <= a && j <= b; ++j)
{
if (a % j == 0 && b % j==0)
return j;
}
}
int main ()
{
int i, n, pro=1, g, t, lcm;
int num[n];
printf ("Enter the no. of numbers: ");
scanf ("%d", &n);
for (i = 0; i <= n-1; i++)
{
printf ("Enter the number: ");
scanf ("%d", &num[i]);
}
g = gcd (num[0], num[1]);
for (t=2; t <= n; t++)
g = gcd(num[t], g);
for (i=0; i <= n-1; i++)
pro = pro*num[i];
lcm = pro/g;
printf ("GCD is %d\n", g);
printf ("LCM is %d", lcm);
return 0;
}
Well, I tried doing it again. The problem with my code was that the return j was in the loop, and thus, it was returning the value of j instead of giving the value of g.
The corrected code:
#include <stdio.h>
int gcd(int a, int b)
{
int g;
for (int j = 2; j <= a && j <= b; ++j)
{
if (a % j == 0 && b % j==0)
g = j;
}
return g;
}
int main ()
{
int i, n, pro=1, g, t, lcm;
printf ("Enter the no. of numbers: ");
scanf ("%d", &n);
int num[n];
for (i = 0; i <= n-1; i++)
{
printf ("Enter the number: ");
scanf ("%d", &num[i]);
}
g = gcd (num[0], num[1]);
for (t=2; t <= n; t++)
g = gcd(num[t], g);
for (i=0; i <= n-1; i++)
pro = pro*num[i];
lcm = pro/g;
printf ("GCD is %d\n", g);
printf ("LCM is %d", lcm);
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);
}
The problem is this http://www.spoj.com/problems/CMPLS/
I thought of implementing newtons divided difference and find out the polynomial. But I am not able to get the correct answer. Some basic test cases are passing but some are not.
# include <stdio.h>
int main()
{
int i, j, k, t, m, x[100][100], s, c, n;
long ans, mul;
scanf ("%d", &t);
while (t--) {
scanf ("%d%d", &s, &c);
n = s;
for (i = 0; i < n; i++)
scanf ("%d", &x[i][0]);
for (j = 1; j < n; j++)
for (i = 0; i < n; i++) {
x[i][j] = x[i+1][j-1] - x[i][j-1];
}
k = n;
printf("\n");
for (i = 0; i < n; i++) {
for(j = 0; j < k; j++)
printf ("%d ", x[i][j]);
printf ("\n");
k--;
}
printf ("\n\n");
for (m = 1; m <= c; m++) {
ans = x[0][0];
for ( i = 1; i < n - 1; i++) {
mul = x[0][i];
for (j = 1; j <= i; j++)
mul = mul * (s + m - j);
ans = ans + mul;
}
printf ("%ld ", ans);
}
printf ("\n");
}
return 0;
}
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");