You are on page 1of 18

// week 1:lab :1

Question No: 1
reportIcon
Single File Programming Question

Problem Statement

Keerthi is a tech enthusiast and is fascinated by polynomial expressions. She loves to perform
various operations on polynomials.

Today, she is working on a program to multiply two polynomials and delete a speci c term from
the result.

Keerthi needs your help to implement this program. She wants to take the coe cients and
exponents of the terms of the two polynomials as input, perform the multiplication, and then allow
the user to specify an exponent for deletion from the resulting polynomial, and display the result.
Input format :

The rst line of input consists of an integer n, representing the number of terms in the rst
polynomial.

The following n lines of input consist of two integers each: the coe cient and the exponent of the
term in the rst polynomial.

The next line of input consists of an integer m, representing the number of terms in the second
polynomial.

The following m lines of input consist of two integers each: the coe cient and the exponent of the
term in the second polynomial.

The last line of input consists of an integer, representing the exponent of the term that Keerthi
wants to delete from the multiplied polynomial.
Output format :

The rst line of output displays the resulting polynomial after multiplication.

The second line of output displays the resulting polynomial after deleting the speci ed term.

Refer to the sample output for formatting speci cations.


Code constraints :

1 <= n, m <= 100


Sample test cases :
Input 1 :

3
22
31
40
2
12
21
2

Output 1 :
fi
fi
fi
fi
ffi
ffi
ffi
fi
fi
fi
Result of the multiplication: 2x^4 + 7x^3 + 10x^2 + 8x
Result after deleting the term: 2x^4 + 7x^3 + 8x

Input 2 :

2
-1 4
41
2
24
-3 2
8

Output 2 :

Result of the multiplication: -2x^8 + 3x^6 + 8x^5-12x^3


Result after deleting the term: 3x^6 + 8x^5-12x^3

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

struct Term {
int coe cient;
int exponent;
};

void multiply_polynomials(const struct Term* poly1, int n1, const struct Term* poly2, int n2, struct
Term** result, int* size) {
for (int i = 0; i < n1; ++i) {
for (int j = 0; j < n2; ++j) {
int new_coe = poly1[i].coe cient * poly2[j].coe cient;
int new_exp = poly1[i].exponent + poly2[j].exponent;
int found = 0;

for (int k = 0; k < *size; ++k) {


if ((*result)[k].exponent == new_exp) {
(*result)[k].coe cient += new_coe ;
found = 1;
break;
}
}

if (!found) {
(*size)++;
*result = realloc(*result, (*size) * sizeof(struct Term));
(*result)[*size - 1].coe cient = new_coe ;
(*result)[*size - 1].exponent = new_exp;
}
}
}

for (int i = 0; i < *size; ++i) {


for (int j = i + 1; j < *size; ++j) {
if ((*result)[i].exponent < (*result)[j].exponent) {
struct Term temp = (*result)[i];
(*result)[i] = (*result)[j];
(*result)[j] = temp;
}
}
ffi
ff
ffi
ffi
ffi
ff
ff
ffi
}
}

void delete_term(struct Term* poly, int* size, int term_to_delete) {


for (int i = 0; i < *size; ++i) {
if (poly[i].exponent == term_to_delete) {
for (int j = i; j < *size - 1; ++j) {
poly[j] = poly[j + 1];
}
(*size)--;
poly = realloc(poly, (*size) * sizeof(struct Term));
break;
}
}
}

void display_polynomial(const struct Term* poly, int size) {


for (int i = 0; i < size; ++i) {
if (poly[i].coe cient > 0) {
printf(" + %dx^%d", poly[i].coe cient, poly[i].exponent);
} else if (poly[i].coe cient < 0) {
printf(" - %dx^%d", -poly[i].coe cient, poly[i].exponent);
}
}
printf("\n");
}

int main() {
int n, m, term_to_delete;

scanf("%d", &n);
struct Term* poly1 = malloc(n * sizeof(struct Term));
for (int i = 0; i < n; ++i) {
scanf("%d %d", &poly1[i].coe cient, &poly1[i].exponent);
}

scanf("%d", &m);
struct Term* poly2 = malloc(m * sizeof(struct Term));
for (int i = 0; i < m; ++i) {
scanf("%d %d", &poly2[i].coe cient, &poly2[i].exponent);
}

scanf("%d", &term_to_delete);

struct Term* result_poly = malloc(0);


int size = 0;
multiply_polynomials(poly1, n, poly2, m, &result_poly, &size);

printf("Result of the multiplication:");


display_polynomial(result_poly, size);

delete_term(result_poly, &size, term_to_delete);

printf("Result after deleting the term:");


display_polynomial(result_poly, size);

free(poly1);
free(poly2);
free(result_poly);
ffi
ffi
ffi
ffi
ffi
ffi
return 0;
}

