0% found this document useful (0 votes)
7 views16 pages

DSpractical 1 2

The document contains several C programming tasks and their solutions, including counting occurrences in a sorted array, copying arrays, finding and replacing elements, adding and multiplying polynomials, and sorting arrays using bubble and insertion sort. It also includes a task to read data from a file and sort it by age. Each task is presented with a code snippet demonstrating the solution.

Uploaded by

shraddhavinod1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views16 pages

DSpractical 1 2

The document contains several C programming tasks and their solutions, including counting occurrences in a sorted array, copying arrays, finding and replacing elements, adding and multiplying polynomials, and sorting arrays using bubble and insertion sort. It also includes a task to read data from a file and sort it by age. Each task is presented with a code snippet demonstrating the solution.

Uploaded by

shraddhavinod1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

SET A

1) Write a C Program to Count number of occurrences (or


frequency) in a given sorted array Input: arr[] = {1, 1, 2, 2, 2, 2,
3,}, x = 2
SOLUTION:
#include <stdio.h>
int main()
{
int Size, i, num, occr = 0;
printf("Please Enter the Array size = ");
scanf("%d", &Size);
int arr[Size];
printf("Enter the Array %d elements : ", Size);
for (i = 0; i < Size; i++)
{
scanf("%d", &arr[i]);
}
printf("Please Enter the Array Item to Know = ");
scanf("%d", &num);
for (i = 0; i < Size; i++)
{
if (arr[i] == num)
{
occr++;
}
}
printf("%d Occurred %d Times.\n", num, occr);
}
2) Write a C program to Copy one array into another array.
Solution:
#include <stdio.h>
void main()
{
int arr1[100], arr2[100];
int i, n;
printf("\n\nCopy the elements one array into another
array :\n");
printf("----------------------------------------------------\n");
printf("Input the number of elements to be stored in the
array :");
scanf("%d",&n);
printf("Input %d elements in the array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
for(i=0; i<n; i++)
{
arr2[i] = arr1[i];
}
printf("\nThe elements stored in the first array are :\n");
for(i=0; i<n; i++)
{
printf("% 5d", arr1[i]);
}
/* Prints the elements copied into the second array. */
printf("\n\nThe elements copied into the second array are :\n");
for(i=0; i<n; i++)
{
printf("% 5d", arr2[i]);
}
printf("\n\n");
}
Set B
1)Write a ‘C’ program to accept n elements store those elements in
array and find and replace a given number.
Solution:
#include<stdio.h>
void main()
{
//Declare array of size
int a[];
int i,num,key,pos;
printf("Enter how many elements you want to insert:\n");
scanf("%d",&key);
printf("Enter the elements: \n");
for(i=0;i<key;i++)
scanf("%d",&a[i]);
printf("Elements in array are:\n");
for(i=0;i<key;i++)
printf("%d ",a[i]);
printf("\n\nEnter which new element you want to insert: \n");
scanf("%d",&num);
//Accept position to insert new element
printf("\nEnter the position to insert new element: \n");
scanf("%d",&pos);
for(i=0;i<key;i++)
{
if(i+1==pos)
{
a[pos-1]=num;
}
}
//Print Output
printf("Final Output: \n");
for(i=0;i<key;i++)
printf("%d ",a[i]);
}

2)Write a ‘C’ program to accept two polynomials and find the


addition of accepted
polynomials.
Solution:
#include<stdio.h>
#include<conio.h>
main()
{
int a[10], b[10], c[10],m,n,k,k1,i,j,x;
clrscr();
printf("\n\tPolynomial Addition\n");
printf("\t===================\n");
printf("\n\tEnter the no. of terms of the polynomial:");
scanf("%d", &m);
printf("\n\tEnter the degrees and coefficients:");
for (i=0;i<2*m;i++)
scanf("%d", &a[i]);
printf("\n\tFirst polynomial is:");
k1=0;
if(a[k1+1]==1)
printf("x^%d", a[k1]);
else
printf("%dx^%d", a[k1+1],a[k1]);
k1+=2;
while (k1<i)
{
printf("+%dx^%d", a[k1+1],a[k1]);
k1+=2;
}
printf("\n\n\n\tEnter the no. of terms of 2nd polynomial:");
scanf("%d", &n);
printf("\n\tEnter the degrees and co-efficients:");
for(j=0;j<2*n;j++)
scanf("%d", &b[j]);
printf("\n\tSecond polynomial is:");
k1=0;
if(b[k1+1]==1)
printf("x^%d", b[k1]);
else
printf("%dx^%d",b[k1+1],b[k1]);
k1+=2;
while (k1<2*n)
{
printf("+%dx^%d", b[k1+1],b[k1]);
k1+=2;
}
i=0;
j=0;
k=0;
while (m>0 && n>0)
{
if (a[i]==b[j])
{
c[k+1]=a[i+1]+b[j+1];
c[k]=a[i];
m--;
n--;
i+=2;
j+=2;
}
else if (a[i]>b[j])
{
c[k+1]=a[i+1];
c[k]=a[i];
m--;
i+=2;
}
else
{
c[k+1]=b[j+1];
c[k]=b[j];
n--;
j+=2;
}
k+=2;
}
while (m>0)
{
c[k+1]=a[i+1];
c[k]=a[i];
k+=2;
i+=2;
m--;
}
while (n>0)
{
c[k+1]=b[j+1];
c[k]=b[j];
k+=2;
j+=2;
n--;
}
printf("\n\n\n\n\tSum of the two polynomials is:");
k1=0;
if (c[k1+1]==1)
printf("x^%d", c[k1]);
else
printf("%dx^%d", c[k1+1],c[k1]);
k1+=2;
while (k1<k)
{
if (c[k1+1]==1)
printf("+x^%d", c[k1]);
else
printf("+%dx^%d", c[k1+1], c[k1]);
k1+=2;
}
getch();
return 0;
}
SET C
1)Write a ‘C’ program to accept two polynomials and find the Multiplication of
accepted polynomials.
Solution:
#include< stdio.h>
#include< conio.h>

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;

