Problem (2): Write the following modular program in C++ a. Write a function swap
ID: 3902104 • Letter: P
Question
Problem (2): Write the following modular program in C++ a. Write a function swap that takes two integer numbers and swap them (interchange their values) b. Write a function readPair that reads two integer numbers each of which should be greater than O and if the second integer is less than the first, the function swap is called to interchange the values of the two integers. c. Write a function multiple that determines for a pair of integers whether the second integer is a multiple of the first. The function should take two integer arguments and return true if the second is a multiple of the first, false otherwise. d. Write a main function that will test k pairs of integer numbers. For each pair, the function readPair is called first to enter values for the two integer numbers, then the function multiply is called to test the two numbers. The program should print out the values of the two integers together with a message notifying whether the second integer is multiple of the first, or notExplanation / Answer
#include <iostream>
using namespace std;
int x, y;
// part a
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
// part b
void readPair()
{
// taking user input
cout << "Enter 2 numbers: ";
cin >> x >> y;
// swapping if y is lesser
if(y < x)
{
swap(&x, &y);
}
}
// part 3
bool multiple(int a, int b)
{
// checking if multiple or not
if(b%a == 0)
return true;
return false;
}
int main() {
int k = 3;
for(int i=0; i<k; i++)
{
readPair();
if(multiple(x, y))
cout << "Multiple" << endl << endl;
else
cout << "Not Multiple" << endl << endl;
}
}
/*SAMPLE OUTPUT
Enter 2 numbers: 4 8
Multiple
Enter 2 numbers: 3 8
Not Multiple
Enter 2 numbers: 2 4
Multiple
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.