473,396 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Can someone look through this code for me?

24
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6. int main(int argc, char* argv[]) {
  7.  
  8.     int Count1 = 1;
  9.     int Count2 = 1;
  10.     int Count = 0;
  11.     int number1,number2;
  12.  
  13. cin>>number1>>number2;   
  14.  
  15.     while (number1 != 1)  
  16.     {      
  17.            if (number1%2==0)     
  18.            {
  19.               number1 = number1/2;  
  20.               cout<<number1<<" ";
  21.            }  
  22.            else 
  23.            {
  24.                 number1 = 3*number1+1;      
  25.                 cout<<number1<<" ";
  26.            }
  27.            ++Count1;
  28.     }
  29. while (number2 != 1)
  30.     {
  31.           if (number2%2==0)
  32.           {
  33.                  number2 = number2/2;  
  34.                  cout<<number2<<" ";
  35.           }
  36.           else
  37.           {
  38.                   number2 = 3*number2+1;      
  39.                   cout<<number2<<" ";
  40.           }
  41.           ++Count2;
  42.     }
  43.  
  44.           Count = Count1 + Count2; 
  45.  
  46.     cout<<endl<<number1<<" "<<number2<<" "<<Count<<endl;
  47.  
  48.     system("pause");
  49.     return 0;
The problem states that,
For any two numbers i and j you are to determine the maximum
cycle length over all numbers between i and j. (in this case, i put i and j as number1 and number 2)..

sample input is 1 10
sample output is 1 10 20

Howver, for my codes, when i input in 1 10, i get 1 1 8.. I cant get the output as wat it wants.. :(
Jan 18 '07 #1
5 1556
r035198x
13,262 8TB
#include <iostream>
#include <sstream>
#include <string>

using namespace std;
int main(int argc, char* argv[]) {

int Count1 = 1;
int Count2 = 1;
int Count = 0;
int number1,number2;

cin>>number1>>number2;

while (number1 != 1)
{
if (number1%2==0)
{
number1 = number1/2;
cout<<number1<<" ";
}
else
{
number1 = 3*number1+1;
cout<<number1<<" ";
}
++Count1;
}
while (number2 != 1)
{
if (number2%2==0)
{
number2 = number2/2;
cout<<number2<<" ";
}
else
{
number2 = 3*number2+1;
cout<<number2<<" ";
}
++Count2;
}

Count = Count1 + Count2;

cout<<endl<<number1<<" "<<number2<<" "<<Count<<endl;

system("pause");
return 0;


The problem states that,
For any two numbers i and j you are to determine the maximum
cycle length over all numbers between i and j. (in this case, i put i and j as number1 and number 2)..

sample input is 1 10
sample output is 1 10 20

Howver, for my codes, when i input in 1 10, i get 1 1 8.. I cant get the output as wat it wants.. :(
Can you or someone else please explain how one gets the maximum cycle length over two numbers i and j? I've never heard about this cycle length thing.
Jan 18 '07 #2
Nhd
24
Can you or someone else please explain how one gets the maximum cycle length over two numbers i and j? I've never heard about this cycle length thing.

Hi... I can only explain this much to u cos i only know what is cycle length but not maximum cycle length...


Total numbers of elements in sequence until it reaches 1, including input number and 1, is called cycle of the sequence of that given input no. For example if user input 5 then the sequence will be something like this.

5 16 8 4 2 1 = cycle length equals to 6..
Jan 19 '07 #3
r035198x
13,262 8TB
Hi... I can only explain this much to u cos i only know what is cycle length but not maximum cycle length...


Total numbers of elements in sequence until it reaches 1, including input number and 1, is called cycle of the sequence of that given input no. For example if user input 5 then the sequence will be something like this.

5 16 8 4 2 1 = cycle length equals to 6..
All I can see is that from 16 we are dividing by two until we get to 1. but how did we get from 5 to 16? Or am I just incredibly dumb?
Jan 19 '07 #4
Nhd
24
All I can see is that from 16 we are dividing by two until we get to 1. but how did we get from 5 to 16? Or am I just incredibly dumb?
No, ur not... Its juz that the question itself is very confusing... I'll explain it..

when an input is an even number, the outcome will be number=number/2. thats where you can see the number above from 16, is divided into two until we get 1. when it reaches 1, it is considered end of the cycle.

however, when an input is an odd number, the outcome is number=3*number+1, and hence in this case, since 5 is an odd number, it will result in 5*3+1 which is 16.

this will result in the total cycle length which is 6. Howver, I'm supposed to fing the max cycle length in which i still cant figure it out, thus seeking help... I'm so sory to make u so confused over this codes.
Jan 19 '07 #5
RedSon
5,000 Expert 4TB
This sounds like a permutation problem. Given a list of numbers from i to j what is the maximum possible permutations before repeating. In otherwords what is the maximum possible cycle length before repeating. This is a classic problem in combinitorics and can be expressed by the equation:
Expand|Select|Wrap|Line Numbers
  1. factorial(n)/(factorial(n-r)) 
  2. where r is the size of each permutation and n is the size of the sequence from which the elements are permuted
  3.  
The trick for your program is going to be the factorial calculation if you are not able to use math libraries.
Jan 19 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Sue | last post by:
Hello! I am back with another question. Remember I am a new JavaScript student and I am aware that this code does not check for all the possibilities and that as a "NEW" JavaScript student I am...
4
by: Aaron Ackerman | last post by:
I am using typed datasets in an N-Tier Windows app using VB.NET. I know this posting cannot be fully explained in a single post that is why I am asking for someone to point me to a real world...
6
by: Helmut Giese | last post by:
Hello out there, I am a rather experienced C programmer. However, today I got a javascript assignment because someone left (something like: "You're a great programmer - you'll handle this.") and I...
21
by: MLH | last post by:
A97 procedure to open http://www.arch.com/message/ enter an 800 number, press "Continue", enter a text msg string from a memo field and press "Send" This is a calendaring and appointment A97...
3
by: Marek | last post by:
Hello gurus! I wrote a code in VBS, that will check, that current user is in one from three groups. But i don't know how asimilate it with asp.net. This page will be a bridge between 2 - main...
3
by: Amy | last post by:
Hi, I have 6 If Then Else statements I was supposed to write. I did so but I know that they have to be wrong because they all look the same. Could someone take a look at them and point me in the...
0
by: William Morgan | last post by:
i'm working on a program that downloads from news groups. it works fine on (UUE and MIME" encoded files but when you get to a yenc encoded file it messes up. the lines are all diffrent lengths...
2
by: Ravi | last post by:
Can someone provide Code for vCARD Parser in C language ? I am in the need for vCard Parser which i can integarte on VC++ environment. Thanks in advance Ravindra Singhai
40
by: aslamhenry | last post by:
please key in any 5 digits number : 56789 and the ouput is 5678 9 567 89 56 789 5 6789
1
by: okonita | last post by:
I need major help resolving a UDF error. My environment id DB2 UDBv8.2 and v9.1 on a Linux and Windows server. I am getting a SQL20148N error which states as follows: SQL20148N Routine...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.