0% found this document useful (0 votes)
13 views14 pages

23BBS0006 VL2023240506375 Ast02

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)
13 views14 pages

23BBS0006 VL2023240506375 Ast02

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/ 14

Data Structures &Algorithms

Digital Assesment 2
Name: Mohammed Sami
Registration Number : 23BBS0006 Date: 08/03/24

1.

Algorithm:
Copy of the file:

Source code:

#include <stdio.h>
#include <stdlib.h>

struct Node {
int coefficient;
int exponent;
struct Node* next;
};

typedef struct Node Node;

Node* createNode(int coeff, int exp) {


Node* newNode = (Node*)malloc(sizeof(Node));
newNode->coefficient = coeff;
newNode->exponent = exp;
newNode->next = NULL;
return newNode;
}

void insert(Node** poly, int coeff, int exp) {


Node* newNode = createNode(coeff, exp);
if (*poly == NULL) {
*poly = newNode;
} else {
Node* prev = NULL;
Node* current = *poly;
while (current != NULL && current->exponent > exp) {
prev = current;
current = current->next;
}
if (current != NULL && current->exponent == exp) {
current->coefficient += coeff;
} else {
if (prev == NULL) {
newNode->next = *poly;
*poly = newNode;
} else {
newNode->next = current;
prev->next = newNode;
}
}
}
}
void display(Node* poly) {
Node* current = poly;
while (current != NULL) {
printf("%dx^%d ", current->coefficient, current->exponent);
if (current->next != NULL) {
printf("+ ");
}
current = current->next;
}
printf("\n");
}

Node* mult(Node* poly1, Node* poly2) {


Node* result = NULL;
Node* current1 = poly1;
while (current1 != NULL) {
Node* current2 = poly2;
while (current2 != NULL) {
int coeff = current1->coefficient * current2->coefficient;
int exp = current1->exponent + current2->exponent;
insert(&result, coeff, exp);
current2 = current2->next;
}
current1 = current1->next;
}
return result;
}

int main() {
Node* poly1 = NULL;
insert(&poly1, 3, 2);
insert(&poly1, 2, 1);
insert(&poly1, 5, 0);

Node* poly2 = NULL;


insert(&poly2, 4, 3);
insert(&poly2, 1, 0);

printf("Polynomial 1: ");
display(poly1);
printf("Polynomial 2: ");
display(poly2);

Node* result = mult(poly1, poly2);


printf("Product of the polynomials: ");
display(result);
return 0;
}
Test Cases:

1.

Input:

Output:

2.

Input:

Output:
3.Extreme input case:

Output:
2.:

Algorithm:
Source code:

#include<stdio.h>
#include<stdlib.h>

int main()
{
do{
int n;
printf("\n *****");
printf("\n\nEnter the number of inputs(enter 0 to exit): ");
scanf("%d",&n);
if(n==0){
break;
}
if(n<0){
printf("\nInvalid ! Enter positive number of inputs!");
}
else{
int input[n];

//Taking inputs
printf("\nEnter the inputs: ");
for(int i=0;i<n;i++){
scanf("%d",&input[i]);
}

int search;
printf("Enter the element to search :");
scanf("%d",&search);
int count=0;

//Linear Search, counter increments if element is present


for(int i=0;i<n;i++){
if(input[i]==search){
printf("\nElement is present.\nLocation:%d",i+1);
count++;
}
}
if(count==0){
printf("\nElement is not present in the list.");
}
}
}while(1);
return 0;
}
Test Cases 1,2,and 3:
3.Sorting algorithms

Bubble sort and Selection sort:


Insertion sort:

Source Code:

#include<stdio.h>
#include<stdlib.h>

#define max_size 5

void bubble_sort(int arr[max_size]){


for(int i=0;i<max_size;i++){
for(int j=0;j<max_size-1;j++){
if(arr[j]>arr[j+1]){
int temp=arr[j+1];
arr[j+1]=arr[j];
arr[j]=temp;
}
}
}
printf("\nSorted using bubble sort\n");
for(int i=0;i<max_size;i++){
printf("%d ",arr[i]);
}
}

void selection_sort(int arr[max_size]) {


int min, min_index;

for (int i = 0; i < max_size - 1; i++) {


min_index = i;
for (int j = i + 1; j < max_size; j++) {
if (arr[j] < arr[min_index]) {
min_index = j;
}
}

if (min_index != i) {
int temp = arr[i];
arr[i] = arr[min_index];
arr[min_index] = temp;
}
}

printf("\nSorted using selection sort\n");


for (int i = 0; i < max_size; i++) {
printf("%d ", arr[i]);
}
}

void insertion_sort(int arr[max_size]) {


for (int i = 1; i < max_size; i++) {
int key = arr[i];
int j = i - 1;

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


arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}

printf("\nSorted using insertion sort\n");


for (int i = 0; i < max_size; i++) {
printf("%d ", arr[i]);
}
}

int main()
{
int arr[max_size]={34,65,34,23,78};
int choice;
do{
printf("\n\nEnter your choice for the sorting algorithm:\n0.Exit\n1.Bubble
sort\n2.Insertion sort\n3.Selection sort\n");
scanf("%d",&choice);
if (choice==0){
break;
}
else
{switch(choice)
{
case 1:
bubble_sort(arr);
break;

case 2:
insertion_sort(arr);
break;

case 3:
selection_sort(arr);
break;

default:
printf("Invalid input!");
break;
}
}}while (1);

return 0;
}
Test Cases 1,2, and 3:

Test case 4:

Extreme case : (invalid input)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy