DSpractical 1 2
DSpractical 1 2
struct poly
{
int degree;
int coeff;
};/*End of structure definition*/
void main()
{
struct poly poly1[10],poly2[10],product[];
int noOfTerms1,noOfTerms2,count=-1;
int i,j;
clrscr();
printf("\nEnter Number Of Terms Of 1st Polynomial: ");
scanf("%d",&noOfTerms1);
for(i=0;i< noOfTerms1;i++)
{
printf("\nEnter Degree: ");
scanf("%d",&poly1[i].degree);
printf("\nEnter Coefficient: ");
scanf("%d",&poly1[i].coeff);
}/*End of i for loop*/
printf("\nEnter Number Of Terms Of 2nd Polynomial: ");
scanf("%d",&noOfTerms2);
for(i=0;i< noOfTerms2;i++)
{
printf("\nEnter Degree: ");
scanf("%d",&poly2[i].degree);
printf("\nEnter Coefficient: ");
scanf("%d",&poly2[i].coeff);
}/*End of i for loop*/
for(i=0;i< noOfTerms1;i++)
{
for (j=0;j< noOfTerms2;j++)
{
product[++count].degree=poly1[i].degree+poly2[j].degree;
product[count].coeff=poly1[i].coeff*poly2[j].coeff;
}/*End of j for loop*/
}/*End of i for loop*/
printf("\nThe Product Of Two Polynomials Is: \n");
for(i=0;i<=count;i++)
{
if(product[i].degree==0)
printf("%d ",product[i].coeff);
else if(product[i].degree==1)
printf("%dx ",product[i].coeff);
else
{
printf("%dx^%d ",product[i].coeff,product[i].degree);
}
if(i!=count)
printf("+ ");
}/*End of i for loop*/
getch();
}
ASSIGNMENT 2
SETA
1) Write a C program to accept and sort n elements in ascending order by using
bubble sort.
Solution:
#include <stdio.h>
#define MAXSIZE 10
void main()
{
int array[MAXSIZE];
int i, j, num, temp;
insertionSort(daysOfWeek, n);
printf("\nSorted order of days:\n");
for (int i = 0; i < n; i++) {
printf("%s\n", daysOfWeek[i]);
}
return 0;
}
2) Write a ‘C’ program to accept names from the user and sort in alphabetical order using bubble sort
Solution:
#include<stdio.h>
#include<string.h>
main()
{
int i,j,n;
char str[][],s[];
printf("Enter number of names \n");
scanf("%d",&n);
printf("Enter names in any order\n");
for(i=0;i<n;i++)
{
scanf("%s",str[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{ if(strcmp(str[i],str[j])>0)
{
strcpy(s,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],s);
}
}
printf("\nThe sorted order of alphabets are:\n");
for(i=0;i<n;i++)
{
printf("%s\n",str[i]);
}
}
SET C
2) Write a C program to read the data from the file “person.txt” which contains personno and
personage and sort the data on age in ascending order using insertion Sort / Selection Sort.
ANS:
include<stdio.h>
fp=fopen("person.txt","r");
if(fp==NULL)
printf("File Not Exist");
else
{
while(!feof(fp))
{
fscanf(fp,"%s%d", a[i].name, &a[i].age);
i++;
}
fclose(fp);
}
return i-1;
}
//Main
int main()
{
int i, n;
char key[20];
record a[20];
n = fileread(a);
insertion_sort(n);
}
void insertion_sort(int n)
{
record a[20],b[30];
n=fileread(a);
int temp,i,j,k;
for (i=0 ;i<n ;i++)
{
temp= a[i].age;
for(k=0;k<='\0';k++)
b[k]=a[i];
j = i - 1;
while (temp<a[j].age && j>=0)
{
}
printf("Sorted list:\n");
for(int i=0; i<n; i++)
printf("%s %d\n", a[i].name, a[i].age);
}