Assignment of Lab Exercise 1,2
Assignment of Lab Exercise 1,2
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.
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 :
#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;
if (!found) {
(*size)++;
*result = realloc(*result, (*size) * sizeof(struct Term));
(*result)[*size - 1].coe cient = new_coe ;
(*result)[*size - 1].exponent = new_exp;
}
}
}
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);
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 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".
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 :
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;
};
if (*poly == NULL) {
*poly = new_term;
} else {
struct Term* temp = *poly;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = new_term;
}
}
return result;
}
int main() {
struct Term* poly1 = NULL;
struct Term* poly2 = NULL;
display_polynomial(poly1);
display_polynomial(poly2);
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 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 :
2
12
21
2
12
21
Output 1 :
Input 2 :
3
12
21
30
3
12
21
40
Output 2 :
#include <stdio.h>
#include <stdlib.h>
struct Term {
int coe cient;
int exponent;
struct Term* next;
};
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;
}
}
int main() {
struct Term* poly1 = NULL;
struct Term* poly2 = NULL;
int n, m;
scanf("%d", &n);
scanf("%d", &m);
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");
}
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.
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;
};
while (!is_empty(stack)) {
post x[j++] = pop(stack);
}
int main() {
char in x[100];
char post x[100];
scanf("%s", in 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.
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.
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;
};
while (!is_empty(stack)) {
post x[j++] = pop(stack);
}
int main() {
char in x[100];
char post x[100];
scanf("%s", in 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 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 :
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;
};
int main() {
char expression[100];
return 0;
}
fi
fi