Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //CSIT 121
- //Dr. Straight
- //Programming Assignment 1
- //Program written by Salvatore Barone
- #include <iostream>
- #include <iomanip>
- using namespace std;
- int main()
- {
- //Variables used:
- int n; //Positive integer used to compute the orbit function
- int response; //Response to iterations question
- //Input n
- cout << “Please enter a positive integer“ << endl;
- cin >> n;
- while (n > 1)
- {
- cout << “Would you like to see iterations of your function? (Enter Y or N)” << end;
- cin << response;
- if (response == ‘Y’)
- {
- length = orbit (n);
- cout << “The length of your orbit is: “ << length;
- cout << “The iterations of your orbit are: “ << endl;
- cout << setw(length) << endl;
- }
- else if (response == 'N')
- {
- //Calculate Orbit
- length = orbit (n);
- cout << “The length of your orbit is: “ << length;
- }
- cout << “Enter another integer, or 0 to quit” << endl;
- cin >> n;
- }
- system("pause");
- return 0;
- }
- int orbit(int n)
- {
- While (length > 1)
- {
- int length; //the number of terms in the sequence up to, but excluding, the first .
- if (n % 2 == 0) n = n/2;
- if (n % 2 == 1) n = 3*n + 1;
- length = length + 1;
- return length;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement