Question

For C++ /* rewrite the following program to generate pairs of random number until the sum...

For C++

/*

rewrite the following program to generate pairs of random number

until the sum of the digits of one random number equals

the sum of the digits of the other random number.

use only while loops in your solution.

use only loop conditions to end the loop or escape the body of the loop.

for example,

23( 5) 71( 8)

92(11) 6( 6)

13( 4) 99(18)

12( 3) 47(11)

63( 9) 18( 9)

*/

#include <cmath>

#include <ctime>

#include <iomanip>

#include <iostream>

#include <random>

#include <string>

using namespace std;

int main()

{

//int seed = 0;

int seed = (int)time(nullptr);

default_random_engine e(seed);

uniform_int_distribution<int> u(10, 100000);

int original_number = u(e);

int number = original_number;

while (number != 0)

{

int digit = number % 10;

cout << setw(6) << number << '/' << setw(1) << digit <<

endl;

number = number / 10;

}

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1


Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you


/*

rewrite the following program to generate pairs of random number

until the sum of the digits of one random number equals

the sum of the digits of the other random number.

use only while loops in your solution.

use only loop conditions to end the loop or escape the body of the loop.

for example,

23( 5) 71( 8)

92(11) 6( 6)

13( 4) 99(18)

12( 3) 47(11)

63( 9) 18( 9)

*/
#include <cmath>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <random>
#include <string>
using namespace std;
int sumOfDigits(int num){
int sum = 0;
int digit;
while(num > 0){
digit = num % 10;
num /= 10;
sum += digit;
}
return sum;
}
int main()
{
//int seed = 0;
int seed = (int)time(nullptr);
default_random_engine e(seed);
uniform_int_distribution<int> u(10, 100000);
int num1 , num2;
int sum1, sum2;
do{
num1 = u(e);
num2 = u(e);
sum1 = sumOfDigits(num1);
sum2 = sumOfDigits(num2);
cout << setw(5) << num1 << "(" << setw(3) << sum1 << ")\t" <<
setw(5) << num2 << "(" << setw(3) << sum2 << ")" << endl;
}while(sum1 != sum2);
return 0;
}

output
-------
51126( 15) 84293( 26)
15208( 16) 73384( 25)
80108( 17) 5968( 28)
49702( 22) 25209( 18)
84829( 31) 78017( 23)
86268( 30) 46712( 20)
9657( 27) 43589( 29)
78652( 28) 88356( 30)
62907( 24) 43945( 25)
78674( 32) 73452( 21)
76202( 17) 72418( 22)
1627( 16) 60576( 24)
1252( 10) 42766( 25)
6196( 22) 88303( 22)

Add a comment
Know the answer?
Add Answer to:
For C++ /* rewrite the following program to generate pairs of random number until the sum...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is for C++ #include <random> #include <iostream> #include <ctime> using namespace std; /* In the...

    This is for C++ #include <random> #include <iostream> #include <ctime> using namespace std; /* In the game of craps, a shooter rolls 2 dice and adds the dots on the upper most faces of the dice. 7 or 11 on the first roll wins, 2, 3, or 12 on the first roll loses, andthing else is call the point and the player rolls again The following program fragment uses 1-way if statements simulate the 1st roll of the dice. Replace...

  • ​Declare an array with 1000 elements of type int Then in a loop generate 1000 random numbers and assign one to each element in the array The random numbers should be between 1 and 50 Then, prompt the user to enter a number, store this number in a local

    Rules to follow are:Declare an array with 1000 elements of type intThen in a loop generate 1000 random numbers and assign one to each element in the arrayThe random numbers should be between 1 and 50do not use rand or srand, use code is providedThen, prompt the user to enter a number, store this number in a local variable, the number should be safety checked using the GetInteger() functionThen, iterate through the array and determine how many times the user's...

  • Program Instructions: Write a C++ program that uses a random number generator to generate a two...

    Program Instructions: Write a C++ program that uses a random number generator to generate a two digit positive integer and allows the user to perform one or more of the following operations: Double the number. Reverse the digits of the number. Raise the number to the power of 2, 3, or 4. Sum the digits of the number. If the number is a two digit number, then raise the first digit to the power of the second digit. If the...

  • Write a c++ code into the given code  to find composite numbers from the given random number...

    Write a c++ code into the given code  to find composite numbers from the given random number list. The composite numbers is only counted once if there is a repeated number. I need to use this code and add on a code to find if the numbers generated is a composite function. Please help #include <cmath> #include <cstdlib> #include <ctime> #include <iostream> #include <vector> using namespace std; int main() {    srand(time(NULL)); int size_of_list = 0; // the number of random...

  • This is C++. The task is to convert the program to make use of fucntions, but...

    This is C++. The task is to convert the program to make use of fucntions, but I am am struggling to figure out how to do so. #include <iostream> #include <iomanip> using namespace std; main(){ char empid[ 100 ][ 12 ];    char fname[ 100 ][ 14 ], lastname[ 100 ][ 15 ]; int hw[ 100 ]; double gp[ 100 ], np[ 100 ], hr[ 100 ], taxrate[100], taxamt[ 100 ]; int counter = 0; int i; cout<<"ENTER EMP ID,...

  • Write a program that uses a random number generator to generate a two digit positive integer...

    Write a program that uses a random number generator to generate a two digit positive integer and allows the user to perform one or more of the following operations: a. Double the number. b. Reverse the digits of the number. c. Raise the number to the power of 2, 3, or 4. d. Sum the digits of the number. e. If the number is a two digit number, then raise the first digit to the power of the second digit....

  • C++ Modify this program (or create your own) so that it performs the following tasks: 1....

    C++ Modify this program (or create your own) so that it performs the following tasks: 1. Insert the numbers into the list in sorted order (ascending). This will require you to keep the list sorted as you continue to insert new numbers. 2. When adding a new number, first check to see whether it is larger than the last number currently in the list. If it is, add it directly to the end of the list, i.e., do not traverse...

  • Write a c++ program that simulates a million of games in craps. I am having a...

    Write a c++ program that simulates a million of games in craps. I am having a trouble getting to loop a million of times. I am trying to use a const for a million. This is the code so far. #include <iostream> #include <cstdlib>// contains prototypes for functions srand and rand #include <ctime>// contains prototype for function time #include <iomanip> using namespace std; int rollDice(); // rolls dice, calculates and displays sum void printstats(); int totroll = 0, games, point,...

  • C++ please 27.5 Loops (nested)**: Sum a given number of integers A user will enter an...

    C++ please 27.5 Loops (nested)**: Sum a given number of integers A user will enter an initial number, followed by that number of integers. Output those integers' sum. Repeat until the initial number is O or negative. Ex: If the user enters 3 96 1 0, the output is 16 Ex: If the user enters 396125 3 0, the output is 16 Hints: Use a while loop as an outer loop. Get the user's initial number of ints before the...

  • Create a program using the Random class and While loops in Java. The program should generate...

    Create a program using the Random class and While loops in Java. The program should generate a random number from 50 through 100. The loop should add the five random numbers generated. On each iteration of the loop the program should print out the number generated, how many numbers have been generated thus far, and the sum so far. On the last iteration, the printout should say the The final total is.... Teach comment the last time I did it:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT