Question

**Program must compile under Ubuntu! (35 pts) Write a C++ program A4p2.cpp with a class of...

**Program must compile under Ubuntu!

(35 pts) Write a C++ program A4p2.cpp with a class of your own design. The class should contain a protected int member variable var, which is initialized with an integer value between 1 and 50 in a constructor that takes an integer parameter. The class should contain a public member function called playthat should print out a sequence of integers as a result of iteratively applying a math function f to the member variable var together with the length of this sequence. The function f is defined as f(x)=(3x+1)/2 if x is odd and f(x)=x/2 if x is even. Stop the iteration when the value 1 is reached. (Example: When var is 6, the play function’s output sequence should be 6,3,5,8,4,2,1 and the length of this sequence is 7.) In your main function create an object of this class whose member var should be initialized through constructor with the first command line argument to your program (i.e., argv[1]) and then call the play member function on the object. You can check whether the supplied first command line argument is an integer between 1 and 50 in your main function. A sample run can look like the following. Submit source code A4p2.cpp and you don’t need to submit a screen shot.

[kwang@computer][~/temp]$./A4p2 6

6  3    5    8    4    2    1

Length of the sequence: 7

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

#include<iostream>

#include<cstdlib>

using namespace std;

class Sequence{

public:

int var;

Sequence(int k){

var = k;

}

void play(){

int count = 1;

cout<<var<<" ";

while(var != 1){

if(var%2==0){

var = var/2;

}else{

var = (3*var + 1)/2;

}

cout<<var <<" ";

++count;

}

cout<<"\nLength of the sequence: "<<count<<endl;

}

};

int main(int argc, char ** argv)

{

int val;

if(argc < 2){

cout << "Invalid number of argument\n";

exit(0);

}

int n = atoi(argv[1]);

if(n < 1 || n > 50){

cout<<"Value not within range 1-50";

exit(0);

}

Sequence seq(n);

seq.play();


return 0;

}

===================================
SEE OUTPUT

PLEASE COMMENT if there is any concern.

=============================

Add a comment
Know the answer?
Add Answer to:
**Program must compile under Ubuntu! (35 pts) Write a C++ program A4p2.cpp with a class of...
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
  • Part (A) Note: This class must be created in a separate cpp file Create a class...

    Part (A) Note: This class must be created in a separate cpp file Create a class Employee with the following attributes and operations: Attributes (Data members): i. An array to store the employee name. The length of this array is 256 bytes. ii. An array to store the company name. The length of this array is 256 bytes. iii. An array to store the department name. The length of this array is 256 bytes. iv. An unsigned integer to store...

  • Write a complete C++ program that defines the following class: Class Salesperson { public: Salesperson( );...

    Write a complete C++ program that defines the following class: Class Salesperson { public: Salesperson( ); // constructor ~Salesperson( ); // destructor Salesperson(double q1, double q2, double q3, double q4); // constructor void getsales( ); // Function to get 4 sales figures from user void setsales( int quarter, double sales); // Function to set 1 of 4 quarterly sales // figure. This function will be called // from function getsales ( ) every time a // user enters a sales...

  • please use c++ Write a program that contains a class Rectangle with two private double precision...

    please use c++ Write a program that contains a class Rectangle with two private double precision members iLength and iWidth, public set and get member functions for these two members, a two argument constructor that sets the length and width to any two user specified values and a void function area() that computes the area and then prints this value by inserting it into the cout output stream. Write a main function that ereates a Rectangle with a length of...

  • C++ Exercice Topic Cover: Class and main implementation 1. Created the class circle.h file 2. Create...

    C++ Exercice Topic Cover: Class and main implementation 1. Created the class circle.h file 2. Create the Circle. cpp 3. Create the main function and use the class Circle in your program (CircleClass) Create a class called circle in other to performing the calculation of the area, sectional area and circumference of any circle. Write a program to test your class. Circle Use double variables to represent the private data of the class. Provide a constructor that enables an object...

  • C++ Program 1a. Purpose Practice the creation and use of a Class 1b. Procedure Write a...

    C++ Program 1a. Purpose Practice the creation and use of a Class 1b. Procedure Write a class named Car that has the following member variables: year. An int that holds the car’s model year. make. A string object that holds the make of the car. speed. An int that holds the car’s current speed. In addition, the class should have the following member functions. Constructor. The constructor should accept the car’s year and make as arguments and assign these values...

  • Please use C++. Write a class named Circle that has a double data member named radius...

    Please use C++. Write a class named Circle that has a double data member named radius and a static double data member named maxRadius. It should have a default constructor that initializes the radius to 1.0. It should have a constructor that takes a double and uses it to initialize the radius. It should have a method called calcArea that returns the area of the Circle (use 3.14159 for pi). It should have a static set method for the maxRadius....

  • Create a CPP file for a C++ Program. Write exactly these functions, power(x,y) function and a...

    Create a CPP file for a C++ Program. Write exactly these functions, power(x,y) function and a print(text, number) function and the main() function. The power(x,y) function returns an integer result that is calculated by raising a number x (integer) to a power y (integer). The second argument y (exponent) of the function can not exceed 100. If the second argument exceeds 100, the function should return -1. Your power(x,y) function must be able to take either 1 or 2 integer...

  • I need help with my homework please. I should write this program in C++ language and...

    I need help with my homework please. I should write this program in C++ language and use Inventory.h file (header file), Inventory.cpp file (implementation file), and main.cpp file (main program). This is the program: Demonstrate the class in a driver program. Input validation, don’t accept negative values for item number, quantity, or cost. 6. Inventory Class Design an Inventory class that can hold information and calculate data for items ma retail store's inventory. The class should have the following private...

  • 6: Write a C+ program (submit a CPP file or link to C++ shell URL) that...

    6: Write a C+ program (submit a CPP file or link to C++ shell URL) that implements the solution to question 2 2: A serial odd parity generator is to be designed. A binary sequence of arbitrary length is presented to the parity generator on input X. When a given bit is presented on input X, the corresponding odd parity bit for the binary sequence is to appear during the same clock cycle on output Z. To indicate that a...

  • In C++ Write a program that contains a class called VideoGame. The class should contain the...

    In C++ Write a program that contains a class called VideoGame. The class should contain the member variables: Name price rating Specifications: Dynamically allocate all member variables. Write get/set methods for all member variables. Write a constructor that takes three parameters and initializes the member variables. Write a destructor. Add code to the destructor. In addition to any other code you may put in the destructor you should also add a cout statement that will print the message “Destructor Called”....

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