Showing posts with label arrays. Show all posts
Showing posts with label arrays. Show all posts
Wednesday, February 11, 2015
C Program for Addition and Multiplication of Polynomial Using Arrays or Linked List
Polynomial addition, multiplication (8th degree polynomials) using arrays
#include<math.h>
#include<stdio.h>
#include<conio.h>
#define MAX 17
void init(int p[]);
void read(int p[]);
void print(int p[]);
void add(int p1[],int p2[],int p3[]);
void multiply(int p1[],int p2[],int p3[]);
/*Polynomial is stored in an array, p[i] gives coefficient of x^i .
a polynomial 3x^2 + 12x^4 will be represented as (0,0,3,0,12,0,0,....)
*/
void main()
{
int p1[MAX],p2[MAX],p3[MAX];
int option;
do
{
printf("
1 : create 1st polynomial");
printf("
2 : create 2nd polynomial");
printf("
3 : Add polynomials");
printf("
4 : Multiply polynomials");
printf("
5 : Quit");
printf("
Enter your choice :");
scanf("%d",&option);
switch(option)
{
case 1:read(p1);break;
case 2:read(p2);break;
case 3:add(p1,p2,p3);
printf("
1st polynomial -> ");
print(p1);
printf("
2nd polynomial -> ");
print(p2);
printf("
Sum = ");
print(p3);
break;
case 4:multiply(p1,p2,p3);
printf("
1st polynomial -> ");
print(p1);
printf("
2nd polynomial -> ");
print(p2);
printf("
Product = ");
print(p3);
break;
}
}while(option!=5);
}
void read(int p[])
{
int n, i, power,coeff;
init(p);
printf("
Enter number of terms :");
scanf("%d",&n);
/* read n terms */
for (i=0;i<n;i++)
{ printf("
enter a term(power coeff.)");
scanf("%d%d",&power,&coeff);
p[power]=coeff;
}
}
void print(int p[])
{
int i;
for(i=0;i<MAX;i++)
if(p[i]!=0)
printf("%dX^%d ",p[i],i);
}
void add(int p1[], int p2[], int p3[])
{
int i;
for(i=0;i<MAX;i++)
p3[i]=p1[i]+p2[i];
}
void multiply(int p1[], int p2[], int p3[])
{
int i,j;
init(p3);
for(i=0;i<MAX;i++)
for(j=0;j<MAX;j++)
p3[i+j]=p3[i+j]+p1[i]*p2[j];
}
void init(int p[])
{
int i;
for(i=0;i<MAX;i++)
p[i]=0;
}
Also Read: C Program for Addition of two Sparse Matrices
Also Read: C Program for Array Representation of Stack [Push, Pop and Display]
Polynomial addition, multiplication (8th degree polynomials) using linked list
#include<math.h>
#include<stdio.h>
#include<conio.h>
#define MAX 17
typedef struct node
{
int coeff;
struct node *next;
}node;
node * init();
void read(node *h1);
void print(node *h1);
node * add(node *h1,node *h2);
node * multiply(node *h1,node *h2);
/*Polynomial is stored in a linked list, ith node gives coefficient of x^i .
a polynomial 3x^2 + 12x^4 will be represented as (0,0,3,0,12,0,0,....)
*/
void main()
{
node *h1=NULL,*h2=NULL,*h3=NULL;
int option;
do
{
printf("
1 : create 1st polynomial");
1 : create 1st polynomial");
printf("
2 : create 2nd polynomial");
2 : create 2nd polynomial");
printf("
3 : Add polynomials");
3 : Add polynomials");
printf("
4 : Multiply polynomials");
4 : Multiply polynomials");
printf("
5 : Quit");
5 : Quit");
printf("
Enter your choice :");
Enter your choice :");
scanf("%d",&option);
switch(option)
{
case 1:h1=init();read(h1);break;
case 2:h2=init();read(h2);break;
case 3:h3=add(h1,h2);
printf("
1st polynomial -> ");
1st polynomial -> ");
print(h1);
printf("
2nd polynomial -> ");
2nd polynomial -> ");
print(h2);
printf("
Sum = ");
Sum = ");
print(h3);
break;
case 4:h3=multiply(h1,h2);
printf("
1st polynomial -> ");
1st polynomial -> ");
print(h1);
printf("
2nd polynomial -> ");
2nd polynomial -> ");
print(h2);
printf("
Product = ");
Product = ");
print(h3);
break;
}
}while(option!=5);
}
void read(node *h)
{
int n,i,j,power,coeff;
node *p;
p=init();
printf("
Enter number of terms :");
Enter number of terms :");
scanf("%d",&n);
/* read n terms */
for (i=0;i<n;i++)
{ printf("
enter a term(power coeff.)");
enter a term(power coeff.)");
scanf("%d%d",&power,&coeff);
for(p=h,j=0;j<power;j++)
p=p->next;
p->coeff=coeff;
}
}
void print(node *p)
{
int i;
for(i=0;p!=NULL;i++,p=p->next)
if(p->coeff!=0)
printf("%dX^%d ",p->coeff,i);
}
node * add(node *h1, node *h2)
{
node *h3,*p;
h3=init();
p=h3;
while(h1!=NULL)
{
h3->coeff=h1->coeff+h2->coeff;
h1=h1->next;
h2=h2->next;
h3=h3->next;
}
return(p);
}
node * multiply(node *h1, node *h2)
{
node *h3,*p,*q,*r;
int i,j,k,coeff,power;
h3=init();
for(p=h1,i=0;p!=NULL;p=p->next,i++)
for(q=h2,j=0;q!=NULL;q=q->next,j++)
{
coeff=p->coeff * q->coeff;
power=i+j;
for(r=h3,k=0;k<power;k++)
r=r->next;
r->coeff=r->coeff+coeff;
}
return(h3);
}
node * init()
{
int i;
node *h=NULL,*p;
for(i=0;i<MAX;i++)
{
p=(node*)malloc(sizeof(node));
p->next=h;
p->coeff=0;
h=p;
}
return(h);
}

Tuesday, February 10, 2015
Arrays in C 2D Array Part 5
Till now I told you about the 1D arrays but today I will discuss about the 2D arrays or two dimensional arrays in c. Array is a collection of elements with similar data type. We can make arrays with any dimension. However programmers rarely go beyond 3D arrays. I will also give you an overview on 3D arrays in the subsequent tutorials. But today lets discuss the 2D arrays briefly.
int a[3][3];
This is a 2D array with 3 rows and 3 columns. The total elements in the array are 9 (3x3).
int num[3][2] = {
{43,56},
{56,54},
{65,98}
};
This is one of the simplest way of initializing a 2D array.
int num[3][2] = {43, 56, 56, 54, 65, 98};
This method will also work but it will decrease the readability of the array too.
int arr[ ][3] = { 12, 34, 23, 45, 56, 45 } ;
int num[][2] = {
{43,56},
{56,54},
{65,98}
{87,86}
};
It is optional to provide the row dimension of a 2D array if we initialize it. Remember giving column dimension is always compulsory.
Lets take one simple program to understand 2D array in C.
Output
%2B-%2BPart%2B5%2BOutptut.jpg)
Explanation
1. In the first statement I have declared the 2D array with name student. Remember 2D array also stores elements with index 00. Elements will be stored in this way.
00 01
10 11
20 21
And so on.
2. Now by using two for loops I have stored the values inside 2D array and display it on the screen.
3. Consider carefully the printf() and scanf() function with arguments “&stud[x][0], &student[x][1]”. We are storing and accessing values inside 2D array by row wise. We can also store values and access them column wise too.
Elements of first row will be stored first, after that elements of second row will be stored. This procedure will continue until the array elements ends. Given below is the memory allocation of a 2D integer array s[4][2].
%2B-%2BPart%2B5.jpg)
2D Arrays in C
As its name suggests, 2D arrays are the arrays having 2 dimensions. Those two dimensions are generally called rows and columns. 2D arrays are also called matrix.Declaration of 2D Array
A two dimensional array can be declared in following way.int a[3][3];
This is a 2D array with 3 rows and 3 columns. The total elements in the array are 9 (3x3).
Initialization of 2D Array
Similar to 1D arrays, a 2D array can also be initialize at the time of its declaration. Given below are some of the initialization methods that are used very frequently.int num[3][2] = {
{43,56},
{56,54},
{65,98}
};
This is one of the simplest way of initializing a 2D array.
int num[3][2] = {43, 56, 56, 54, 65, 98};
This method will also work but it will decrease the readability of the array too.
int arr[ ][3] = { 12, 34, 23, 45, 56, 45 } ;
int num[][2] = {
{43,56},
{56,54},
{65,98}
{87,86}
};
It is optional to provide the row dimension of a 2D array if we initialize it. Remember giving column dimension is always compulsory.
Lets take one simple program to understand 2D array in C.
#include<stdio.h>
int main()
{
int student[6][2];
int x;
for(x=0;x<6;x++)
{
printf(" Enter roll no. and marks
");
scanf("%d %d",&student[x][0],&student[x][1]);
}
for(x=0;x<6;x++)
printf("
%d %d",student[x][0],student[x][1]);
return 0;
}
Output
%2B-%2BPart%2B5%2BOutptut.jpg)
1. In the first statement I have declared the 2D array with name student. Remember 2D array also stores elements with index 00. Elements will be stored in this way.
00 01
10 11
20 21
And so on.
2. Now by using two for loops I have stored the values inside 2D array and display it on the screen.
3. Consider carefully the printf() and scanf() function with arguments “&stud[x][0], &student[x][1]”. We are storing and accessing values inside 2D array by row wise. We can also store values and access them column wise too.
Memory Allocation of 2D Array
2D arrays also stores its values similar to 1D arrays. They also store elements in contiguous memory locations.Elements of first row will be stored first, after that elements of second row will be stored. This procedure will continue until the array elements ends. Given below is the memory allocation of a 2D integer array s[4][2].
%2B-%2BPart%2B5.jpg)
Subscribe to:
Posts (Atom)