Question No: 2
reportIcon
Single File Programming Question

Problem Statement

Akila is a tech enthusiast and wants to write a program to add two polynomials. Each polynomial
is represented as a linked list, where each node in the list represents a term in the polynomial.

A term in the polynomial is represented in the format ax^b, where a is the coe cient and b is the
exponent.

Akila needs your help to implement a program that takes two polynomials as input, adds them,
and stores the result in ascending order in a new polynomial-linked list. Write a program to help
her.
Input format :

The input consists of the coe cient and exponent of each term of two polynomials on separate
lines.

The polynomials are terminated by entering 0 0 as the coe cient and exponent.
Output format :

The rst line of output prints the rst polynomial.

The second line of output prints the second polynomial.

The terms in the polynomial are displayed in ascending order of exponents.

The third line of output prints the resultant polynomial after adding the given polynomials.

Each term in the polynomials should be displayed in the format ax^b, where a is the coe cient
and b is the exponent.

If the resultant polynomial has zero terms (i.e., the sum of the two polynomials is zero), the output
will be "0".

Refer to the sample output for the exact format.


Code constraints :

The coe cients and exponents in the polynomials are integers.

The exponent values are non-negative integers.

The coe cient values can be positive, negative, or zero.

The absolute value of the coe cients will not exceed 1000.
Sample test cases :
Input 1 :
fi
ffi
ffi
ffi
ffi
fi
ffi
ffi
ffi
34
23
12
00
12
23
34
00

Output 1 :

1x^2 + 2x^3 + 3x^4


1x^2 + 2x^3 + 3x^4
2x^2 + 4x^3 + 6x^4

Input 2 :

13
22
00
-1 3
-2 2
00

Output 2 :

2x^2 + 1x^3
-2x^2 + -1x^3
0

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

struct Term {
int coe cient;
int exponent;
struct Term* next;
};

void insert_term(struct Term** poly, int coe , int exp) {


struct Term* new_term = (struct Term*)malloc(sizeof(struct Term));
new_term->coe cient = coe ;
new_term->exponent = exp;
new_term->next = NULL;

if (*poly == NULL) {
*poly = new_term;
} else {
struct Term* temp = *poly;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = new_term;
}
}

struct Term* add_polynomials(struct Term* poly1, struct Term* poly2) {


ffi
ffi
ff
ff
struct Term* result = NULL;

while (poly1 != NULL || poly2 != NULL) {


int coe 1 = (poly1 != NULL) ? poly1->coe cient : 0;
int exp1 = (poly1 != NULL) ? poly1->exponent : -1;

int coe 2 = (poly2 != NULL) ? poly2->coe cient : 0;


int exp2 = (poly2 != NULL) ? poly2->exponent : -1;

if (exp1 > exp2) {


insert_term(&result, coe 1, exp1);
poly1 = poly1->next;
} else if (exp1 < exp2) {
insert_term(&result, coe 2, exp2);
poly2 = poly2->next;
} else {
int sum_coe = coe 1 + coe 2;
if (sum_coe != 0) {
insert_term(&result, sum_coe , exp1);
}
poly1 = poly1->next;
poly2 = poly2->next;
}
}

return result;
}

void display_polynomial(struct Term* poly) {


while (poly != NULL) {
printf("%dx^%d", poly->coe cient, poly->exponent);
poly = poly->next;
if (poly != NULL) {
printf(" + ");
}
}
printf("\n");
}

void free_polynomial(struct Term* poly) {


while (poly != NULL) {
struct Term* temp = poly;
poly = poly->next;
free(temp);
}
}

int main() {
struct Term* poly1 = NULL;
struct Term* poly2 = NULL;

int coe , exp;


while (1) {
scanf("%d %d", &coe , &exp);
if (coe == 0 && exp == 0) {
break;
}
insert_term(&poly1, coe , exp);
}
ff
ff
ff
ff
ff
ff
ff
ff
ff
ff
ff
ffi
ff
ff
ffi
ffi
while (1) {
scanf("%d %d", &coe , &exp);
if (coe == 0 && exp == 0) {
break;
}
insert_term(&poly2, coe , exp);
}

display_polynomial(poly1);
display_polynomial(poly2);

struct Term* result_poly = add_polynomials(poly1, poly2);


display_polynomial(result_poly);

free_polynomial(poly1);
free_polynomial(poly2);
free_polynomial(result_poly);

return 0;
}

Question No: 3
reportIcon
Single File Programming Question

Problem Statement

Udhaya loves studying polynomials, and she wants to write a program to compare two
polynomials represented as linked lists and display whether they are equal or not.

The polynomials are expressed as a series of terms, where each term consists of a coe cient and
an exponent. The program should read the polynomials from the user, compare them, and then
display whether they are equal or not.
Input format :

The rst line of input consists of an integer n, representing the number of terms in the rst
polynomial.

The following n lines of input consist of two integers each: the coe cient and the exponent of the
term in the rst polynomial.

The next line of input consists of an integer m, representing the number of terms in the second
polynomial.

The following m lines of input consist of two integers each: the coe cient and the exponent of the
term in the second polynomial.
Output format :

The rst line of output prints the rst polynomial.

The second line of output prints the second polynomial.

The polynomials should be displayed in the format ax^b, where a is the coe cient and b is the
exponent.

If the two polynomials are equal, the third line of output should print "Polynomials are Equal."

If the two polynomials are not equal, print "Polynomials are Not Equal."
fi
fi
ff
fi
ff
ff
fi
ffi
ffi
ffi
fi
ffi
Refer to the sample output for the formatting speci cations.
Code constraints :

The number of terms in each polynomial will be non-negative integers.

The coe cients of the terms will be integers (positive or negative).


Sample test cases :
Input 1 :

2
12
21
2
12
21

Output 1 :

Polynomial 1: (1x^2) + (2x^1)


Polynomial 2: (1x^2) + (2x^1)
Polynomials are Equal.

Input 2 :

3
12
21
30
3
12
21
40

Output 2 :

Polynomial 1: (1x^2) + (2x^1) + (3x^0)


Polynomial 2: (1x^2) + (2x^1) + (4x^0)
Polynomials are Not Equal.

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

struct Term {
int coe cient;
int exponent;
struct Term* next;
};

void insert_term(struct Term** poly, int coe , int exp) {


struct Term* new_term = (struct Term*)malloc(sizeof(struct Term));
new_term->coe cient = coe ;
new_term->exponent = exp;
new_term->next = NULL;

if (*poly == NULL) {
*poly = new_term;
ffi
ffi
ffi
ff
ff
fi
} else {
struct Term* temp = *poly;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = new_term;
}
}

void display_polynomial(struct Term* poly) {


while (poly != NULL) {
printf("(%dx^%d)", poly->coe cient, poly->exponent);
poly = poly->next;
if (poly != NULL) {
printf(" + ");
}
}
printf("\n");
}

int are_polynomials_equal(struct Term* poly1, struct Term* poly2) {


while (poly1 != NULL && poly2 != NULL) {
if (poly1->coe cient != poly2->coe cient || poly1->exponent != poly2->exponent) {
return 0;
}
poly1 = poly1->next;
poly2 = poly2->next;
}

return poly1 == NULL && poly2 == NULL;


}

int main() {
struct Term* poly1 = NULL;
struct Term* poly2 = NULL;

int n, m;
scanf("%d", &n);

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


int coe , exp;
scanf("%d %d", &coe , &exp);
insert_term(&poly1, coe , exp);
}

scanf("%d", &m);

for (int i = 0; i < m; ++i) {


int coe , exp;
scanf("%d %d", &coe , &exp);
insert_term(&poly2, coe , exp);
}

printf("Polynomial 1: ");
display_polynomial(poly1);

printf("Polynomial 2: ");
display_polynomial(poly2);

if (are_polynomials_equal(poly1, poly2)) {
ff
ff
ffi
ff
ff
ff
ff
ffi
ffi
printf("Polynomials are Equal.\n");
} else {
printf("Polynomials are Not Equal.\n");
}

// Free allocated memory


while (poly1 != NULL) {
struct Term* temp = poly1;
poly1 = poly1->next;
free(temp);
}

while (poly2 != NULL) {


struct Term* temp = poly2;
poly2 = poly2->next;
free(temp);
}

return 0;
}

// lab:2

Question No: 1
reportIcon
Single File Programming Question

Problem Statement

Usha is a Math enthusiast and has a deep interest in scienti c calculations. She decides to create
a scienti c calculator application that allows users to perform mathematical expressions involving
basic arithmetic operators, as well as built-in functions like sin, cos, tan, exp, log, and sqrt.

She wants to implement a feature that converts the entered in x expression to post x notation
using a stack data structure.

Example