printf("Enter the value of num \n");


scanf("%d", &num);
printf("Enter the elements one by one \n");
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Input array is \n");
for (i = 0; i < num; i++)
{
printf("%d\n", array[i]);
}
/* Bubble sorting begins */
for (i = 0; i < num; i++)
{
for (j = 0; j < (num - i - 1); j++)
{
if (array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
printf("Sorted array is...\n");
for (i = 0; i < num; i++)
{
printf("%d\n", array[i]);
}
}
2) Write a C program to accept and sort n elements in ascending order by using insertion sort.
Solution:#include<conio.h>
#include<stdio.h>
#define size 6
void main()
{
int arr[size],i,j,k,key,temp;
printf("Please enter elements: \n ");
for(i=0;i<=size-1;i++)
{
scanf("%d",&arr[i]);
}
for(j=1;j<=size-1;j++)
{
key=arr[j];
k=j-1;
while (k>=0 && arr[k]>key)
{
arr[k+1] = arr[k];
arr[k]=key;
k=k-1;
}}
printf(" \n Entered Elements :");
for(i=0;i<=size-1;i++)
printf("\n %d ",arr[i]);
}
SET B
1) Write a C program to create a string array with day of week and sort them using Insertion
sort.
ANS:-
#include <stdio.h>
#include <string.h>

void insertionSort(char arr[][20], int n) {


for (int i = 1; i < n; i++) {
char key[20];
strcpy(key, arr[i]);
int j = i - 1;

while (j >= 0 && strcmp(arr[j], key) > 0) {


strcpy(arr[j + 1], arr[j]);
j--;
}
strcpy(arr[j + 1], key);
}
}
int main() {
char daysOfWeek[][20] = {
"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"
};
int n = sizeof(daysOfWeek) / sizeof(daysOfWeek[0]);

printf("Original order of days:\n");


for (int i = 0; i < n; i++) {
printf("%s\n", daysOfWeek[i]);
}

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>

typedef struct record


{
char name[30];
int age;
}record;

int fileread(record a[20])


{
FILE *fp;
int i=0;

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);

for(int i=0; i<n; i++)


printf("%s %d\n", a[i].name, a[i].age);
/* displaying records*/

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)
{

a[j + 1].age = a[j].age;


for(k=0;k<='\0';k++)
a[j+1]=a[j];
j = j - 1;
}
for(k=0;k<='\0';k++)
a[j+1]=b[k];
a[j + 1].age = temp;

}
printf("Sorted list:\n");
for(int i=0; i<n; i++)
printf("%s %d\n", a[i].name, a[i].age);
}

You might also like