Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab Assignment 3 Due on 02/28/2018 1. Reverse a number (integer) by iterative pr

ID: 3723204 • Letter: L

Question

Lab Assignment 3 Due on 02/28/2018 1. Reverse a number (integer) by iterative program. Reverse a number: Input: prompt user to enter an integer. Output: print out the integer reversely e.g.: if user enters 12345, your output will be 54321. 2. Reverse a number (integer) by recursive program. 3. ADT array-based polynomials a. Input/output Input: (1) The degree of the polynomial (the highest degree of all the terms in the polynomial) (2) The coefficient for each term from the highest degree to the lowest degree Output: Print out the polynomial. e.g.: if user enters 3 for the degree of the polynomial and enters 7, 0, 9, 10 for each term from the highest degree to the lowest degree, then the output will be: 7x3+9x+10 b. Evaluation Once the polynomial has been entered by the user, prompt the user to en the value of x and evaluate the polynomial and print out the value of the polynomial e.g. if user enter 2 for the value of x (that is, x 1), then you evaluate the polynomial as 7*243+9*2+10-84 and you print out the value of the polynomial is 84 Add two polynomials. Prompt the user to enter two polynomials (as above in step a), then add these two polynomials and print out the result. e.g.: for the first polynomial, the user enters 3 for the degree of the polynomial and enters 7, 0,9, 10 for each term from the highest degree to the lowest degree; for the second polynomial, the user enters 2 for the degree of the polynomial and enters 1, 2, 3 for each term from the highest degree to the lowest degree, then you have: 1st polynomial: 7x 3+9x+10 2nd polynomial: x2+2x+3 After you add these two polynomials: your have 7x3+x2+11x+13. c.

Explanation / Answer

1.

#include <iostream>

using namespace std;

/* Iterative function to reverse digits of num*/

int reversDigits(int num)

{

int rev_num = 0;

while(num > 0)

{

rev_num = rev_num*10 + num%10;

num = num/10;

}

return rev_num;

}

/*Driver program to test reversDigits*/

int main()

{

int num = 1234;

std::cout << "Reverse of no. is "<< reversDigits(num) <<std::endl;

getchar();

return 0;

}

2.

#include <iostream>

using namespace std;

/* Iterative function to reverse digits of num*/

int reversDigits(int num)

{

static int rev_num = 0;

static int base_pos = 1;

if(num > 0)

{

reversDigits(num/10);

rev_num += (num%10)*base_pos;

base_pos *= 10;

}

return rev_num;

}

/*Driver program to test reversDigits*/

int main()

{

int num = 1234;

std::cout << "Reverse of no. is "<< reversDigits(num) <<std::endl;

getchar();

return 0;

}