Input: sin(3x)+cos(4x)

Output: sin3xcos4x+
Input format :

The rst line of input consists of the in x expression with mathematical functions sin, cos, tan,
exp, log, and sqrt.

The input string will contain only numbers and parentheses and the above-mentioned functions.
Output format :

The output displays the post x expression equivalent of the input in x expression.
Code constraints :

The input in x expression will only contain valid arithmetic operators (+, -, *, /, %, ^) and
parentheses ((, )).
fi
fi
fi
fi
fi
fi
fi
fi
fi
The function names will only consist of lowercase alphabetical characters.

The expression will not contain any whitespace characters except for function names.

The length of the in x expression will not exceed 100 characters.


Sample test cases :
Input 1 :

sin(x)+2*cos(y)

Output 1 :

sinx2cosy*+

Input 2 :

sin(cos(x)*tan(y))

Output 2 :

sincosxtany*

Input 3 :

sin(3x)+cos(4x)

Output 3 :

sin3xcos4x+

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

struct Stack {
int top;
unsigned capacity;
char* array;
};

struct Stack* create_stack(unsigned capacity) {


struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->top = -1;
stack->capacity = capacity;
stack->array = (char*)malloc(stack->capacity * sizeof(char));
return stack;
}

int is_empty(struct Stack* stack) {


return stack->top == -1;
}

void push(struct Stack* stack, char item) {


stack->array[++stack->top] = item;
}

char pop(struct Stack* stack) {


if (!is_empty(stack)) {
return stack->array[stack->top--];
fi
}
return '$';
}

char peek(struct Stack* stack) {


if (!is_empty(stack)) {
return stack->array[stack->top];
}
return '$';
}

