Question

C++ problem

Please help me with it. Thank you.

Project Description / Specification Your program takes as input three integer values (same line, space separated) the first two integers, the beginning and ending value in a range of integers you are going to examine, as space separated integers. They are in that order: the first is the lower range and the second is the upper range. Calculate the sequence for each value in that range inclusive of the endpoints the third integer a value indicating whether to print each sequence: 1 (indicating yes) or 0 (indicating no) * Your program will print (look at the Mimir test cases for examples) the sequence for each number in the range on a separate line if the third input value is 1 . note in printing the sequence the numbers are followed by a comma, except for the last number in the sequence. A personal pet peeve of mine. o On a separate line, two comma separated numbers. For the sequence that is the longest, print the starting number and the length of the sequence (again, see Mimir for format) On a separate line, two comma separated numbers. For the sequence that contains the biggest number, print the starting number and the biggest number in that sequence. . Requirements I. You should check that the first two input range numbers are >= 2, and that the smallest (the first entered value) is indeed strictly smaller than the second (the second value). If not, the program prints an error message and halts Notes I. The % (modulus) operator is useful for this project 2. If you ask for a large enough element, you might overflow an integer. If you go big enough, you will overflow a long (though it will take awhile)

the output would be like this:

10-20 with sequence

10 - 30 no sequence:

this is the background:

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

Required program

#include <iostream>

#include <cmath>

using namespace std;

// This function calculates the juggler sequence

void printJuggler(long n, int toPrint, long &largest, long &length)

{

// n is the number whose sequence is to be calculated

// toPrint determines whether to print or not

long a = n;

// print the first term

if (toPrint)

cout << a << ": ";

// calculate terms until last term is not 1

length = 0;

  

while (a != 1)

{

length++;

long b = 0;

// Check if previous term is even or odd

if (a % 2 == 0)

b = floor(sqrt(a));

else

b = floor(sqrt(a)*sqrt(a)*sqrt(a));

if (toPrint)

{

// print comma only if more terms exist

if (b != 1)

cout << b << ", ";

else

cout << 1 << endl;

}   

a = b;

if(b>largest)

largest = b;

}

}

int main()

{

long start, stop;

int toPrint;

cin>>start>>stop>>toPrint;

if (start<2 || stop<2 || start>=stop)

{

cout << "Please enter proper ranges! Exiting!" << endl;

return 1;

}

long maxLength = -1, globalLargest = -1;

long length, largest;

long startMaxLength, startLargest;

for(long i = start ; i <= stop ; i++)

{

largest = -1;

printJuggler(i, toPrint, largest, length);

// if longer sequence found, increase maxLength

if (length > maxLength)

{

maxLength = length;

startMaxLength = i;

}

// if larger number found in sequence, set globalLargest

if (largest > globalLargest)

{

globalLargest = largest;

startLargest = i;

}

}

// print the largest length and the largest number and starting of sequence

cout << startMaxLength << ", " << maxLength << endl;

cout << startLargest << ", " << globalLargest << endl;

return 0;

}


Sample Output:
codestation@codestation:/temp/HomeworkLib$ ./a.out 10 20 1 10: 3, 5, 11, 36, 6, 2, 1 11: 36, 6, 2, 1 12: 3, 5, 11, 36, 6, 2, 1 13:

I have explained the program in details using the comments. Your question already contains a detailed description of how to generate the numbers. I hope you find this useful.

Add a comment
Know the answer?
Add Answer to:
C++ problem Please help me with it. Thank you. the output would be like this: 10-20...
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
  • Must use Array. Must be in JAVA. Write a program that uses a Scanner object to...

    Must use Array. Must be in JAVA. Write a program that uses a Scanner object to read in a sequence of integers. The first number in the sequence represents the number of integers in the sequence (i.e., do NOT include the first number as part of the sequence). Create an array of integers that is the same size as the number of integers. Then the program will read each integer and place it in the array. Print the content of...

  • Hi, I was wondering if you'd be able to help me figure out how to evaluate...

    Hi, I was wondering if you'd be able to help me figure out how to evaluate the sum of the numbers in char data type since built in data type is not applicable (cannot contain 80 digits).(C programming).(the problem is written below). Is it necessary to use multidimensional array ? A country has a budget of more than 81 trillion yen. We want to process such data, but conventional integer type which uses signed 32 bit can represent up to...

  • Plz i want answer these question using list in python programme. You are given two sequences...

    Plz i want answer these question using list in python programme. You are given two sequences of n integers: 21, 22, ...,an and b1,b2, ..., bn Print a sequence of 2n integers created by alternating elements from the given sequences: a1, 61, 42, 62, a3, 63, ..., an, bn. Input The first line contains a positive integer n (1 <n<1000) - the length of the sequences The second line contains n space-separated integers 01, 02, ..., an (-1000 <a; <...

  • This is to be written in C++. Thank you so much for you help as I'm...

    This is to be written in C++. Thank you so much for you help as I'm really struggling with this. Must be written in this format: #include <stdio.h> int main(void) { printf("Hello World\n"); return 0; } The explosive growth of Internet communications and data storage on Internet-connected computers has greatly increased privacy concerns. The field of cryptography is concerned with coding data to make it difficult (and hopefully—with the most advanced schemes—impossible) for unauthorized users to read. In this exercise,...

  • write the program with the language NODEJS need help Programming challenge, screenshot your work and output.  ...

    write the program with the language NODEJS need help Programming challenge, screenshot your work and output.   Description A positive integer is a palindrome if its decimal representation (without leading zeros, is a palindromic string (a string that reads the same forwards and backwards). For example, the number 5, 77, 363, 4884, 11111, 12121 and 349943 are palindromes A range of integers is interesting if it contains an even number of palindromes. The range [L, R], with L <= R, is...

  • Hi can anyone help me with this question? Please use python when you do it. THANKS...

    Hi can anyone help me with this question? Please use python when you do it. THANKS 1. Arithmetic trees 50 marks You are given an input file with multiple pairs of input lines. The first line of each pair is a tree given as a predecessor array. The second line is the value at the corresponding node. Values at leaf nodes (nodes with no children) are integers. At non-leaf nodes, the two possible values are or * The tree represents...

  • I need help writing these 2 programs please include all the indents :) In this programming...

    I need help writing these 2 programs please include all the indents :) In this programming challenge you are to create two Python programs: randomwrite.py andrandomread.py. One program, randomwrite.py, is to write a set of random numbers to a file. The second program, randomread.py, is to read a set of random numbers from a file, counts how many were read, displays the random numbers, and displays the total count of random numbers. Random Number File Writer (randomwrite.py) Create a program...

  • urgent Help needed in python program ! Thanx # This is a function definition. You can...

    urgent Help needed in python program ! Thanx # This is a function definition. You can ignore this for now. def parse_integer_list_from_string(string): """ Returns a list of integers from a string containing integers separated by spaces. """ # Split the line into a list of strings, each containing a number. number_string_list = string.split() # Create an empty list to store the numbers as integers. numbers = [] # Convert each string to an integer and add it to the list...

  • please use c++ programming and single dimensional arrays to solve this problem thank you Problem 02:...

    please use c++ programming and single dimensional arrays to solve this problem thank you Problem 02: Large Integer (20 points) In CH, the largest int value is 2147483647. So, an integer larger than this cannot be stored and processed as an integer. Similarly, if the sum or product of two positive integers is greater than 2147483647, the result will be incorrect. One way to store and manipulate large integers is to store each individual digit of the number in an...

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