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

book: c++ prgramming:from problem analysis to program design (6th edition) by: D

ID: 647326 • Letter: B

Question

book: c++ prgramming:from problem analysis to program design (6th edition)

by: D.S. Malik

chapter 12 program Exercise 4 page 849

4. Programming Exercise 11 in Chapter 8 explains how to add large integers using

arrays. However, in that exercise, the program could add only integers of, at

most, 20 digits. This chapter explains how to work with dynamic integers.

Design a class named largeIntegers such that an object of this class can

store an integer of any number of digits. Add operations to add, subtract,

multiply, and compare integers stored in two objects. Also add constructors to

properly initialize objects and functions to set, retrieve, and print the values

of objects.

Explanation / Answer

#include <iostream>
#include <string>

using namespace std;

class largeInts{
  
public:
    int num1, num2;
    void addInts(); // function to add values
    void multiplyInts(); // function to multiply values
    void subtractInts(); // function to subtract values
    void compareInts(); // function to compare values
    void printResults(); // function to print results
    void setValues(int, int); // function to set two values
    int* values; // pointer with int value to store multiple values (?)
};

void largeInts::setValues(int x, int y){
  
    num1 = x;
    num2 = y;
}

void largeInts::addInts(){
    cout << num1 + num2;
}
void largeInts::multiplyInts(){

cout<<num1*num2;

}
void largeInts::subtractInts(){

cout<< num1- num2;

}

void largeInts::compareInts(){

if(num1<num2){

cout<< num2 +"is big";

} else {

cout<< num1 +"isbig";

}

}

int main() {
    largeInts test;
    test.setValues(2147483647, 10000000);
    test.addInts();
  
    return 0;
}