int is_operator(char ch) {


return (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%' || ch == '^');
}

int get_precedence(char op) {


if (op == '^') {
return 3;
} else if (op == '*' || op == '/' || op == '%') {
return 2;
} else if (op == '+' || op == '-') {
return 1;
}
return 0;
}

void in x_to_post x(const char* in x, char* post x) {


struct Stack* stack = create_stack(strlen(in x));
int i, j;
i = j = 0;

while (in x[i] != '\0') {


if ((in x[i] >= 'a' && in x[i] <= 'z') || (in x[i] == '(')) {
push(stack, in x[i]);
i++;
} else if (in x[i] == ')') {
while (!is_empty(stack) && peek(stack) != '(') {
post x[j++] = pop(stack);
}
pop(stack);
i++;
} else if (is_operator(in x[i])) {
while (!is_empty(stack) && get_precedence(in x[i]) <= get_precedence(peek(stack))) {
post x[j++] = pop(stack);
}
push(stack, in x[i]);
i++;
} else {
while (in x[i] >= '0' && in x[i] <= '9') {
post x[j++] = in x[i++];
}
post x[j++] = ' ';
}
}

while (!is_empty(stack)) {
post x[j++] = pop(stack);
}

post x[j] = '\0';


fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
}

int main() {
char in x[100];
char post x[100];

scanf("%s", in x);

in x_to_post x(in x, post x);

printf("%s\n", post x);

return 0;
}

Question No: 2
reportIcon
Single File Programming Question

Problem Statement

Suppose you are building a calculator application that allows users to enter mathematical
expressions in in x notation. One of the key features of your calculator is the ability to convert the
entered expression to post x notation using a Stack data structure.

Write a function to convert in x notation to post x notation using a Stack.

Function Prototype: void in xToPost x(char *in x, char *post x)


Input format :

The input consists of an in x expression that includes only digits(0-9) and operators(+, -, *, /).
Output format :

The output displays the equivalent post x expression of the given in x expression.
Code constraints :

The in x expression will contain only valid arithmetic operators (+, -, *, /), numbers, and
parentheses.

The in x expression will have a maximum length of 100 characters.

The numbers in the in x expression will be non-negative integers.


Sample test cases :
Input 1 :

1+2*3/4-5

Output 1 :

123*4/+5-

Input 2 :

5+6-4*8/2

Output 2 :
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
56+48*2/-

Whitelist
Set 1:
in xToPost x

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

struct Stack {
int top;
unsigned capacity;
char* array;
};

struct Stack* create_stack(unsigned capacity) {


struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->top = -1;
stack->capacity = capacity;
stack->array = (char*)malloc(stack->capacity * sizeof(char));
return stack;
}

int is_empty(struct Stack* stack) {


return stack->top == -1;
}

void push(struct Stack* stack, char item) {


stack->array[++stack->top] = item;
}

char pop(struct Stack* stack) {


if (!is_empty(stack)) {
return stack->array[stack->top--];
}
return '$';
}

char peek(struct Stack* stack) {


if (!is_empty(stack)) {
return stack->array[stack->top];
}
return '$';
}

int is_operator(char ch) {


return (ch == '+' || ch == '-' || ch == '*' || ch == '/');
}

int get_precedence(char op) {


if (op == '+' || op == '-') {
return 1;
} else if (op == '*' || op == '/') {
return 2;
}
return 0;
}
fi
fi
void in x_to_post x(char* in x, char* post x) {
struct Stack* stack = create_stack(strlen(in x));
int i, j;
i = j = 0;

while (in x[i] != '\0') {


if (in x[i] >= '0' && in x[i] <= '9') {
while (in x[i] >= '0' && in x[i] <= '9') {
post x[j++] = in x[i++];
}
post x[j++] = ' ';
} else if (is_operator(in x[i])) {
while (!is_empty(stack) && get_precedence(in x[i]) <= get_precedence(peek(stack))) {
post x[j++] = pop(stack);
}
push(stack, in x[i]);
i++;
} else if (in x[i] == '(') {
push(stack, in x[i]);
i++;
} else if (in x[i] == ')') {
while (!is_empty(stack) && peek(stack) != '(') {
post x[j++] = pop(stack);
}
pop(stack);
i++;
} else {
i++;
}
}

while (!is_empty(stack)) {
post x[j++] = pop(stack);
}

post x[j] = '\0';


}

int main() {
char in x[100];
char post x[100];

scanf("%s", in x);

in x_to_post x(in x, post x);

printf("%s\n", post x);

return 0;
}

Question No: 3
reportIcon
Single File Programming Question

Problem Statement
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
In a prestigious educational institute, Professor Smith designs a programming challenge for
Computer Science students. As part of an assessment on Data Structures and Algorithms,
students are presented with a post x expression that involve mathematical operations.

The challenge requires students to develop a robust algorithm to evaluate these expressions
accurately and e ciently. This exercise not only hones their coding skills but also emphasizes the
importance of understanding stack-based computations, enhancing their problem-solving
capabilities for real-world software development scenarios.

Write a program to evaluate the given post x expression, and display the result.
Input format :

The input consists of the post x mathematical expression.

The expression will contain real numbers and mathematical operators ( +, -, *, / ), without any
space.
Output format :

The output prints the result of evaluating the given post x expression.
Code constraints :

The arithmetic operators to be included in the expression are +, -, *, and /.


Sample test cases :
Input 1 :

82/

Output 1 :

Input 2 :

545*+5/

Output 2 :

Input 3 :

82-4+

Output 3 :

10

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

struct Stack {
int top;
unsigned capacity;
double* array;
};

struct Stack* create_stack(unsigned capacity) {


ffi
fi
fi
fi
fi
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->top = -1;
stack->capacity = capacity;
stack->array = (double*)malloc(stack->capacity * sizeof(double));
return stack;
}

int is_empty(struct Stack* stack) {


return stack->top == -1;
}

void push(struct Stack* stack, double item) {


stack->array[++stack->top] = item;
}

double pop(struct Stack* stack) {


if (!is_empty(stack)) {
return stack->array[stack->top--];
}
return 0.0; // Returns 0.0 as a placeholder if the stack is empty
}

double evaluate_post x(char* expression) {


struct Stack* stack = create_stack(strlen(expression));

for (int i = 0; expression[i] != '\0'; ++i) {


if (isdigit(expression[i])) {
double operand = 0.0;
while (isdigit(expression[i])) {
operand = operand * 10 + (expression[i] - '0');
i++;
}
push(stack, operand);
i--; // Move back one position after reading the last digit of the operand
} else {
double operand2 = pop(stack);
double operand1 = pop(stack);
switch (expression[i]) {
case '+':
push(stack, operand1 + operand2);
break;
case '-':
push(stack, operand1 - operand2);
break;
case '*':
push(stack, operand1 * operand2);
break;
case '/':
if (operand2 != 0) {
push(stack, operand1 / operand2);
} else {
// Handle division by zero
printf("Error: Division by zero\n");
exit(EXIT_FAILURE);
}
break;
default:
// Ignore other characters
break;
}
fi
}
}

double result = pop(stack);


free(stack->array);
free(stack);
return result;
}

int main() {
char expression[100];

// Input post x expression


scanf("%s", expression);

// Evaluate and output the result


printf("%g\n", evaluate_post x(expression));

return 0;
}
fi
fi

You might also like