Dec 30, 2011

C Program to add two polynomials using structures.

C Program to add two polynomials using structures. Structure is a c composite data type, in which we can define all the data types under the same name or object. Size of the Structure is the size of all data types, plus any internal padding. the key word "struct" is used to declare the structure. A Polynomial is a mathematical expression involving a sum of powers in one or more variables multiplied by coefficients. Read more about C Programming Language .

```/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
*                      Happy Coding
***********************************************************/

#include < stdio.h >
#include < conio.h >
#define MAX 20

struct addpolynomial {
int exp, coef;
};

void main() {
struct addpolynomial p1[MAX], p2[MAX], p3[MAX];
int max1, max2, max3;
clrscr();
printf("\nEnter first addpolynomial : ");
max1 = read_addpolynomial(p1);
printf("\nEnter second addpolynomial : ");
max2 = read_addpolynomial(p2);
max3 = add_addpolynomial(p1, p2, p3, max1, max2);
printf("\nFirst addpolynomial is ");
print_addpolynomial(p1, max1);
printf("\nSecond addpolynomial is ");
print_addpolynomial(p2, max2);
printf("\n The resultant addpolynomial after addition is");
print_addpolynomial(p3, max3);
}

//function to read polynomial
int read_addpolynomial(struct addpolynomial p[]) {
int i, texp;
i = 0;
printf("\nEnter exp ( use -1 to exit) : ");
scanf("%d", &texp);
while (texp != -1) {
p[i].exp = texp;
printf("\nEnter coef : ");
scanf("%d", &p[i].coef);
i++;
printf("\nEnter exp ( use -1 to exit) : ");
scanf("%d", &texp);
}
return (i);
}

//function to print polynomial
int print_addpolynomial(struct addpolynomial p[], intMAX1) {
int i;
for (i = 0; i < max1; i++)
printf("%+dX%d ", p[i].coef, p[i].exp);
return;
}

//function to ad polynomials
int add_addpolynomial( p1, p2, p3, max1, max2)
struct addpolynomial p1[], p2[], p3[];
intMAX1, max2;
{
int i,j,k;
i = j = k = 0;
while ( i <max1 && j <max2)
{
if( p1[i].exp > p2[j].exp)
{
p3[k] = p1[i];
k++;
i++;
}
else
if( p1[i].exp < p2[j].exp)
{
p3[k] = p2[j];
k++;
j++;
}
else
{
p3[k].exp = p1[i].exp;
p3[k].coef = p1[i].coef + p2[j].coef;
i++;
j++;
k++;
}
}
while( i <max1 )
{
p3[k] = p1[i];
k++;
i++;
}
while( j <max2 )
{
p3[k] = p2[j];
k++;
j++;
}
return(k);
}

```
```Array In C

C Data Structures

Simple C Programs```
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link

K & R C Programs Exercise 2-6.

K and R C, Solution to Exercise 2-6:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition) You can learn and solve K&R C Programs Exercise.
C Function setbits(x, p, n, y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged.Read more about C Programming Language .
```/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
*                      Happy Coding
***********************************************************/

#include <stdio.h>

unsigned setbits(unsigned x, int p, int n, unsigned y)
{
return (x & ((~0 << (p + 1)) | (~(~0 << (p + 1 - n))))) | ((y & ~(~0 << n)) << (p + 1 - n));
}

int main(void)
{
unsigned i;
unsigned j;
unsigned k;
int p;
int n;

for(i = 0; i < 30000; i += 511)
{
for(j = 0; j < 1000; j += 37)
{
for(p = 0; p < 16; p++)
{
for(n = 1; n <= p + 1; n++)
{
k = setbits(i, p, n, j);
printf("setbits(%u, %d, %d, %u) = %u\n", i, p, n, j, k);
}
}
}
}
return 0;
}

```
```Read more Similar C Programs
C Basic

K and R C Programs Exercise```

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link

K & R C Programs Exercise 2-5.

K and R C, Solution to Exercise 2-5:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language, second addition, by Brian W.Keringhan and Dennis M.Ritchie(Prentice Hall,1988). You can learn and solve K&R C Programs Exercise.
Write the C function any(s1, s2), which returns the first location in the string s1 where any character from the string s2 occurs, or -1 if s1 contains no characters from s2.The standard library function strpbrk does the same job but returns a pointer to the location.Read more about C Programming Language .

```/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
*                      Happy Coding
***********************************************************/

int any(char s1[], chars2[])
{
int i, j;
for(i=0;s1[i]!='\0';i++)

for(j=0;s2[[j]!='\0' ;j++)

if(s1[i]==s2[j])
return i;

return -1;
}
```
```Read more Similar C Programs
C Basic

K and R C Programs Exercise```

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link

K & R C Programs Exercise 2-4.

K and R C, Solution to Exercise 2-4:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language, second addition, by Brian W.Keringhan and Dennis M.Ritchie(Prentice Hall,1988). You can learn and solve K&R C Programs Exercise.
Write an alternate version of squeeze(s1,s2) that deletes each character in s1 that matches any character in the string s2.
Compare ,First string with the second string, if any character matched,  delete the matched character of the first string. Read more about C Programming Language .

```/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
*                      Happy Coding
***********************************************************/

/*squeeze: delete each char in s1 which is in s2*/
void squeeze(char s1[], chars2[])
{
int i, j, k;
for(i=k=0;s1[i]!='\0';i++)
{
for(j=0;s2[[j]!='\0' && s2[j]!=s1[i];j++)
;
if(s2[j]=='\0')
s1[k++]=s1[i];
}
s1[k]='\0';
}
```
```Read more Similar C Programs
C Basic

K and R C Programs Exercise```

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link