You are on page 1of 5

Lahore University of Management and Sciences

Lab-Mid-Term Solution
for
Introduction to Programming
(CS200)

Question-1:
int sumOfDigits(int x) {
if (x >= 0 && x <= 9)
return x;
return (x % 10) + sumOfDigits(x / 10);
}
int main() {
int x = 9123871298;
if (x < 0)
x = x * (-1);
cout << sumOfDigits(x);

return 0;
}

Question-2:
bool is_anagram(string s1, string s2) {
bool check = true;
for (int i = 0; i < s1.size(); i++) {
for (int j = 0; j < s1.size(); j++) {
if (s1[i] == s2[j]) {
s2[j] = '&';
break;
}
}

Wednesday, October 23, 2019 CS-200 Lab Mid Term


}
for (int i = 0; i < s2.size(); i++){
if (s2[i] != '&') {
check = false;
}
}
return check;
}
int main() {
string s1 = "create";
string s2 = "eeatrc";
is_anagram(s1, s2) ? cout<<"They are anagrams" : cout<<"They are not anagrams";

return 0;
}

Question-3:
#include<iostream>
#include<string>
using namespace std;
class Polynomial {
private:
int* coeffs;
int size;
public:
Polynomial() {
size = 4;
coeffs = new int[4];
for (int i = 0; i < 4; i++)
coeffs[i] = 0;
}
Polynomial(const Polynomial &obj) {
size = obj.size;
coeffs = new int[size];
for (int i = 0; i < size; i++) {
coeffs[i] = obj.coeffs[i];
}
}
void operator = (const Polynomial& obj) {
size = obj.size;
coeffs = new int[size];
for (int i = 0; i < size; i++) {
coeffs[i] = obj.coeffs[i];
}
}
~Polynomial() {
size = 0;
delete[] coeffs;
}
Polynomial(string s) {
size = 4;
coeffs = new int[4];
for (int i = 0; i < 4; i++)
coeffs[i] = 0;
//Loop over the whole string e.g of form "3x3+x+4"
for (int i = 0; i < s.size(); i++) {

Wednesday, October 23, 2019 CS-200 Lab Mid Term


//Check if we have an integer
if (s[i] >= '0' && s[i] <= '9') {

//Check if it is an exponent followed by a variable


if (s[i + 1] == 'x') {

//If it an exponent check if it has power or not


if (s[i + 2] >= '0' && s[i + 2] <= '9') {

//If it has power than assign the exponent to


the right power in the coeffs array
coeffs[s[i + 2] -48] = s[i] -48;
//Skip three elements because we are done with
this term
i += 3;
}

//Else, if it has no power that mean it has Variable x


but no power i.e. power =1
else {
//Assign exponent to power 1 place in coeffs
coeffs[1] = s[i]-48;
//Skip 2 index because we are done with this
term
i += 2;
}
}

//If it is not followed by a variable then it has to be last


exponent
else {
coeffs[0] = s[i]-48;
//Skip 1 index because we are done with this term
i += 1;
}
}
//Otherwise it must be a variable with no exponent meaning exp=1
else if(s[i] == 'x'){
//Check if the variabe has power given
if (s[i+1] >= '0' && s[i+1] <= '9') {
//If it has power than it assign coeff 1 to that power
coeffs[s[i + 1] - 48] = 1;
//Skip 2 index because we are done with this term
i += 1;
}

//if the power is not given than it must be power=1


else {
coeffs[1] = 1;
}
}
}
}

friend ostream& operator <<(ostream& output, Polynomial& obj) {


for (int i = obj.size-1; i >= 0; i--) {
if (obj.coeffs[i] != 0) {

Wednesday, October 23, 2019 CS-200 Lab Mid Term


if (i == 0)
output << obj.coeffs[i];
else
output << obj.coeffs[i] << "X^" << i << " + ";
}
}
output << endl;
return output;
}
Polynomial operator +(Polynomial obj) {
Polynomial result;
result.size = obj.size;
result.coeffs = new int[result.size];
for (int i = 0; i < result.size; i++) {
result.coeffs[i] = this->coeffs[i] + obj.coeffs[i];
}
return result;
}
Polynomial operator -(Polynomial obj) {
Polynomial result;
result.size = obj.size;
result.coeffs = new int[result.size];
for (int i = 0; i < result.size; i++) {
result.coeffs[i] = this->coeffs[i] - obj.coeffs[i];
}
return result;
}
};
int main() {
//Calling Parametrized construstor with string input
Polynomial p1("6x3+x2+5x+4");
cout << endl<< "The First Polynomial is: ";
//Printing p1
cout << p1;

//Calling another parametrized constructor with string


Polynomial p2("x2+2x+3");
cout << endl << "The Second Polynomial is: ";
//Printing p2
cout << p2;

//Creating an object and saving the addition result in p3


Polynomial p3;
p3 = p1 + p2;
cout << endl << "The Result of Addition of the two Polynomials is: ";
//Printing p3
cout << p3;

//Saving the Subtraction result in p3


p3 = p1 - p2;
cout << endl << "The Result of Subtraction of the two Polynomials is: ";
//Printing p3
cout << p3;
return 0;
}

Wednesday, October 23, 2019 CS-200 Lab Mid Term


Bonus-Question:
#include<iostream>
using namespace std;
int main() {
//Declaration and Initialization of the 3-D array
int*** ptr = new int** [3];
int** dimensions = new int* [3];
for (int i = 0; i < 3; i++) {
int rows;
int cols;
cout << "Enter the number of rows for 2D Array-" << i + 1 << ": ";
cin >> rows;
dimensions[i] = new int[2];
dimensions[i][0] = rows;
ptr[i] = new int* [rows];
for (int j = 0; j < rows; j++) {
cout << "Enter the number of cols for 2D Array-" << i + 1 << " and
Row " << j + 1 << " : ";
cin >> cols;
dimensions[i][1] = cols;
ptr[i][j] = new int[cols];
}
}
//Taking input
for (int i = 0; i < 3; i++) {
for (int j = 0; j < dimensions[i][0]; j++) {
for (int k = 0; k < dimensions[i][1]; k++) {
cout << "Enter the Value for [" << i + 1 << "][" << j + 1 <<
"]{" << k + 1 << "] : ";
cin >> ptr[i][j][k];
}
}
}
bool check = true;
//Check if all dimesnions are same
for (int i = 0; i < 2; i++) {
if (dimensions[0][i] != dimensions[1][i] || dimensions[0][i] !=
dimensions[2][i] || dimensions[1][i] != dimensions[2][i]){
check = false;
break;
}
}
if (check) {
//If dimensions are same then check the values
for (int i = 0; i < dimensions[0][0]; i++) {
for (int j = 0; j < dimensions[0][1]; j++) {
if (ptr[0][i][j] != ptr[1][i][j] || ptr[0][i][j] !=
ptr[2][i][j] || ptr[1][i][j] != ptr[2][i][j])
check = false;
}
}
}
cout << endl;
check ? cout << "All 2d_arrays are equal" : cout << "All 2d_arrays are not equal";
cout << endl;
return 0;}

Wednesday, October 23, 2019 CS-200 Lab Mid Term

You might also like